My problem is to inverse 2 patterns in only one1 sed command.
Let's considered this text file :
LAYER M1
DIRECTION HORIZONTAL
END M1
LAYER M2
DIRECTION VERTICAL
END M2
So I want to invert HORIZONTAL / VERTICAL words pattern in one pass like this :
LAYER M1
DIRECTION VERTICAL
END M1
LAYER M2
DIRECTION HORIZONTAL
END M2
There is a solution to this over at unix.stackexchange - https://unix.stackexchange.com/questions/21061/cleanly-swap-all-occurences-of-two-strings-using-sed
To apply it here would be:
sed 's/HORIZONTAL/VERTICAL/g; t; s/VERTICAL/HORIZONTAL/g' file
Depends on both VERTICAL and HORIZONTAL not appearing on the same line.
I do not have a proof, but I fear it is not possible in sed (If by "command" you mean sed commands like s, i, b etc.). It is possible with one command in Perl, though:
perl -pe 's/(HORIZONTAL)|(VERTICAL)/$1 ? VERTICAL : HORIZONTAL/e'
sed -e 's/day/temp/g' -e 's/night/day/g' -e 's/temp/night/g' infile
I don't know if sed can do multiple substitutions in one pass. The solution above sets every occurrence of day to temp, then in another pass night to day, and finally in the last pass temp to night.
sed -n "N
$ {
s/HORIZONTAL\(.*\)VERTICAL/VERTICAL\1HORIZONTAL/
t print
s/VERTICAL\(.*\)HORIZONTAL/HORIZONTAL\1VERTICAL/
: print
p
}" YourFile
you need to first load all line in sed.
it work for your sample, if there is pattern border, code have to be modified to limit between pattern edge
sed -e 's/HORIZONTAL/TMP/g;s/VERTICAL/HORIZONTAL/g;s/TMP/VERTICAL/g'
Related
I used to have a way to do this, but it's been lost to time... I have one program that outputs an undeliniated ascii data file, and another program that needs the data formatted it's own way. The output contains X,Y data points in the format:
X120207Y041009
X120107Y040071
etc. ...
where each of these ordinates represents a 2.4 data point. The input file needs it as such:
X 12.0207 Y 04.1009
X 12.0107 Y 04.0071
etc. ...
Not all of the lines in the file are data points, but the ones that are start with "X", have the exact same format, and contain nothing else on that line.
All my searching for a similar conversion points toward using sed as a quick, elegant way to do this, but I never learned sed. I think before I actually wrote a c program to do this conversion but that seems like the really hard way to recreate. If anyone could bail me out, I'll owe you a bagel!
You can make use of gawk's FIELDWIDTHS to handle fixed format text:
awk -v FIELDWIDTHS="1 6 1 6" '/^X/{for(x=2;x<=5;x+=2)sub(/../,"&.",$x)}7' file
Let's see an example:
kent$ cat f
X120207Y041009
X120107Y040071
we want to leave it, no matter what it is
kent$ awk -v FIELDWIDTHS="1 6 1 6" '/^X/{for(x=2;x<=5;x+=2)sub(/../,"&.",$x)}7' f
X 12.0207 Y 04.1009
X 12.0107 Y 04.0071
we want to leave it, no matter what it is
Hi I am looking for a quick and dirty answer,
I try to make a surface or contourplot with gnuplot from a data file. The problem is the fileformat:
Its all in one row
line 1 till line 32 contain the values for the x-coordinates 1-32 and y=1,
line 33 is the value for x=1 and y=2 and so on...
I tried to do that with the "everyline" command but since they are not separated by a blank line this is not working.
Since this data file is an output file and from my program I get many of them, it is not practicable to modify each of them. It would be best if I find a way to do that directly with gnuplot.
I also tried it with "sed" but I am not yet further than extracing values from a specific line to a specific line.
If someone could help me with a quick applicable solution for this problem, it would be great.
You could use awk to add the needed columns, e.g.,
splot "<awk '{print int(NR/32),NR%32,$1}' datafile" u 1:2:3 with points
I may have switched the x and y columns around
I have to make a total amount of simulations (e.g. NUM)
with Matlab witch I run with the following line
./run.sh -d num
repeatedly, where num ( NUM = integer*num) indicates the number of new Matlabs.
The same could happen with any other computing program in C or NS, etc.
The problem is that the computer where these processes run only admits a maximum number of Matlabs, e.g. MAX.
I would like to keep track of the number of Matlabs running and when the maximum number is reduced to (MAX-num), because 'num' has already finished, then run the next 'num' simulations of Matlab until the total NUM of simulations is completed.
To know the number of running processes I run the following script
ps axu |grep plopez|grep simulacion|grep MATLAB|awk '{ $2} END{print NR}'
which gives the total Matlabs running in this moment.
How could I make the whole set of simulations just from a single script?
The quick and dirty way would be to divide your set of simulations into NUM subsets, then you can work sequentially on these subsets.
I used the following script to process some images. The folder structure was like this:
CASE
20p
40p
60p
80p
100p
So I entered the CASE folder in the Terminal and ran my script. This called sequentially matlab and ran the script "processImages" to produce some extract data.
The
list=$(ls | grep p)
is really some quick and dirty line of code. However, it worked in my case and might give you the hint to solve the problem.
Have a very nice day.
#!/bin/bash
list=$(ls | grep p)
echo $list
for d in $list
do
echo $d
cd $d
matlab -nodisplay -nosplash -nodesktop -r "processImages; exit"
cd ..
done
I would like to use these answer to insert the char % at the end of each line in a marked region when using emacs. However, in order to avoid endings like %% I have to first delete all the occurrences of % at the end of the lines in the region. For example
foo%
foo2
foo3%
foo4%%
bar
bar%
should become
foo
foo2
foo3
foo4
bar
bar
Note that not all lines in the region end with % (otherwise I would be done), and some might end with more the one %. That is, one cannot simply delete the last char of each line. I guess it is rather simple, but I'm too much of emacs newbie.
C-M-%%+C-qC-jEnterC-qC-j! invokes query-replace-regexp replaceing any number of %'s followed by a newline by a newline.
You can directly do the two steps in one, replacing any existing % characters at end of line with only one.
C-M-% <- query-replace-regexp
%*$ <- 0 or more %s at end of line
Enter
% <- by only one %
Enter
! <- apply to all (optional, see below)
If you are using Transient Mark mode, then the command will operate inside the active region. Otherwise, you may choose to narrow-to-region (C-x n n)to make sure it doesen't affect other parts of the file.
Is there (somewhere) a command-line program for Windows which will create PNG/JPEG visual from MP3/WAV?
EDIT:
This is a good example of how the image should look like.
Sox, "the Swiss Army knife of audio manipulation", can generate accurate PNG spectrograms from sound files. It plays pretty much anything, and binaries are available for Windows. At the most basic level, you'd use something like this:
sox my.wav -n spectrogram
If you want a spectrogram with no axes, titles, legends, and a light background that's 100px high:
sox "Me, London.mp3" -n spectrogram -Y 130 -l -r -o "Me, London.png"
Sox accepts a lot of options if you only want to analyze a single channel for example. If you need your visuals to be even cooler, you could post-process the resulting PNG.
Here is a short overview from the commandline about all available parameters, the manpage has more details:
-x num X-axis size in pixels; default derived or 800
-X num X-axis pixels/second; default derived or 100
-y num Y-axis size in pixels (per channel); slow if not 1 + 2^n
-Y num Y-height total (i.e. not per channel); default 550
-z num Z-axis range in dB; default 120
-Z num Z-axis maximum in dBFS; default 0
-q num Z-axis quantisation (0 - 249); default 249
-w name Window: Hann (default), Hamming, Bartlett, Rectangular, Kaiser
-W num Window adjust parameter (-10 - 10); applies only to Kaiser
-s Slack overlap of windows
-a Suppress axis lines
-r Raw spectrogram; no axes or legends
-l Light background
-m Monochrome
-h High colour
-p num Permute colours (1 - 6); default 1
-A Alternative, inferior, fixed colour-set (for compatibility only)
-t text Title text
-c text Comment text
-o text Output file name; default `spectrogram.png'
-d time Audio duration to fit to X-axis; e.g. 1:00, 48
-S time Start the spectrogram at the given time through the input
A real waveform is possible with ffmpeg, you can download it here.
Install it somewhere and use the following command line as example:
ffmpeg.exe -i "filename.mp3" -lavfi showwavespic=split_channels=1:s=1024x800 waveform.png
or the following to match your example picture color, or other colors:
ffmpeg.exe -i "filename.mp3" -lavfi showwavespic=s=1024x800:colors=0971CE waveform.png
Documentation of FFmpeg showwavespic
I've created a small PHP library that does this: https://github.com/jasny/audio
It works as following. It gets the samples using
sox TRACK.mp3 -t raw 4000 -c 1 -e floating-point -L -
This downsamples the track to 4k and puts everything in 1 channel.
Next I take chunks of samples (per pixel witd) and calculate the min and max. Use them to draw the waveform.
I found this here quite nice (from a web archive, the original one is gone):
http://web.archive.org/web/20140715171716/http://andrewfreiday.com/2011/12/04/optimizing-the-php-mp3-waveform-generator/
its PHP based and uses lame through shell.
update : the site seems dead from time to time, howerver here is the repo : https://github.com/afreiday
An updated, batched version of Wander Nauta which generate histogram for all wav files into folder (BASH/DASH):
for i in *.wav; do ./sox $i -n spectrogram -y 130 -l -r -o ${i%%.wav}.png; done