AWK swapping fields on different lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK swapping fields on different lines
# 1  
Old 04-16-2012
AWK swapping fields on different lines

Hi All,

Sorry if this question has been posted elsewhere, but I'm hoping someone can help me! Bit of an AWK newbie here, but I'm learning (slowly!)

I'm trying to cobble a script together that will save me time (is there any other kind?), to swap two fields (one containing whitespace), with each field on different lines

As an arbitrary example (I'll explain as best as I can!):


Code:
ENTRANT LIST
 
Name         Entry Type    Fastest Time
--------------------------------------------
Andrew         Cyclist          10:59        #(newline here)
Cyclist Mr. Smith has a great track record....

******************************
Chris             Runner          45:12
Runner Mr. Jones has been an avid runner for....
******************************

Now my problem is this: I want to Switch $1 of the first line ("Andrew") with $2 of the second line ("Mr. Smith" [note whitespace]) to give me:

Code:
ENTRANT LIST
 
Name         Entry Type    Fastest Time
--------------------------------------------
Mr. Smith         Cyclist          10:59        #(newline here)
Cyclist Andrew has a great track record....

******************************



Im assuming I'll need to use the *s as record separator and operate from there, but I'm coming up short on:

A) How to read two lines at once - presumably I will need a third area in memory to store one of the fields while the other is moved- should I create a temp file for this?

B) Avoiding having 'Mr. Smith' separated into two fields.

I'm trying to avoid creating two arrays if possible, but I can see this being the only way - does anybody have any suggestions about how I could approach this another way?

Sorry for all the questions - hopefully someone can help me out!
Moderator's Comments:
Mod Comment How to use code tags

Last edited by Scrutinizer; 04-16-2012 at 08:43 AM.. Reason: code tags
# 2  
Old 04-16-2012
Code:
 
$ nawk '{a=$1;b=$0;getline;c=$2" "$3;sub(c,a);sub(a,c,b);printf("%s\n%s",b,$0)}' test.txt
Mr. Smith Cyclist 10:59 #(newline here)
Cyclist Andrew has a great track record....
 
$ cat test.txt 
Andrew Cyclist 10:59 #(newline here)
Cyclist Mr. Smith has a great track record....

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 04-16-2012
And there it is, in black and white.

Thanks for such a quick and elegant solution - my thought process was way off!

Thanks itkamaraj!
# 4  
Old 04-16-2012
And for something a little cryptic.. Smilie
Code:
sed '/^[-*]/{N;N;s/\(\n\)\([^ \t]*\)\(.*\n[^ \t]*[ \t]*\)\([^ \t]*[ \t]*[^ \t]*\)/\1\4\3\2/;}' infile

Code:
sed '/^[-*]/{N;N;s/\(\n\)\([^ ]*\)\(.*\n[^ ]* *\)\([^ ]* *[^ ]*\)/\1\4\3\2/;}' infile

Code:
sed -E '/^[-*]/{N;N;s/(\n)([^ ]*)(.*\n[^ ]* *)([^ ]* *[^ ]*)/\1\4\3\2/;}' infile


Last edited by Scrutinizer; 04-16-2012 at 09:09 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-16-2012
Code:
sed '/^[-*]/{N;N;s/\(\n\)\([^ \t]*\)\(.*\n[^ \t]*[ \t]*\)\([^ \t]*[ \t]*[^ \t]*\)/\1\4\3\2/;}' infile

Catchy, and easy to remember Smilie

Thanks for that, Scrutinizer (and for sorting out the tags in my original post).

Working my way through the O'reilly Sed and Awk book at the moment - Aside from applying the knowledge gained to anything and everything I can, does anyone have any recommendations for resources following on from this?

Sorry for straying off-topic a little!

Last edited by Bravestarr; 04-16-2012 at 09:50 AM..
# 6  
Old 04-16-2012
If you want to try perl:
Code:
perl -lane 'if ( /^\s*(\w+)\s+\w+\s+(\d\d:\d\d).*/) {$L1=$_;$name=$1;next}
            if ($L1 && $name ne "" ){
                  s/^\s*(\w+\s)(.*)(\s+has.*)/$1$name$3/;$surn=$2;
                  $L1 =~ s/^(\s*)(\w+)(\s+.*)/$1$surn$3/;
                  print $L1."\n".  $_;undef $L1;undef $name;
             }else {print $_}' infile

Code:
ENTRANT LIST
 
Name         Entry Type    Fastest Time
--------------------------------------------
Mr. Smith         Cyclist          10:59        #(newline here)
Cyclist Andrew has a great track record....

******************************
Mr. Jones             Runner          45:12
Runner Chris has been an avid runner for....
******************************

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Swapping/replacing fields

Hallo Team, I would like to replace filed 4 and 7 with filed 39 how can i achieve this ? -bash-3.2$ cat dip1.csv| cut -f4,7,24,36,39 -d","|sort -u +27113996891,+27113996891,196.35.130.52,828854047,+27873500077 +27116452690,+27825702918,10.0.109.13:5060,+27116452690,+27116452690... (2 Replies)
Discussion started by: kekanap
2 Replies

3. Shell Programming and Scripting

Swapping fields

Hallo Team, This is the command that i am running : grep ",Call Forward Not Reachable" *2013* this is the output that i am getting (i did a head -10 but the files can be more than 1000) ... (8 Replies)
Discussion started by: kekanap
8 Replies

4. Shell Programming and Scripting

select lines with certain values on certain fields with awk

I need a awk command to select from a log-file only the lines that have on the 2nd field (considering "|" separator) one of the values 10.216.22.XX or 10.216.22.YY or 10.216.22.ZZ and on the 4th field only values that contain strictly digits. I want the command to work parsing the file only once (I... (2 Replies)
Discussion started by: black_fender
2 Replies

5. Shell Programming and Scripting

Swapping of fields in file

Hi Friends , I have file1.txt 1|b|46|123|47673|348738 2|c|63|124|7346|4783 3|y|45|125|5555|78789 output should swap the 4th field to the first field. output 123|1|b|46|47673|348738 124|2|c|63|7346|4783 125|3|y|45|5555|78789 (3 Replies)
Discussion started by: i150371485
3 Replies

6. Shell Programming and Scripting

awk - use fields from subsequent lines

I've run into a problem getting exactly what I want out of awk - some folks may recognize this as an output from Amazon's ec2-describe-instances: Given the following: INSTANCE i-4960f321 BLOCKDEVICE Line2Var2 TAG instance i-4960f321 Name web1 TAG instance i-4960f321... (2 Replies)
Discussion started by: colinjohnson
2 Replies

7. Homework & Coursework Questions

Swapping Fields with Sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The assignment is to convert a text table to csv format. I've got the cleaning up done, but I need to swap two... (0 Replies)
Discussion started by: VoiceInADesert
0 Replies

8. Shell Programming and Scripting

How to (n)awk lines of CSV with certain number of fields?

I have a CSV file with a variable number of fields per record. How do I print lines of a certain number of fields only? Several permutations of the following (including the use of escape characters) have failed to retrieve the line I'm after (1,2,3,4)... $ cat myfile 1,2,3,4 1,2,3 $ # Print... (1 Reply)
Discussion started by: cs03dmj
1 Replies

9. Shell Programming and Scripting

Swapping of fields in a file

Hi I have afile with 15fields,say f1,f2....f15 delimited on comma. How can i swap the f1,f15 fields using unix shell commands or any script? Thanks (3 Replies)
Discussion started by: ksailesh
3 Replies

10. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies
Login or Register to Ask a Question