Bash file 1 and file then add field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash file 1 and file then add field
# 1  
Old 11-19-2013
Bash file 1 and file then add field

Hi All,

Seeking for your assistance regarding on how to bash file 1 and file 2 then add the field if the bash is true.

Ex.

nik1.csv - input file 1
Code:
1,0054,0001,4,5,6,7,May 1,USA 
11,0053,0002,14,15,16,17,May 2,PHL 
40,0052,0003,43,44,45,46,May 5,GER

nik2.csv - input file 2
Code:
20,21,22,23,24,0054,0001,May 5 
30,31,32,33,34,0053,0002,May 7

Input File 2 Expected Output:
20,21,22,23,24,0054,0001,May 5,USA - Added the 8th Field(USA) because the the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1.
30,31,32,33,34,0053,0002,May 7, PHL - Added the 8th Field(PHL) because the the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1.

If the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1, i'm going to add the 8th field of input file 1 into input file 2.

I don't know how to code it. T_T

Please advise,

Thanks,
-nik

---------- Post updated at 11:52 AM ---------- Previous update was at 11:03 AM ----------

Seeking on assistance pls..

Thanks,
-nik

Last edited by Franklin52; 11-20-2013 at 03:01 AM.. Reason: Please use code tags
# 2  
Old 11-19-2013
Try :

Code:
$ awk -F, 'FNR==NR{A[$6]=$0;next}($6 in A){$0=A[$6] FS $8}A[$6]'  inputfile2 inputfile1
20,21,22,23,24,0054,May 5,USA
30,31,32,33,34,0053,May 7 ,PHL

OR
Code:
$ awk -F, 'FNR==NR{A[$6]=$8;next}($6 in A){$0=$0 FS A[$6]}A[$6]'  inputfile1 inputfile2
20,21,22,23,24,0054,May 5,USA
30,31,32,33,34,0053,May 7 ,PHL


Last edited by Akshay Hegde; 11-20-2013 at 12:09 AM.. Reason: modification
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 11-20-2013
okay Sir Akshay Hegde i will try and keep you posted.

Thanks,
-nik

---------- Post updated at 12:24 PM ---------- Previous update was at 12:20 PM ----------

Hi Sir Akshay,

May i know where in this code is the 6th field of file 1?
Code:
$ awk -F, 'FNR==NR{A[$6]=$0;next}($6 in A){$0=A[$6] FS $8}A[$6]'  inputfile2 inputfile1

Code:
$ awk -F, 'FNR==NR{A[$6]=$8;next}($6 in A){$0=$0 FS A[$6]}A[$6]'  inputfile1 inputfile2

Please advise,

Thanks,
-nik

Last edited by Franklin52; 11-20-2013 at 03:02 AM.. Reason: Please use code tags
# 4  
Old 11-20-2013
Quote:
Originally Posted by nikki1200
okay Sir Akshay Hegde i will try and keep you posted.

Thanks,
-nik

---------- Post updated at 12:24 PM ---------- Previous update was at 12:20 PM ----------

Hi Sir Akshay,

May i know where in this code is the 6th field of file 1?

$ awk -F, 'FNR==NR{A[$6]=$0;next}($6 in A){$0=A[$6] FS $8}A[$6]' inputfile2 inputfile1

$ awk -F, 'FNR==NR{A[$6]=$8;next}($6 in A){$0=$0 FS A[$6]}A[$6]' inputfile1 inputfile2

Please advise,

Thanks,
-nik
Red color represents 6th column of inputfile1
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 11-20-2013
Thank you very much Sir Akshay Hegde.

How about If the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1, i'm going to add the 8th field of input file 1 into input file 2.

Please see edited scenario above.

Thanks,
-nik
# 6  
Old 11-20-2013
Code:
awk -F"," 'NR==FNR{a[$2"|"$3]=$9; next}
a[$6"|"$7]{ print $0 FS a[$6"|"$7]}' file1 file2

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 11-20-2013
Quote:
Originally Posted by nikki1200
Thank you very much Sir Akshay Hegde.

How about If the 6th and 7th field of input file 2 is equal to 2nd and 3rd field of input file 1, i'm going to add the 8th field of input file 1 into input file 2.

Please see edited scenario above.

Thanks,
-nik
Code:
awk ' FNR==NR{A[$2 FS $3]=$8;next}($6 FS $7 in A){$0 = $0 FS A[$6 FS $7]}A[$6 FS $8]' file1 file2

