Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating record lines using awk

Status
Not open for further replies.

HIB049

Technical User
Jan 31, 2003
19
GB
Hi,

Please i am trying to perform the following using awk, and i havent got a clue on where to start, can someone help or direct me to a useful resource.

My file is shown below

0.00 75.72 0
135.76 206.40 0
819.12 1077.72 0
1744.60 2003.72 0
2609.12 2868.72 0
3374.44 3715.60 0

want to manipulate it to the below

0.00 206.40 0
819.12 1077.72 0
1744.60 2003.72 0
2609.12 2868.72 0
3374.44 5000.00 0

As you can see line 2 has been removed, but the second record of line 2 has been taken to the 2nd record of line one. On line 5, the 2nd record would be a fixed number i.e. 5000.00.

Thanks for your assistance on this matter.
 
Here's a starting point:

Code:
awk '
    NR==1 { t=$1; next }
    NR==2 { print t,$2,$3; next }
    ...
' inputfile

As you can see you can do special handling of each line by comparing to the NR variable. I'm sure you can figure out the rest!

Annihilannic.
 
Annihilannic,

Thks for the tip above, but i cant seem to usr NR==2 before NR==1. It seems like i have to use the NR sequentially. Below is what i have come up with so far, but doesnt seem to work

awk '
NR==2 { t=$1; next }
NR==1 { print $1,t,$3; next }
NR==3 { print $1,$2,$3; next }
...
NR==6 { print $1,5000,$3; next }
' inputfile
 
Hi Guys,

After sleepless night, came up with the below solution, might not be elegant, but works......please let me know if there is a better solution than the below.

Many Thanks for all you help on this one.

Cheers
Ed

=======================================================
Code
=======================================================
awk '
{
a[NR]=$1; b[NR]=$2; c[NR]=$3;
}
END {
for (j=1;j<=NR;j++) {
if ( j == 1 ) {
k=2
print a[j]"\t"b[k]"\t"c[j]
} else if ( j == 2 )
continue
else if ( j == NR )
print a[j]"\t"5000"\t"c[j]
else
print a[j]"\t"b[j]"\t"c[j]
}
}
'
 
The rest of my solution was:

Code:
awk '
    NR==1 { t=$1; next }
    NR==2 { print t,$2,$3; next }
    NR==6 { print $1,5000, $3; next }
    NR>2
'

The order you place your code chunks is not important (except for the logic) because the input file will always be processed in order. For every input line awk tests the condition and executes the following code block if it evaluates to "true". If no code block follows the condition the default action is to print the line.

Annihilannic.
 
Thks Annihilannic, for your time on this.....only if i was really able to understand how awk does its processing....i kno w there are lots of books and websites for awk tutorial, but is there any resource that you can point me too, for a better understanding.

Thks once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top