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
# 1  
Old 04-19-2012
Bug Gawk on Windows: Joining lines only if 1st field matches

Hi..
i have two files::
file_1::
Code:
mOnkey
huMAn

file_2::
Code:
Human:hates:banana
i:like:***
Monkey:loves:banana
dogs:kill:cats

desired output::
Code:
Monkey:loves:banana
Human:hates:banana

so only when the 1st field matches from both files print it from file_2 ((case-sensitive))



i also would like to know how to match different fields..
file_1::
Code:
AA:11
BB:22

file_2::
Code:
first:AA:2011
second:BB:2012

desired output::
Code:
first:AA:2011:11
second:BB:2012:22

so only if field1 from file_1 matches with field2 from file_2 print whole file_2 joined with field2 from the same line in file_1

i hope u understand my cheesy english : P THANKx in advance
# 2  
Old 04-19-2012
You say it's case sensitive, but the case of your example data doesn't always match. Do you actually mean case insensitive?
# 3  
Old 04-19-2012
hmmm.. no i actually meant to follow file_2..
note:: it would be really great if some1 can explain both awk and join commands
# 4  
Old 04-19-2012
join requires the files to be in the same order, so doesn't sound like what you need.

awk is an entire self-contained programming language for dealing with flat files, with powerful abilities to select columns, use associative arrays, and match regular expressions. In some ways it's even more powerful than perl, or at least more convenient -- columns are built deeply into the language in a way perl's mostly abandoned. But perl is a general-purpose language, and awk is not...

Code:
$ awk -F":" 'NR==FNR{A[$1]=$0; next}; $1 in A { print A[$1] }' file2 file1
Monkey:loves:banana
Human:hates:banana

$

# 5  
Old 04-19-2012
@Corona68::
thanks.. but doesn't work with my files... nor with the examples given above..
i get no output at all..
im using gawk with windows.. maybe thats why??
i converted the quotes::
Code:
gawk -F":" "NR==FNR{A[$1]=$0; next}; $1 in A { print A[$1] }" 11.txt 22.txt

# 6  
Old 04-19-2012
He used the files in reverse order. You mean, match case insensitive, but print case from file2...?

Code:
gawk -F":" "NR==FNR{a[tolower($1)]++;next}tolower($1) in a" 11.txt 22.txt

edit: i believe gawk even compiled on windows will not treat DOS line endings well. Try this next to remove them first
Code:
gawk -F":" "1{sub(/\r$/,"")}NR==FNR{a[tolower($1)]++;next}tolower($1) in a" 11.txt 22.txt

This User Gave Thanks to neutronscott For This Post:
# 7  
Old 04-19-2012
@neutronscott::
actually the 1st one worked very well.. THANKS
now please.. could u try the other request
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