Shell variable as awk condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell variable as awk condition
# 1  
Old 08-31-2014
Shell variable as awk condition

Hi,

I'm trying to automate part of a script which uses awk to grab out some lines of a log file based on certain fields matching. For example, my log file looks something like the following (but 1000s of lines):

Code:
1 Tom 123 abc 345
2 Dick 345 abc 678
3 Harry 567 abc 345
4 Tom 345 cde 345
5 Harry 123 cde 567

My script simply uses awk to grab the second field being 345 or 123 for example:

Code:
awk '$2 == "345" || $2 == "123" log.file

However, the 345 or 123 is dynamic, so what I'd like is to replace the awk condition with a shell variable, i.e. something like the following

Code:
awk -v SHELLVAR=${DYNAMIC} 'SHELLVAR' log.file

Where $DYNAMIC would be as follows, generated elsewhere in the script:

Code:
$2 == "345" || $2 == "123"

I've tried various things around the above, but just get every line from the log file rather than selecting out those that match as per $DYNAMIC

I'm probably missing something really obvious here - Any ideas?
# 2  
Old 08-31-2014
well your $2 is names in this example, so that should be $3. the proper way to pass and use variables follows:

Code:
$ value=345
$ awk -vn="$value" '$3 == n' file
2 Dick 345 abc 678
4 Tom 345 cde 345

what you describe is using a shell variable to hold an awk program.

Code:
mute@thedoctor:~$ code='$3 == 345 || $3 == 123'
mute@thedoctor:~$ awk "$code" file
1 Tom 123 abc 345
2 Dick 345 abc 678
4 Tom 345 cde 345
5 Harry 123 cde 567

# 3  
Old 08-31-2014
Well that's embarrassing! I'm sure I tried that in the very first instance, but it didn't work, hence trying to over complicate it! I must have got my code slightly wrong.

Thanks!
# 4  
Old 08-31-2014
You could also try (simplified "code" assignment):
Code:
code='345|123'
awk -vCOD="$code" '$3 ~ "^("COD")$"' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

How to pass IF condition via shell varibale in awk?

Hello, I have one query regarding passing IF condition shell variable inside awk. Here is the case- File content of keydefn.exp 201~2~LM Limit 02-current value~Limit 02 ~Limit02~Current~Value ~N~Y~S~0~9999999 201~3~LM Limit 03-current value~Limit... (2 Replies)
Discussion started by: anillambait
2 Replies

3. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

4. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

5. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

6. 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

7. Shell Programming and Scripting

Problem using shell variable in awk if condition

Hi friends, I'm having a bit of a problem using shell variable in an awk if statement. Please note that i'm using -v option as listed in many forums but I still don't get it working. Here's my code. Kindly help as I've gone crazy trying to work this out :wall: #!/bin/bash -xv ... (4 Replies)
Discussion started by: vishwas.s
4 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

Shell variable with awk

line_no=6 echo 'Phone,' `awk 'NR==$line_no{print;exit}' <filename>` what is the error in this.. it says.. awk: Field $() is not correct. The input line number is 1. The file is <filename>. The source line number is 1. i want to print the data in the $line_no line of a certain... (2 Replies)
Discussion started by: St.Fartatric
2 Replies

10. Shell Programming and Scripting

shell variable in awk

Hi All, How can i use the file for printing in awk file1 ---------- update table crn_ras_disc_dtl a set a.a5=$1,a.a1=$2,a.a2=$3,a.a3=$4,a.a4=$5; file2 -------- 10|KUMAR|23|MALE|US 20|RAJ|24|MALE|AU Output --------- update table crn_ras_disc_dtl a set... (12 Replies)
Discussion started by: cskumar
12 Replies
Login or Register to Ask a Question