awk to reorder lines in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to reorder lines in file
# 1  
Old 09-25-2016
awk to reorder lines in file

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 Smilie.

file
Code:
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
Code:
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
Code:
awk 'BEGIN{split("R_",n)} {a[NR]=$0} END{for(i=1;i in n;i++) print a[n[i]]}' file

# 2  
Old 09-25-2016
Hello cmccabe,

Could you please try following and let me know if this helps you.
Code:
awk '/^Ion/{A=$0;next} /^R_/{print $0 ORS A;A=""}'  Input_fie

Output will be as follows.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-25-2016
Hi,

If input file contents are same as in post#1, please try the following one:
Code:
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:
Code:
sed -ne '/IonXpress_007/{x;n;/^R_/{p};x;p}' file

Code:
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:
Code:
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


Last edited by greet_sed; 09-25-2016 at 01:32 PM.. Reason: Adding sed solution
This User Gave Thanks to greet_sed For This Post:
# 4  
Old 09-25-2016
Quote:
Originally Posted by cmccabe
Code:
awk 'BEGIN{split("R_",n)} {a[NR]=$0} END{for(i=1;i in n;i++) print a[n[i]]}' file

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.
Code:
awk '{A[FNR]=$0} END{for(i=1;i<=FNR;i+=2){if(A[i+1]){print A[i+1]};if(A[i]){print A[i]}}}'   Input_file

Output will be as follows.
Code:
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

Last edited by RavinderSingh13; 09-25-2016 at 02:39 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 09-25-2016
Thank you both very muchSmilie.

Last edited by cmccabe; 09-25-2016 at 02:19 PM.. Reason: added additional thanks
# 6  
Old 09-25-2016
If you just want to switch the order of pairs of lines in file using awk, you could also try:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 11-23-2016
Thank you all Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reorder of fields

I need to reorder the fields of an input file. I was using the following awk statement but I am stumped on how to get it exactly as I need. I need the output to be tab delimited except for the CITY, NAME and ID. Those last set of columns should be a single field separated by a space. And they... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

2. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

3. Shell Programming and Scripting

awk last n lines of file

Just my second week working on awk I need a hint for the following tasks. I want to limit my logfile from the very outset to 200 lines. All I do until now is head -c 10K >> /home/uplog.txt | awk 'END{print NR " swap " NF$5; exit}' /home/uplog.txt; After being read it shall print the very... (27 Replies)
Discussion started by: 1in10
27 Replies

4. UNIX for Dummies Questions & Answers

Reorder column in Unix

I have a flat file with the 3 columns and separate by tab delimiter, and the last column with special character. A B C D F G Now I would like to swap the third column with second column to following format: A B C D F G I know this cannot be done in Unix command with using cut... (3 Replies)
Discussion started by: newbie2011
3 Replies

5. Shell Programming and Scripting

Reorder and insert column using awk

Hi friends, I am beginner in shell scripting. I am trying to modify (Reorder and insert column) a BIG .txt file using awk to create a .bim file. My start file has 25 columns But I need to make a new file with only 5 columns from the start file and insert a new column in position 3 with value... (5 Replies)
Discussion started by: smitra
5 Replies

6. Shell Programming and Scripting

Reorder the Cut characters

Hi, I have a fixed width flatfile, I want to view this file specific to it's character position and in order I want to...example as below ABCDE.txt 01COLTSMANNING18 02PATS BRADY 12 03PACKSROGERS 12I used unix cut command to see specific field based on length but unable to order them as... (6 Replies)
Discussion started by: okkadu
6 Replies

7. Shell Programming and Scripting

reading comma separated data and reorder

hey guys! i need to read data from a file that are comma separated then reorder them in another file to be generated for example: x,y,z a,b,c l,m,n o,p,q and transform this into: x,a,l,o y,b,m,p z,c,n,q Will appreciate your fast reply Regards! (5 Replies)
Discussion started by: maiooi90
5 Replies

8. Shell Programming and Scripting

Bash script to reorder csv

hi guys, im fairly new to unix and bash scripts and therefore your help would really be appreciated. i need to write a bash script that will take a csv file, and reorder the data and output to another csv file. The source csv file will look something like this: HEAD,671061,Add,SS... (3 Replies)
Discussion started by: daz_20
3 Replies

9. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

10. UNIX for Advanced & Expert Users

Help with splitting lines in a file using awk

I have a file which is one big long line of text about 10Kb long. Can someone provide a way using awk to introduce carriage returns every 40 chars in this file. Any other solutions would also be welcome. Thank you in advance. (5 Replies)
Discussion started by: martinbarretto
5 Replies
Login or Register to Ask a Question