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
# 15  
Old 04-20-2012
finally this has been mastered::
this could be useful to some1::
Code:
gawk -F: "FNR==NR{a[($1)]=$2;next}$1 in a{print $0 FS a[$1]}"
                      ^ matching field file1             ^ matching field file2
                           ^ the field we want to append from file1
                                   ^ matching field file2(agian)

done this with many files.. and its SUPER! =D
# 16  
Old 04-20-2012
@M@LIK, the parentheses are superfluous (FNR==NR{a[$1]=$2 .. ).

So to be clear, this is gawk running straight on windows? This could never function on Unix since there should be single quotes around the awk statements, otherwise the shell will interpret the variables...

Moving to windows forum
# 17  
Old 04-20-2012
@Scrutinizer::
O.o
it was about awk not windows or dos??
yea.. on windows you have to use double quotes.. unix single quote.. thats a minor thing
and yes its working very well.. u can try it on unix also after replacing the quotes.. the rest is the SAME
-_-
# 18  
Old 04-20-2012
I wouldn't call cmd.exe a "shell" but it is fun putting quotes inside of your quotes. I suggest using files and gawk -f input.awk for any awk which involves a string on Windows. Smilie

@Scrtinizer: I do think it should still be in the awk forum, since the original post wasn't pertaining to cmd.exe quoting issues.
# 19  
Old 04-20-2012
@ scrutinizer

have done it but still getting the error..


Code:
#!/usr/bin/nawk -f
BEGIN {FS=":"}
{
if (FNR==NR)
{
a[$1]=$2;
next;
}
}

for ($2 in a) {
print $0 FS a[$2];
}

# 20  
Old 04-20-2012
Try:

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

indenting the code helps a lot by the way ...
# 21  
Old 04-20-2012
sorry Smilie , still getting the same error
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