cmccabe
September 25, 2016, 12:02pm
1
The output of an awk
script is the below file
. Line 1,3 that starts with the Ion...
need to be under line 2,4 that starts with R_
. The awk
runs but no output results. Thank you :).
file
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV40
R_2016_09_20_12_47_36_user_S5-00580-8-Medexome
desired output
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-8-Medexome
IonXpress_007 MEV40
awk
awk 'BEGIN{split("R_",n)} {a[NR]=$0} END{for(i=1;i in n;i++) print a[n]}' file
Hello cmccabe,
Could you please try following and let me know if this helps you.
awk '/^Ion/{A=$0;next} /^R_/{print $0 ORS A;A=""}' Input_fie
Output will be as follows.
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-8-Medexome
IonXpress_007 MEV40
Thanks,
R. Singh
1 Like
Hi,
If input file contents are same as in post#1, please try the following one:
awk '/IonXpress_007/{x=$0;getline;print $0"\n"x}' file
you need to consider RavinderSingh13 solution, if you want to check if line starts with R_ for example.
Adding sed solution:
sed -ne '/IonXpress_007/{x;n;/^R_/{p};x;p}' file
cat file
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV40
R_2016_09_20_12_47_36_user_S5-00580-8-Medexome
output is:
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-8-Medexome
IonXpress_007 MEV40
1 Like
Hello cmccabe,
If you want to tackle this problem with an array(where our Input_file in which line starts from Ion
comes on each odd line of Input_file and R_
comes into each even line of Input_file), following may help you in same too.
awk '{A[FNR]=$0} END{for(i=1;i<=FNR;i+=2){if(A[i+1]){print A[i+1]};if(A){print A}}}' Input_file
Output will be as follows.
R_2016_09_20_12_47_36_user_S5-00580-7-Medexome
IonXpress_007 MEV37
R_2016_09_20_12_47_36_user_S5-00580-8-Medexome
IonXpress_007 MEV40
Thanks,
R. Singh
1 Like
cmccabe
September 25, 2016, 1:17pm
5
Thank you both very much:).
If you just want to switch the order of pairs of lines in file using awk
, you could also try:
awk 'NR%2{s=$0;next}{print $0 ORS s}' file
As always, if someone wants to try this on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
or nawk
.
1 Like