having trouble with using if clause in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting having trouble with using if clause in AWK
# 1  
Old 02-06-2009
having trouble with using if clause in AWK

The goal:
I have a list of people in teams. The list looks something like this

$1 = Job Position (marketing, IT, PR)
$2 = Name
$3 = Team Name
$4 = Targeted member (somebody in field 2 targets somebody else)
$5 = Employment Status (full time/part time/etc)

The idea is to search through the second field and spit out the lines that have the search results. The problem is since everybody who is in field 2 is also in field 4 (since everybody is targetted at least once) I'm getting double the results I want. I just want to search through the second field.

I'm trying to have a simple line of code that will look into the second field of a list, and use a regular expression to search that field and print it out. This is what I have, which I think should work, but it won't, and i'm fresh out of ideas. Any assistance would be helpful.

like

awk -F, ' /John/{print $0}' myfile

does the trick, it finds any line with john, fantastic. HOWEVER, my goal is to find all the Johns in ONE field.

awk -F, ' { if($4=="John") print $0} ' myfile

now this line of code should work (and returns nothing even when the first line returns the correct entries) but even if it did, it finds precisely John, where i'd also like to find any name that might make up John, ie, ohn.

I know that sounds like a grep problem but i'm having trouble dealing with grep even more then awk which i'm sure is how to go about doing this problem.

any and all help greatly appreciated.
# 2  
Old 02-07-2009
Code:
string=John
awk -F, -v str="$string" '$2 ~ str' FILENAME

# 3  
Old 02-10-2009
that worked perfectly! A huge thanks, I was not aware I had to use -v to use variables in awk (though makes sense :/)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble using awk command

Hi, I have 2 .txt pads containing data. I need a script which reads content of one .txt file, performs some operations and calculates a number which is stored in a variable. Now , all the content of another .txt pad should be appended to first .txt pad at pre calculated nth line number. ... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

2. Shell Programming and Scripting

Trouble with awk command

Hi, I need to read a string with ; separated using loop one filed by one field and perform some operation. Can you please check and let me know how to print command parameterised. key=phani;ravi;kiran number_of_keys=`echo $key|awk '{print NF}' FS=';'` for (( i = 1; i <= $number_of_keys;... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

3. UNIX for Dummies Questions & Answers

trouble with awk

I am trying to figure awk. I have a file in my home directory called testawk.sh, have made it executable, and have run it... But don't see any output. This is the contents of the file: #!/usr/bin/awk -f { print " - HI -" }I enter ./testawk.sh in the prompt, press enter, and watch as the... (2 Replies)
Discussion started by: matthewden
2 Replies

4. Shell Programming and Scripting

Trouble getting the next to last record with awk

Hello all, I'm a beginner to shell/ awk script writing, and I'm trying to do something that looks like it shouldn't be (too) hard at all to do, but unfortunately, I can't seem to be able to find the right way to do it with awk. I need to look for the time several processes start & end in a... (5 Replies)
Discussion started by: Muadib
5 Replies

5. UNIX and Linux Applications

Print date at END clause of AWK

Hi to all! I 'm new in unix programing so... may be I decided a wrong tool to solve the problem but anyway... all road goes to rome jajaja. My question is: There is any way to print date at the END clause of an AWK script. I mean, I'm writing a tool with AWK and the results are redirected to a... (4 Replies)
Discussion started by: fmeriles
4 Replies

6. Shell Programming and Scripting

The function of substr clause in awk command

Hello all, Please help me in letting me know the function of *substr* function in awk... actually i am new with this function i can play with awk but for this function needs your help in making me understand the correct way of using it. I am writting a code please advice regarding this... (4 Replies)
Discussion started by: jojo123
4 Replies

7. Shell Programming and Scripting

Trouble with Awk

Hi all, I'm writing a program in bourne shell that compresses a file 3 different ways then displays a table of data with the compression type, original file size, compressed size and compression ratio. I've written most of it but reached 2 problems that won't allow me to finish it correctly. The... (2 Replies)
Discussion started by: javajynx
2 Replies

8. Shell Programming and Scripting

Trouble with printing to other folders w/ awk

Again, I am in need of some advice. Earlier I was shown how to have awk create folders for me. That was so cool and helpful, but now I am exploring the posibilities of combining operations with bash scripts. Now, I am creating the directories with the bash script, and then I want awk to... (1 Reply)
Discussion started by: ccox85
1 Replies

9. Shell Programming and Scripting

Trouble with awk

This is probably a fairly simple question but I cant seem to get it to work. Im trying to multiply an entire column in a file by a variable in my bash script but just cant seem to get it to work with awk. Here is what I'm trying $varr is some value $line is my file awk '{print... (1 Reply)
Discussion started by: RichieFondel
1 Replies

10. Shell Programming and Scripting

if clause in AWK END block not working.

Hello all... I have a slight problem in my awk script... I have a script which checks a csv file and keeps a count of any invalid records and then if it finds any, exits with a code of 1. problem is it dosnt seem to work properly :rolleyes: Everthing seem to work interms of the stats output,... (1 Reply)
Discussion started by: satnamx
1 Replies
Login or Register to Ask a Question