Awk: output lines with common field to separate files


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Awk: output lines with common field to separate files
# 1  
Old 09-14-2018
Awk: output lines with common field to separate files

Hi,

A beginner one.

my input.tab (tab-separated):
Code:
h1	h2	h3	h4	h5
item1	grpA	2	3	customer1
item2	grpB	4	6	customer1
item3	grpA	5	9	customer1
item4	grpA	0	0	customer2
item5	grpA	9	1	customer2

objective:
output a file for each customer ($5) with the item number ($1) only if $2 matches 'grpA'.

outputs:
in 'customer1.tab'
Code:
item1
item3

in 'customer2.tab'
Code:
item4
item5

my command:
Code:
gawk '
BEGIN{
   FS=OFS="\t"
}
NR>1{
   if($2 ~ /grpA/){
      a[$5]=$1
   }
}
END{
   for(i in a){
      print i FS a[i] >> i".tab"
   }
}' input.tab

What I get is only the last occurrence of the array value for each customer, even though I loop over the array:
in 'customer1.tab'
Code:
item3

in 'customer2.tab'
Code:
item5


There is clearly a problem to populate the array. I don't get it Smilie
# 2  
Old 09-14-2018
You are overwriting the respective array elements with every occurrence of the respective customer. Try
Code:
awk -F"\t" '
NR == 1         {next
                }

$2 ~ /grpA/     {a[$5]  = a[$5] DL[$5] $1
                 DL[$5] = ORS
                }

END             {for (i in a)   print a[i] > (i ".tab")
                }
' file
cf cu*

---------- customer1.tab: ----------

item1
item3

---------- customer2.tab: ----------

item4
item5

As you don't need OFS, the field separator is set with the -F option.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-14-2018
Works great ! Thanks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create separate files but not include specific field in output

I am trying to use awk to create (in this example) 3 seperate text file from the unique id in $1 in file, if it starts with the pattern aa. The contents of each row is used to populate each text file except for $1 which is not needed. It seems I am close but not quite get there. Thank you :). ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Finding out the common lines in two files using 4 fields with the help of awk and UNIX

Dear All, I have 2 files. If field 1, 2, 4 and 5 matches in both file1 and file2, I want to print the whole line of file1 and file2 one after another in my output file. File1: sc2/80 20 . A T 86 F=5;U=4 sc2/60 55 . G T ... (1 Reply)
Discussion started by: NamS
1 Replies

3. UNIX for Dummies Questions & Answers

Values with common field in same line with awk

Hi all ! I almost did it but got a small problem. input: cars red cars blue cars green truck black Wanted: cars red-blue-green truck black Attempt: gawk 'BEGIN{FS="\t"}{a = a (a?"-":"")$2; $2=a; print $1 FS $2}' input But I also got the intermediate records... (2 Replies)
Discussion started by: beca123456
2 Replies

4. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

5. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies

6. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

7. Shell Programming and Scripting

Concatenating lines of separate files using awk or sed

For example: File 1: abc def ghi jkl mno pqr File 2: stu vwx yza bcd efg hij klm nop qrs I want the reult to be: abc def ghistu vwx yza jkl mno pqrbcd efg hij klm nop qrs (4 Replies)
Discussion started by: tamahomekarasu
4 Replies

8. Shell Programming and Scripting

awk command to separate a field

I have a log file that I am trying to convert. File contents something like this: aaaaa bbbbbb cccc dddddd\123 eeeee ffffffff I am trying to output the fields in a different order and separate field 4 so that the "123" and "dddddd" can be output separately. for example bbbbbb aaaaa 123... (5 Replies)
Discussion started by: jake1988
5 Replies

9. Shell Programming and Scripting

How to append two files with common field.

I have two files like File1 : will get this file from "who" command. It is a unix file. user val1 Jul 29 13:15 (IP Address1) user val3 Jul 30 03:21 (IP Address2) user val2 Jul 29 13:16 (IP Address3) user val4 Jul 29 13:17 (IP Address4) ... (4 Replies)
Discussion started by: manneni prakash
4 Replies

10. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies
Login or Register to Ask a Question