awk csv using a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk csv using a loop
# 1  
Old 04-03-2013
awk csv using a loop

I have a two files. One is a list called Good_System and the other is a csv called Many_System. I want to do a for loop to go through each
Good_System line and where the two fields match the line in Many_System , edit that line by inserting "Good" into the first field.

If no match, then just leave the original line in Many_System. How do I go through the loop without creating duplicate lines in Many_System when there's no match ?
Thanks,
wbrunc

Good_System

SysA,10.123.456.789
SysD,10.123.456.787
SysE,10.123.456.786

Many_System

,SysA,10.123.456.789
,SysB,10.123.456.785
,SysD,10.123.456.787
Good,SysD,10.123.456.787
,SysE,10.123.456.786

Desired Many_System file. Edits in Italic.

Good
,SysA,10.123.456.789
,SysB,10.123.456.785
,SysC,10.123.456.788
Good,SysD,10.123.456.787
Good,SysD,10.123.456.787 - no change to this line
Good,SysE,10.123.456.786
# 2  
Old 04-03-2013
Does it have to be a for loop? With awk, try:
Code:
$ awk 'FNR==NR {GOOD[","$0]="Good"; next} {print GOOD[$0] $0}' file1 file2
Good,SysA,10.123.456.789
,SysB,10.123.456.785
Good,SysD,10.123.456.787
Good,SysD,10.123.456.787
Good,SysE,10.123.456.786

There's no SysC line in your input, so I'm surprised there's one in your output, and there is a duplicate for SysD in your output even though you insisted on NO duplicates...
# 3  
Old 04-03-2013
To clarify. Many_System is the base file that I'm editing. The original lines in it should remain unless they need to be edited because of a match from a Good_System line. Therefore, the SysC line that you mention is part of that and should remain.

I'm only editing if Good_Systems fields match Many_System fields 2 and 3 AND Many_System field 1 is blank.
So the first SysD line in Many_System should be edited but the next SysD
line should be left alone. When I said no duplicates, I meant that I don't want
lines created in Many_System only editing of existing lines .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk on csv files

wk on csv files Hi I have two csv files 1 ) keys.csv with data as below XX1,offsite XX2,offsite XX8,none XX3,offsite XX4,onsite XX7,none XX5,onsite XX6,onsite XX15,none 2) source.csv file with data like 1,0123,ppp,XX1 2,0122,sss,XX2 3,1239,yyy,XX8 4,567,kkk,XX5 (2 Replies)
Discussion started by: zozoo
2 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

awk read column csv and search in other csv

hi, someone to know how can i read a specific column of csv file and search the value in other csv columns if exist the value in the second csv copy entire row with all field in a new csv file. i suppose that its possible using awk but i m not expertise thanks in advance (8 Replies)
Discussion started by: giankan
8 Replies

5. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

6. Shell Programming and Scripting

Sorting a .csv using awk or other

Hello all, I am new here and *relatively* new to Unix. I have a bit of an emergency. I have a three column file that I need to sort: sample name, miRNA, reads per million (RPM) There are multiple samples, and for each sample name there are multiple miRNAs and associated RPMs. Some of these... (6 Replies)
Discussion started by: dunnybocter
6 Replies

7. Shell Programming and Scripting

Merge 2 csv files with awk

I have 2 files pipe delimted and want to merge them based on a key e.g file 1 123$aaa$yyy$zzz 345$xab$yzy$zyz 456$sss$ttt$foo 799$aaa$ggg$dee file 2 123$hhh 345$ddd 456$xxx 888$zzz so if the key is the first field, and the result should be the common key between file 1 and 2 (6 Replies)
Discussion started by: loloAix
6 Replies

8. Shell Programming and Scripting

Help needed: Adding columns in csv file in loop

Hi Everyone: My shell script creates multiple csv files (~30) in for loop. I want to compile (or merge) 3rd column from each (all) of these files to another file (in loop). Please help. Thanks. (3 Replies)
Discussion started by: smap007
3 Replies

9. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

10. Shell Programming and Scripting

Work CSV file with awk loop

I need to converts a CSV file from Format "A" into Format "B", where the first field is repeated in a loop with a counter. I'am quite sure that this is with AWK possible burt the "Forum Search Function" doesn't work at the moment. Format "A" A1;B1 ;B2 ;B3 A2;C1 ;C2 ;C3 ... (2 Replies)
Discussion started by: frieling
2 Replies
Login or Register to Ask a Question