FNR==NR{A[$2 FS $3]=$8;next}
Array A with index $2 FS $3 fields of file1 holds $8 field of file1


($6 FS $7 in A){$0 = $0 FS A[$6 FS $7]}
if $6 FS $7 fields (index) found in Array A then line = line + field Separator + $8 field of file1 which is in Array A with index $6 FS $7 of file2

...}A[$6 FS $7] ' file1 fil2

if A[$6 FS $7] is not empty then true and it prints line
if A[$6 FS $7] is empty then it false it will not print

You might aware
1 --> true
0 --> false
This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

2. Shell Programming and Scripting

Add tab after digit in specific field in file

I am trying to add a tab after the last digit in $3 in the input. The grep below is all I can think off. Thank you :) sed -n 's/:/&/p' input input chr1 955542 955763AGRN-6|gc=75 chr1 957570 957852AGRN-7|gc=61.2 chr1 976034 976270AGRN-9|gc=74.5 desired output chr1... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. UNIX for Dummies Questions & Answers

Find and add a field for an input file

Hello Sir/Madam, I'm new to UNIX and wondering if I can process a find & add using AWK command in UNIX? requirements: I have to find *.JPG field for each line in the input file and add it as a separate field for each line.. for ex: 0000684631,E,3088017,7... (10 Replies)
Discussion started by: V1l1h1
10 Replies

4. UNIX for Dummies Questions & Answers

add new 'date field' in a pipe delimited file

i need to add a new field in a pipe delimited line. the field will be the current date today. aa|a|s|w|1 as|oiy|oiy|oiy|2 given that all lines are uniformed in the number of fields i want it to look like this:\ aa|a|s|w|1|20120126 as|oiy|oiy|oiy|2|20120126 please help :) (3 Replies)
Discussion started by: kokoro
3 Replies

5. Shell Programming and Scripting

how I can add a constant to a field without changing the file format

Hi, I need to edit a file Protein Data Bank (pdb) and then open that file with the program VMD but when I edit the file with awk, it changes pdb format and the VMD program can not read it. I need to subtract 34 to field 6 ($ 6). this is a pdb file : ATOM 918 N GLY B 103 -11.855 8.675... (8 Replies)
Discussion started by: bio_
8 Replies

6. Shell Programming and Scripting

How to add text to a field within a csv file

I have a csv file which has three columns mem no. name surname 1234 John Smith 12345 John Doe I want to change the mem no. to add TF to the mem no. field i.e. mem no. name surname 1234TF John Smith 12345TF John Doe How do you do this for all records in the file? (3 Replies)
Discussion started by: Pablo_beezo
3 Replies

7. Shell Programming and Scripting

Add a field seperator in a file.

"355"|""|"NJ"|"A0A 1W0"|"V"|""|""|""|"N" I've the above sample data seperated with pipe delimeter and in the file I want to replace a space with "|" to the 4th field so the result would be like below. So it would change from 9 fields to 10 fields. "355"|""|"NJ"|"A0A"|"1W0"|"V"|""|""|""|"N" ... (3 Replies)
Discussion started by: rudoraj
3 Replies

8. Shell Programming and Scripting

DECODE file field is required in Bash

Dear All, I want to decode the one of the file field. Input file: 9393939393|999|2009-02-20 00:00:01|2||4587|2007-02-28 00:00:01|0 9393939393|2001|2009-02-20 00:00:01|2||4587|2007-02-28 00:00:01|0 9393939393|1500|2009-02-20 00:00:01|2||4587| 2007-02-28 00:00:01|0... (1 Reply)
Discussion started by: hanu_oracle
1 Replies

9. Shell Programming and Scripting

add lines automatically based on a field on another file

hello I have a number of lines that need to be added at the end of a file each time I add a field in another file (let's name it file2) file2 has this format: filed1:field2:path1:path2:path3:path... Whenever I add a path field, I should add to file1 these lines: <Location path1>... (0 Replies)
Discussion started by: melanie_pfefer
0 Replies

10. Shell Programming and Scripting

Fetch the field from file and add to new file.

Admins Pls. assist me to update a new field having 40 rows to a file. Test1.output LV Space %free Space_used_04_15 dev 500 7 67% dev1 800 7 75% dev2 800 7 75% dev3 900 7 ... (2 Replies)
Discussion started by: maestrohero
2 Replies
Login or Register to Ask a Question