Adding new field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding new field
# 8  
Old 08-16-2010
Quote:
Originally Posted by ahmad.diab
Here you go :-

Code:
nawk '
(FILENAME=="server3.txt") { a[$4]=FILENAME ; next}
(FILENAME=="server2.txt") { a[$4]=FILENAME ; next}
(FILENAME=="server1.txt") { a[$4]=FILENAME ; next}
( $3 in a ) {print $0,"\t",a[$3] }
' server3.txt server2.txt server1.txt mainfile.txt

SmilieSmilieSmilie
Code:
awk '
(FILENAME!="mainfile.txt") { a[$4]=FILENAME ; next}
( $3 in a ) {print $0,"\t",a[$3] }
' server3.txt server2.txt server1.txt mainfile.txt

Looks shorter. SmilieSmilieSmilie
# 9  
Old 08-16-2010
Quote:
Originally Posted by pravin27
ahmad,

can you please explain the code ?
The programme reads each input file, in order, that is listed on the command line following the programme; server3.txt, server2.txt, server1.txt, and mainfile.txt. The variable FILENAME is set by awk to the name of the file that is currently being processed.

For each record, when the filename is server?.txt, it saves the current filename into the associative array 'a' using the IP address (the contents of the fourth field or $4) as the index. The 'next' statement causes awk to stop processing the current record, read the next record and start processing that record from the top of the programme.

Code:
(FILENAME=="server3.txt") { a[$4]=FILENAME ; next}
(FILENAME=="server2.txt") { a[$4]=FILENAME ; next}
(FILENAME=="server1.txt") { a[$4]=FILENAME ; next}

When the input record does not come from server?.txt, none of the statements with 'next' are matched and the last line of the programme is executed. The third field, the IP address, is used as an index into the array 'a' and if the array contains a non-null value, it prints the input record from the main file and the contents of a[$3] which is the name of the last server?.txt file that the address was seen in.

Code:
( $3 in a ) {print $0,"\t",a[$3] }

It could have also been written to be more clear to a beginner:
Code:
{ 
   if( a[$3] != "" )
      print $0 " \t" a[$3];
}

Quote:
Originally Posted by rdcwayx
Code:
awk '
(FILENAME!="mainfile.txt") { a[$4]=FILENAME ; next}
( $3 in a ) {print $0,"\t",a[$3] }
' server3.txt server2.txt server1.txt mainfile.txt

Looks shorter. SmilieSmilieSmilie
Yes, it looks shorter, but more importantly it allows a variable number of server*.txt files to be processed without requiring the code to change.

It might also be nice to write the line
Code:
(FILENAME!="mainfile.txt") { a[$4]=FILENAME ; next}

as
Code:
(FILENAME!="mainfile.txt") { a[$4] = a[$4] FILENAME " "; next}

which allows for the ip address to exist in multiple server*.txt files and to have each of those names presented rather than the name of the last file that the address was noticed in.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding field to file

Hi I have file as given below 040|14300|40.0|563000 042|13200000|40.0 041|100|40.0 043|10000|40.0 045|102|40.0 I want to check if field 4 is null then I have to place | in position 4. result set should be 040|14300|40.0|563000 042|13200000|40.0| 041|100|40.0| 043|10000|40.0|... (9 Replies)
Discussion started by: shabeena
9 Replies

2. Shell Programming and Scripting

Field extract w/o - and adding formats

I have a txt file like below: Accession Orderable Unique ID Subject ID Timepoint Colletion Date/Time 12-170-0185 Glucose 1756487 1 D-07_H00.03 18-JUN-2012 06:03:00 12-170-0185 Insulin 1756487 1 D-07_H00.03 18-JUN-2012 06:03:00 12-170-0200 Glucose 1756487 1 D-07_H00.05... (9 Replies)
Discussion started by: Daniel Gate
9 Replies

3. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

4. Shell Programming and Scripting

Adding matching field

I have a flat file test.log red,5,,,,, green,7,,,,, blue,4,,,,, red,8,,,,, green,9,,,,, How i get a a result: blue,4,,,,, green,16,,,,, red,13,,,,, Thanks Video tutorial on how to use code tags in The UNIX and Linux Forums. (2 Replies)
Discussion started by: sabercats
2 Replies

5. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

6. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

7. Shell Programming and Scripting

adding another field to SED output

Dear experts, I have a file called "check" with contents like below i used the sed command like below to get the value of "success" and "failed" only My question is how can i get the value to include the time "03:15", so that i can get a value such as below : - Appreciate... (4 Replies)
Discussion started by: aismann
4 Replies

8. Shell Programming and Scripting

Problem adding into an array field!!!

Hi, Kindly assist by analyzing the code below and suggest changes to achieve the required output. The input file: 01-010241800000 35000 MV010 02/03/09 0306 03060226 03 02-004103300000 470000 MV010 02/03/09 0301 03010276 03 The objective is to convert field No4. from dd/mm/yy to yyyymmdd... (5 Replies)
Discussion started by: talk2pawee
5 Replies

9. UNIX for Dummies Questions & Answers

Adding a new field using sed or awk?

I have a bcp file that contains 10 fields. These fields are separated by a tab. How can I add my name as a new field in the 8th position for every record? I've been playing w/ sed and awk but can't seem to figure this out. (3 Replies)
Discussion started by: sasabune
3 Replies

10. Shell Programming and Scripting

Adding field of flatfile by an index.

Hey guys, I was given a task that involved parcing a large file that looked somthing like this... A1-0999999,SMITH,.25 A1-0999999,JOHN,.75 A1-0999999,HELMET,.1.25 A1-0999999,HOOP,.10.25 D1-1212121,SMITH,4.00 D1-1212121,TH,9.00 D1-1212121,MITCH,10.20 D1-1212121,RETAL,3.00 A1-9909555,,3.00... (2 Replies)
Discussion started by: djsal
2 Replies
Login or Register to Ask a Question