I have a data file which has the following arrangement :
#REY2_0 REY1_0 alpha1 alpha2 omega
1000 10000 (-3,0) (1,0) (-0.21259151,-0.17763971)
I have to use the REY2_0, REY1_0 and the second element of omega i.e -0.17763971 in this case. How would I be able to use this in splot ? Can I add multiple separators to gnuplot and then use the resulting columns ? How is this done ? Can I change the data file using sed?
Edit :
The sample output would be :
#REY2_0 REY1_0 alpha1 alpha2 omega
1000 10000 -3 0 1 0 -0.21259151 -0.17763971
You can use this sed,
sed 's/[(,)]/\t/g' yourfile
If you want to made the changes in file,
sed -i.bak 's/[(,)]/\t/g' yourfile
To get proper formatted output,
sed 's/[(,)]/\t/g' yourfile | column -t > newupdatedfile
It is working for your sample input file.
Related
This question already has answers here:
How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?
(9 answers)
Closed 4 years ago.
Suppose a file with such content:
a
x
y
z
c
some to be omitted
a
b
c
I want to print all those lines which are between the "a" line and "c" line (both with and without the "c" line are ok):
a
x
y
z
c
a
b
c
I tried the sed command:
sed -n '/a/{:my_tag /./p; N; /c/ t end; t my_tag; :end}'
and it gives me the output:
a
a
Does the N in the command work for once only? I cannot see any loop work out here.
the help info of sed is a little bit confusing and it seemed like I was doing in the wrong way.
Or maybe some tools rather than sed would be more helpful and efficient to this problem and please show me how.
Could you please try following(if ok with awk).
awk '/^a/{flag=1} flag; /^c/{flag=""}' Input_file
Output will be as follows.
a
x
y
z
c
a
b
c
With sed:
sed -n '/^a$/,/^c$/p' file
or
sed '/^a$/,/^c$/!d' file
Output:
a
x
y
z
c
a
b
c
I have a huge text file that has several iterations of the same thing at different times, with a basic structure of:
Header (5 lines)
Data (thousands of lines)
Header (5 lines)
Data (thousands of lines)
Header (5 lines)
Data (thousands of lines)
This repeats and goes on for a while.
I want to cull this file, by removing every other set of Header + Data. I was thinking I'd use sed, but I can't figure out how.
It might be of help that each "cycle" starts with the same line (for the purpose of this example, imagine it says Program X output) and that exact line only appears once, at the beginning of each "cycle".
Thanks
Keep track of how often you see the keywords, and print only when this count is an odd number:
awk '/Program X output/ {n++} n%2 == 1' <<END
Program X output
a
b
c
Program X output
d
e
Program X output
f
g
h
i
j
Program X output
m
n
o
END
Program X output
a
b
c
Program X output
f
g
h
i
j
Sounds like all you need is:
awk '/Program X output/ && c++{exit} 1' file
e.g.
$ seq 50 | awk '/2/ && c++{exit} 1'
1
2
3
4
5
6
7
8
9
10
11
If that's not all you need then edit your question to clarify your requirements and show us concise, testable sample input and expected output.
This might work for you (GNU sed):
sed -r '/Program X output/{x;s/^/x/;x};G;/\n(x{2})*$/!P;d' file
When encountering a header line, add 1 to a counter in the hold space (HS). Append the HS to every line and only print the first line in the pattern space (PS) if the counter is a multiple of the required amount.
I am reading a .dat File and one of headings of a Column contains the & character. When I try to plot the columns via Gnuplot the & character disapears in the name of the function. I am using the columnheader function to get the name. How do I have to adjust the name of the Column?
Current Column headings:
i D&C
You can double escape the & character in the data file like this:
i D\\&C
1 4
2 3
3 5
Now the plot command works:
plot "a.dat" w lp title columnheader
This is the result:
I have created 800 Poisson distributed random numbers. then write those numbers in a .txt file. I want to write my each data value in new line like,
1
2
3
but it is coming like
1 2 3..
I used dlmwrite as,
dlmwrite('rts2_data.txt',rts2, '\t');
Which delimiter should I use to take each data value in new line?
I don't know specifically about Matlab, but \t is the tabulation character.
If you want a new line, perhaps you could use the new line character, \n, or maybe \r\n if it does allow more than one character (\r is a "carriage return").
Ok, so Matlab doesn't allow to place the new line character directly as delimiter. Instead, you can use this syntax:
dlmwrite('rts2_data.txt', rts2, 'delimiter', ' ', 'newline', 'pc');
As seen here. You can also check out this page which documents the parameters available for the dlmwrite function.
You can arrange the data as a column vector initially (not a row). dlmwrite tries to keep a matrix structure you have.
Here is my working example:
z=[0 1 2 3]
dlmwrite('rts2_data1.txt',z)
dlmwrite('rts2_data2.txt',z')
and the outputs of the files are:
rts2_data1.txt
0,1,2,3
rts2_data2.txt
0
1
2
3
I have several equations mixed throughout a document, appearing in the following forms:
5^4 %A
3^-1 %B
5.01 x 10^2.05 %C
5.01 x 10^2 %D
-5 x 10^3 %E
In other words, they fit in the format of x^y, or z * x^y, where z, x, and y can be any integer or rational number (expressed with a decimal point), positive or negative.
I wish to convert these to math mode for TeX. E.g.:
$5.01 \cdot 10^2$
With much assistance from others, I have managed to create this BASH script with sed to solve items A and B:
sed "s/\-\{0,1\}[0-9]\{1,\}^\-\{0,1\}[0-9]\{1,\}/$&$/" input > output
This is able to convert items A and B to math mode, but I found it only converts the first occurrence it finds within a line. For instance, if a line says 5^10 is greater than 1^2 it converts this to $5^10$ is greater than 1^2. A second pass with the script results in $$5^10$$ is greater than 1^2.
I managed to modify the above script to handle items C, D, and E, but cannot figure out how to handle the back second part (I have marked it with "???"):
sed "s/\-\{0,1\}[0-9]\{1,\}\ x\ \-\{0,1\}[0-9]\{1,\}^\-\{0,1\}[0-9]\{1,\}/???/" input > output
This presents a problem:
Even if the above could work, if I first run the first sed script, then run the second, the first confuses the second, i.e. I would end up with 5.01 x $10^2.05$. If I ran the second script first, I would end up with $5.01 x $10^2.05$$ after running the second script.
In short, how can I perform this kind of conversion for all items within a document?
5^4 --> $5^4$
3^-1 --> $3^-1$
5.01 x 10^2.05 --> $5.01 \cdot 10^2.05$
5.01 x 10^2 --> $5.01 \cdot 10^2$
-5 x 10^3 --> $-5 \cdot 10^3$
but I found it only converts the first occurrence it finds within a line
Use the /g global replacement flag.
Converting your text is best done in several passes
Pass 1
sed 's/\(-\?[0-9].\?[0-9]*\) x \(-\?[0-9]\{1,\}\)^\([0-9]\{1,\}\.\?[0-9]*\)/$\1 cdot \2^^\3$/g' input > tmp
What we've done here is capture \(...\) x \(...\)^\(...\) into the sed remembered patterns \1 \2 and \3 which we then use to convert the text.
This deals with your %C,%D,%E and for example converts 5.01 x 10^2.05 into $5.01 cdot 10^^2.05$. Note that we have converted the occurrences of ^ into ^^ temporarily.
Pass 2
sed -i 's/-\?[0-9]\+\^-\?[0-9]\+/$&$/g' tmp
This deals with your examples %A and %B. As we previously converted the ^ in 10^2.05 to ^^ this was ignored by pass 2 solving the problems you noted.
Pass 3
sed -i 's/\^^/^/g' tmp
Which simply converts the ^^ back into ^
Based on the output you need, will this following method work for you?
[jaypal~/Temp]$ cat file0
5^4
3^-1
5.01 x 10^2.05
5.01 x 10^2
-5 x 10^3
[jaypal~/Temp]$ sed -e 's/^/\$/' -e 's/$/\$/' -e 's/x/\\cdot/' file0
$5^4$
$3^-1$
$5.01 \cdot 10^2.05$
$5.01 \cdot 10^2$
$-5 \cdot 10^3$
This might work for you:
sed -i 's/\(-\?[0-9]\+\(\.[0-9]\+\)\? \)x\( -\?[0-9]\+\^-\?[0-9]\+\(\.[0-9]\+\)\?\)\|\(-\?[0-9]\+\^-\?[0-9]\+\)/$\1\\cdot\3\5$/g;s/\$\\cdot/$/g' file
although the GNU sed -r switch makes it look a lot less cluttered:
sed -ri 's/(-?[0-9]+(\.[0-9]+)? )x( -?[0-9]+\^-?[0-9]+(\.[0-9]+)?)|(-?[0-9]+\^-?[0-9]+)/$\1\\cdot\3\5$/g;s/\$\\cdot/$/g' file