Gawk on Windows: Joining lines only if 1st field matches


 
Thread Tools Search this Thread
Special Forums Windows & DOS: Issues & Discussions Gawk on Windows: Joining lines only if 1st field matches
# 8  
Old 04-19-2012
try

Code:
awk -F: 'FNR==NR{a[$1]=$2;next}$2 in a{print $0 FS a[$2]}'

This User Gave Thanks to neutronscott For This Post:
# 9  
Old 04-20-2012
@neutronscott::
i dont know how it works but it does =D.. i guess i'll have to study it
thanks a bunch

---------- Post updated at 07:54 AM ---------- Previous update was at 06:07 AM ----------

@neutronscott::
would u mind explaining the last command briefly??
# 10  
Old 04-20-2012
Quote:
Originally Posted by M@LIK
im using gawk with windows.. maybe thats why??
That would have been nice to know, yes. I've spent a lot of time fighting with gawk in windows because if CMD's ridiculous quoting problems.
# 11  
Old 04-20-2012
Quote:
Originally Posted by Corona688
That would have been nice to know, yes. I've spent a lot of time fighting with gawk in windows because if CMD's ridiculous quoting problems.
LOL... im used to it..
i just wanted him to explain because i have a lot of similar cases.. and i dont like asking too much
# 12  
Old 04-20-2012
Hi,

i am learning awk and have started looking into one liners shown in this forum. i have doubt in this. i have just expanded this oneliner for my understanding and trying to execute it but it throws error. please help me

Code:
bash-3.00# cat nawktest
#!/usr/bin/nawk -f
BEGIN {FS=":"}
if (FNR==NR)
{
a[$1]=$2;
next;
}
for ($2 in a)
{
print $0 FS a[$2];
}



Code:
bash-3.00# ./nawktest test.txt test1.txt
/usr/bin/nawk: syntax error at source line 3
 context is
         >>> if <<<  (FNR==NR)
/usr/bin/nawk: bailing out at source line 3
bash-3.00#

# 13  
Old 04-20-2012
@chidori::
where did u get the "if" from?? there no any if in the one liner..
# 14  
Old 04-20-2012
@chidori: you need to put an extra opening brace before the if and a closing one at the end.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

2. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

5. UNIX for Dummies Questions & Answers

join 2 lines based on 1st field

hi i have a file with the following lines 2303:13593:137135 16 abc1 26213806....... 1234:45675:123456 16 bbc1 9813806....... 2303:13593:137135 17 bna1 26566444.... 1234:45675:123456 18 nnb1 98123456....... i want to join the lines having common 1st field i,e., ... (1 Reply)
Discussion started by: anurupa777
1 Replies

6. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

7. UNIX for Dummies Questions & Answers

JOINING MULTIPLE LINES IN A TEXT FILE USING GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. I WANT THE DATA... (0 Replies)
Discussion started by: KANNI786
0 Replies

8. UNIX for Dummies Questions & Answers

Joining lines of a text file using GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. ... (0 Replies)
Discussion started by: KANNI786
0 Replies

9. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

10. Shell Programming and Scripting

Joining columns from two files, if the key matches

I am trying to join/paste columns from two files for the rows with matching first field. Any help will be appreciated. Files can not be sorted and may not have all rows in both files. Thanks. File1 aaa 111 bbb 222 ccc 333 File2 aaa sss mmmm ccc kkkk llll ddd xxx yyy Want to... (1 Reply)
Discussion started by: sk_sd
1 Replies
Login or Register to Ask a Question