Struggling with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Struggling with AWK
# 1  
Old 10-22-2009
Struggling with AWK

I know this is an easy problem, but I've been struggling with it all morning and finally admit I need help.

I have two files that I need to compare. After doing this the easy way (grep -F) I realize that there are other fields in file2 that match field1 in file1, so I turned to awk.

I want to compare the first field in each, and if field1 of file1 matches field1 of file2, print out the entire line in file2. I've been trying to write an awk script but just don't fully understand how to do this.

I get as far as:

Code:
awk '
FS="\t";
BEGIN {
while (getline <"file1.txt"){
$fieldq==$1;

and I can't figure out how to read in the second file to do the comparison and then print $0 from file2.

Last edited by Franklin52; 10-22-2009 at 11:04 AM.. Reason: Please use code tags!
# 2  
Old 10-22-2009
  1. Please use
    Code:
    CODE

    tags
  2. Please use sample input and output, its like using picture instead of words

try this,

Code:
awk 'BEGIN { while ( getline < "abc.txt" ) { arr[$0] = $1 } } { if ( $1 in arr ) { print } }' file2.txt

( not tested )
# 3  
Old 10-22-2009
Another approach:

Code:
awk 'NR==FNR{a[$1];next}$1 in a' file1 file2

# 4  
Old 10-22-2009
Code:
awk 'NR==FNR{a[$1];next}$1 in a' file1 file2



---------- Post updated at 10:08 AM ---------- Previous update was at 10:07 AM ----------

Wow , just few seconds difference Smilie
# 5  
Old 10-22-2009
Can I suggest that you might look at Perl instead?

Awk is, well, awkward. Years ago, I'd keep my awk and sed skills up because I couldn't count on anything better being available on clients machines, but today Perl is everywhere. Why struggle when a Perl script will give you so much more with less effort?

Just my opinion, of course. See my "Why I love Perl" at Why I love Perl - I wrote that almost 10 years ago when I first started using Perl and I only love it more today.
# 6  
Old 10-22-2009
Thanks, as soon as I get this report finished I'm going to go back and read up some more on awk. I still have the sed&awk book somewhere, just haven't used it in 10 years and am beyond rusty.

---------- Post updated at 08:22 AM ---------- Previous update was at 06:27 AM ----------

Because I'm older, stubborn and can do this at the shell prompt, no other good reason. Again, I've been away from unix and coding for a while, but at one point 10 or 12 years ago was extremely fluent, especially in PERL. I used to hassle people in my group for writing scripts when I could do the same thing at the shell prompt Smilie
# 7  
Old 10-23-2009
Code:
perl -ne '/^(.+?) /; $foo{$1}++; eof && print if $foo{$1} > 1' file1 file2

You imply that you can't solve this at the command prompt with perl. Not so. You also imply that you would have to "bother" other people if you use perl. True in this case. Consider me "bothered" for a perl command-liner.

sed / perl: dude, whatever you are comfortable with Smilie

---------- Post updated at 01:57 AM ---------- Previous update was at 12:44 AM ----------

Code:
perl -ne '/^(.+?) /; ++$_{$1}; $ARGV eq 'b' && print if $_{$1} > 1' a  b

Sorry, the perl above is broken, this is not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

Parsing file: struggling against sed command

Hello fellows, Sure you can help this poor guy that is struggling against sed command, being unable to tame it I have a lot of files like this: From this one, I need to obtain values highlighted in bold/red To do so, I am executing this piece of code inside a loop: ... (10 Replies)
Discussion started by: manolain
10 Replies

4. UNIX for Dummies Questions & Answers

Struggling to combine two Greps statements

Greetings! I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values. When running each statement separately, I get... (4 Replies)
Discussion started by: alan
4 Replies

5. Shell Programming and Scripting

Struggling with arrays and delimited file

Hi, I am trying to use arrays in my script but can not seem to get it to work. I have a file called sections, this contains headers from a tripwire log file, separated by "@" but could be "," if easier The headers will be used to cut sections from the log file into another to be mailed. ... (5 Replies)
Discussion started by: pobman
5 Replies

6. Shell Programming and Scripting

struggling within awk

!#/bin/bash cat input.sh | awk ' { cur1=tolower($1) cur2=tolower($2) rsh $cur1 report | grep $cur2 # i just want to make the code line to work } ' the error which i get is .... ./madh1.sh: line 1: !#/bin/bash: No such file or directory awk: cmd. line:13: rsh $cur1 report |... (4 Replies)
Discussion started by: geeko
4 Replies

7. UNIX for Advanced & Expert Users

Help- Unix File Compare- Struggling

I had posted this earlier about 3 weeks ago and had recieved a response and I did sort both the files and the comm command is still not working. Can someone please assist me, I would really appreciate it. Below is what I am trying to do I need to compare File A with File B and create FILE C... (2 Replies)
Discussion started by: guiguy
2 Replies

8. Shell Programming and Scripting

Struggling with a text file

Hi, I am struggling with the following... I try to grep out information of a text file I got with lynx, a text browser. The text file I get from lynx with dump is attached in the bottom. What I would like to get is another file containing the astro-ph/98324 (number) and title and list of... (13 Replies)
Discussion started by: pau
13 Replies

9. UNIX for Dummies Questions & Answers

Struggling with mkfifo

Gurus, I did my research (on google, this site and my local library) but I am *still* lost. I am trying to teach myself about `named pipes` playing around with MKFIFO (Why not?). (1) It seems MKNOD is reserved to ROOT whereas MKFIFO is accessible to all users. Am I correct? If the answer is... (20 Replies)
Discussion started by: alan
20 Replies

10. UNIX for Dummies Questions & Answers

struggling with addusr ...

I'm completely new to UNIX, and having got the hang of directory navigation / creation (Just about), I think it is time to create an FTP account, so that I can upload a web site remotely. Herein lies the problem... 1: I cannot find a breakdown of addusr commands, so I cannot create a... (2 Replies)
Discussion started by: Dufty
2 Replies
Login or Register to Ask a Question