I have a Input.txt document which contains data fields seperated by tabs. There are 4 fields totally Named UNIQUE, ORDER, CONTACT and WINS. The UNIQUE field contains unique ID and the CONTACT field contains data seperated by comma in some records. I am looking to write an awk script which will split the CONTACT field values by comma and produce an output file with the split data and Unique ID as shown in the attachement. Any help is appreciated very much.
###########################################
UNIQUE CONTACT
G001 SP
G001 SD
G002 B
G002 DR
G002 P
G003 AF
G004 PO
G004 AC
###########################################
Next time better use CODE-tags to enhance readability (the # in the editor window when writing a post) instead of attaching a file, ty.
$> awk '/^###/ {print} {split($3,arr,","); for(e in arr){print $1"\t"arr[e]} }' infile
###########################################
UNIQUE CONTACT
G001 SP
G001 SD
G002 B
G002 DR
G002 P
G003 AF
G004 PO
G004 AC
###########################################
Many thanks, your code works perfectly. I need some more help from you. when my CONTACT field values contain spaces between words then the code produces only the 1st value and ignores the rest of the field value. For example please see the 1st record.
If my input file is in the below format
UNIQUE ORDER CONTACT WINS
G001 R01 Swr Pq,SD solution
G002 R01A B,DR,P
G003 R02 A F fix
G004 R03 PO,A C build
then i want the output to be
UNIQUE CONTACT
G001 Swr Pq
G001 SD
G002 B
G002 DR
G002 P
G003 A F
G004 PO
G004 A C
however i get the following output
UNIQUE CONTACT
G001 Swr
G002 B
G002 DR
G002 P
G003 A
G004 PO
Any idea how I can correct it, that is to allow spaces between words.
$ awk '{n=split($3,arr,",");for(i=0;i++< n;){printf "%s\t%s\n",$1,arr;}}/#/' file1
UNIQUE CONTACT
G001 Swr
G002 B
G002 DR
G002 P
G003 A
G004 PO
G004 A
With FS set to tab..
$ awk -F '\t' '{n=split($3,arr,",");for(i=0;i++< n;){printf "%s\t%s\n",$1,arr;}}/#/' file1
UNIQUE CONTACT
G001 Swr Pq
G001 SD
G002 B
G002 DR
G002 P
G003 A F
G004 PO
G004 A C
Ygor,
I tried this code and get the following message, I have typed exactly as shown by you.
awk: syntax error near line 1
awk: bailing out near line 1
Any idea what i am doing wrong
Many Thanks for all your responses.
---------- Post updated at 02:15 PM ---------- Previous update was at 02:13 PM ----------
Franklin,
Yes i did try Ygor solution (with tab) but get syntax error, any idea what could go wrong?