append field to file(nawk)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append field to file(nawk)
# 1  
Old 11-10-2004
append field to file(nawk)

I have two files. File A and File B.

File A has two fields separated by comma

352020252365988, 652020100572356
546876543215667, 652065465654686
...

File B has many Fields separate by spaces

Date Name 352020252365988 Reference
Date2 Name2 546876543215667 Reference


I want to search for a match of Field 1 of file A in file 2. If there is a match print the line that contains the match, and append field 2 of File A at the end of each matched line.

so to have

Date
# 2  
Old 11-10-2004
This code works for me

Code:
#!/bin/sh

while read line
do
  lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
  field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
  match=`grep "\<$lookup\>" file_b`
  if [ "$?" -eq "0" ]; then
    echo "$match" | while read record
    do
      echo "$record $field"
    done
  fi
done < file_a

assuming your files are named file_a and file_b.

The nested while is in case you have duplicate records and more than one record matches.

You'll need to redirect the output of this script to a file obviously.

Cheers
ZB
# 3  
Old 11-10-2004
If the match is on the 3rd field of the 2nd file, then you can try this awk...
Code:
BEGIN {
   FS=","
   while(getline<xref) arr[$1]=$2
   FS=" "
}
$3 in arr {
   print $0, arr[$3] 
}

Use it like this...

awk -f above.awk -v xref=file_a file_b

Tested on the sample data...

Date Name 352020252365988 Reference 652020100572356
Date2 Name2 546876543215667 Reference 652065465654686
# 4  
Old 11-14-2004
Thanx, both these scripts have worked!

I prefer the last one since I get to name files at the command prompt.

gurus u are, u guys!

___
Axl
# 5  
Old 11-14-2004
Just FYI, if you change the code I posted thusly
Code:
#!/bin/sh

while read line
do
  lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
  field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
  match=`grep "\<$lookup\>" ${2}`
  if [ "$?" -eq "0" ]; then
    echo "$match" | while read record
    do
      echo "$record $field"
    done
  fi
done < ${1}

You can call this with ./my_script file_a file_b - i.e. specifying the files at the prompt as you wish!

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

2. Shell Programming and Scripting

Nawk One Liner to Count Field Combinations

I have text files, fields separated by commas, and there are 115 fields. I need to use nawk to look at each line, if field $4==10, then count how many times the combination of field $5 and field $11 occur in the file. I tried the following: nawk -F, '{if($4==10){tg++;cd++,tgcd++}}END{for(i in... (3 Replies)
Discussion started by: he204035
3 Replies

3. Shell Programming and Scripting

Append column using awk/nawk

Is there any way I can achieve this? Considering test1 : a 1 2 3 4 b 2 3 4 5 c 12 1232 14 1 d 10 13 4 5 e 1 5 6 9 1 And test to be some string : qw twe tew we qw I want something like this : a 1 2 qw 4 b 2 3 twe 5 (5 Replies)
Discussion started by: aksijain
5 Replies

4. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

5. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

6. Shell Programming and Scripting

awk (nawk) field separator

Hi; i have a file and i want to get; - If the last word in line 14 is NOT equal to "Set."; then print 2nd, 3rd, 4th and 5th values of 3rd line. and my code is: nawk 'NR==14 {if ($NF!="Set.") (NR==3{print $2,$3,$4,$5}) }' file.txt but no result?? :confused::(:confused::( (4 Replies)
Discussion started by: gc_sw
4 Replies

7. Shell Programming and Scripting

I need help counting the fields and field separators using Nawk

I need help counting the fields and field separators using Nawk. I have a file that has multiple lines on it and I need to read the file 1 at a time and then count the fields and field separators and then store those numbers in variables. I then need to delete the first 5 fields and the blank... (3 Replies)
Discussion started by: scrappycc
3 Replies

8. Shell Programming and Scripting

Append tabs at the end of each line in NAWK -- varying fields

Hi, I need some help in knowing how I can append tabs at the end of each line... The data looks something like this: field1, field2, field3, field4 1 2 3 4 5 I have values in field1 and field 2 in the first row and I would like to append tab on field3 and field4 for the first row..and in... (6 Replies)
Discussion started by: madhunk
6 Replies

9. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question