Need help with simple comparison in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with simple comparison in AWK
# 1  
Old 04-29-2012
Need help with simple comparison in AWK

Hi,

I'm new to AWK and I'm having problems comparing a field to a string variable.

Code:
/ARTIST/ {x = $2}
$1 ~ x {print $0}

My code tries to find a record with the string "ARTIST". Once it finds it, it stores the second field of the record into a variable. I don't know what the problem is for the second line. The point is if the first field of a record matches the variable, it will print the entire record. But for some reason, it's not working.

Thanks, need help urgently!!
# 2  
Old 04-29-2012
Can you post sample data and desired output?
# 3  
Old 04-29-2012
There are two input files.

The first one contains data about artists.

Peter trumpet
Bob piano
John drums


The second one contains a list of commands.

ARTIST John

For example, I want to go through the second file, so I'll store "John" into variable x. Then I want to compare x to the first fields of the first file. If it equals, then I'll print out the entire record. I'm having trouble making that comparison for the variable and the field.
# 4  
Old 04-29-2012
Code:
awk 'NR==FNR{x=$2}NR!=FNR&&$1==x' file2 file1

# 5  
Old 05-01-2012
Need help with simple comparison in AWK

Hi Bart,

This is not giving desired out put. This is outputing only one value. if I put 2 entry in file2

Code:
ARTIST John
PLAYER Bob

Then it is giving output as Bob piano. it should give
Code:
Peter trumpet
Bob piano

I tried modified code like but not able to do correctly.

Code:
awk 'NR==FNR{x[i]=$2}NR!=FNR&&$1 in x { print x[j]} ' f3 f2

Can some one make this correct?

Idea is to take values in array then compare

Thanks
Krsnadasa

Moderator's Comments:
Mod Comment Use straight brackets for code tags instead of angular ones

Last edited by Scrutinizer; 05-01-2012 at 06:18 AM..
# 6  
Old 05-01-2012
Try:
Code:
awk 'NR==FNR{x[$2]=1}NR!=FNR&&$1 in x' file2 file1

# 7  
Old 05-01-2012
Need help with simple comparison in AWK

Thanks Bart,

Can you please help me to understand the array concept here? As I am always confuse.

Here first we are first reading f2 and filling arr X like :

Code:
x[John]=1,x[Bob]=1 ...

is this is an associative array?

When we read file 1 then N==FNR is false then we are matching the pattern $1 from file1 in array..
Code:
 
awk 'NR==FNR{x[$2]=1}NR!=FNR&&$1 in x' file2 file1

Want to understand how array is formed and then serached.

Thanks
Krsnadasa
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk IF date comparison help

Hey everyone, I'm trying to create a script using awk and if that will list all of our aws tapes that have archived date that is past 90 days from todays current date, so that I can pass that to my aws command to remove. The fifth column is the creation date in epoch/seconds, so I'm... (13 Replies)
Discussion started by: beyondmondays
13 Replies

2. Shell Programming and Scripting

File comparison using awk

Hi All, i have two files file1 ,file 2 file 1 col1|col2|col3|col4|col5|col6|col7|col8 11346925|0|2009-09-20|9999-12-31|100|0 11346925|0|2009-09-20|9999-12-31|120|0 12954311|0|2009-09-11|9999-12-31|100|0 12954311|0|2009-07-23|2999-12-31|120|0 12954312|0|2009-09-11|9999-12-31|100|0... (9 Replies)
Discussion started by: mohanalakshmi
9 Replies

3. Shell Programming and Scripting

Simple comparison between two lists.

I have two lists (input) Alpha and Beta. Alpha: Beta: Need the output like this: I would like to get an output like this: Alpha vs Beta | -- | a=1 | |z=3 | z=4 | Is it possible ? :cool: (5 Replies)
Discussion started by: linuxadmin
5 Replies

4. Shell Programming and Scripting

AWK string comparison

Hey guys.. New in linux scripting and need some help on some scripting with history command. I managed to export the command history into a file and now i'm trying to select from that file some specific commands that were made in a certain period. Here's what i got so far echo -n... (2 Replies)
Discussion started by: mishu_cgm
2 Replies

5. Shell Programming and Scripting

awk comparison and substitution

Hi, here's my - not so easy to describe - problem: I want to compare the values of one file (FileA) with a cutoff-value and, if this comparison is true, substitute those values with those in the second file (FileB). However, there are many FileA's (FileA), whereas there is only one FileB. Every... (10 Replies)
Discussion started by: waddle
10 Replies

6. Shell Programming and Scripting

awk comparison

Hello all, Probably a very simple question, I am stuck with a small part of a code: I am trying to do a comparison to get the maximum value of column 6 if columns 1, 4 and 5 of two or more rows match. Here is what I am doing: awk -F'\t' '{if ($6 > a)a=$6}END{for (i in a) print i"\t"a}' ... (4 Replies)
Discussion started by: jaysean
4 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

Simple XML file comparison and merging

Okay, first of all, thanks to everyone who's helped me out before... I appreciate the opportunity to learn. I have two iTunes XML files, and I simply want to compare the contents, then merge. Theoretically, this will allow me to merge two libraries, keeping playlists intact (depending on iTunes'... (4 Replies)
Discussion started by: karlp
4 Replies

9. UNIX for Dummies Questions & Answers

multiple comparison in awk

I have an input file. Each line in it has several characters. If the first three characters of the line is '000' or '001' or '002' or '003', I need to print it in output. How can I do this in awk. I am able to do if the search string is only one (let us say 000). cat <filename> | awk... (1 Reply)
Discussion started by: paruthiveeran
1 Replies

10. Shell Programming and Scripting

Comparison of two files in awk

Hi, I have two files file1 and file2 delimited by semicolon, And I want to compare column 2 and column3 of file1 to column3 and column 4 in file2. file1 -------- abc;cef;155.67;143_34; def;fgh;146.55;123.3; frg;hff;134.67;; yyy;fgh;134.78;35_45; file 2 --------- abc;cef;155.09;;... (12 Replies)
Discussion started by: jerome Sukumar
12 Replies
Login or Register to Ask a Question