How to pass shell variables to awk's pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass shell variables to awk's pattern?
# 1  
Old 09-02-2010
How to pass shell variables to awk's pattern?

How would I get folders owned by specific users.. I want to pass users as a shell variable to awk.

Code:
 
drwxr-x--x   3 user1  allusers     512 Oct 14  2006 946157019/
drwxr-x--x   3 user2  allusers     512 Mar  9  2008 94825883/
drwxr-x--x   3 user3  allusers     512 Mar  9  2008 948390501/

For some reason when I pass search expression as argument, its not working. But when I replace $var1 with hardcoded string it works..

Code:
 
$>ls -lt | /usr/xpg4/bin/awk -v var1='user1|user2' '$3 ~ /"'$var1'"/'

How do we pass pattern as argument to awk?
Same approach works if its awk's statement.
# 2  
Old 09-02-2010
Code:
ls -lt | /usr/xpg4/bin/awk -v var1='user1|user2' '$3 ~ var1'

# 3  
Old 09-02-2010
That works,, why this version of syntax does not use !!?

Quote:
'$3 ~ /var1/'
instead it uses
Quote:
'$3 ~ var1'
When I replace var1 with its value without // wrapping it does not work..
# 4  
Old 09-02-2010
Hi.

From the awk man page:

Quote:
/re/ is a constant regular expression
Here, the regular expression would be re (the literal string re), not the value of the variable re.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - using variables in pattern which contain full pathname

Hello. I would like to make this bash command working. In the following code, the bash variable 'ZYPPER_LOCAL_REP' contain a full pathname like '/path/to/path/somewhere' The command list all available repositories, search for the string 'zipper_local' then on the same line search for... (4 Replies)
Discussion started by: jcdole
4 Replies

2. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. UNIX for Dummies Questions & Answers

Pass variables from a text file to a shell script

Hi, I have a text file as follows: a.txt ------ STEPS=3 STEP_DURATION=100 INTERVAL=60 I want to use these values in a shell script. How to go about this? (3 Replies)
Discussion started by: akarnya
3 Replies

5. UNIX for Advanced & Expert Users

use of variables in awk to search for pattern from a file

Hi, I need to extract all the content between two strings stored in two variables "startstring" and "endstring" startstring=hello enstring=world #notworking awk '/$startstring/, $NF ~ /$endstring/ ' file > file2 The above code is not working with variables. It works when actual string... (2 Replies)
Discussion started by: jeanjkj
2 Replies

6. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

7. Shell Programming and Scripting

Text pattern with AWK and variables

How to to pass variable to the below awk command I would like to pass variables to the awk command instead of the constants "a/cc" but I don't know exactly the correct syntax awk '/a/,/cc/' temp1.out file.txt a b s cc g d output a b s (8 Replies)
Discussion started by: ahmedamro
8 Replies

8. Shell Programming and Scripting

pass perl variables to shell script

I have a perl script that opens a text file containing numbers on each line: for example: 755993 755994 755995 755996 755997 755998 The perl script takes these numbers and store them as an array @raw_data, where I can access individual numbers by using $raw_data for the value 755993.... (2 Replies)
Discussion started by: xchen89x
2 Replies

9. AIX

how to pass variables surrounded in double quotes to awk?

Hi, I'm making progress on this but hung up on one last detail. I'd like to use AWK to pass the system date and time(among other things) to the first line of a file. Here's what I have: BEGIN {TOTALPP = 0;FREEPP=0;USEDPP=0;print "LPAR NAME:",lpar,"DATE:",tdate } I call AWK with the... (4 Replies)
Discussion started by: cruiser
4 Replies

10. Shell Programming and Scripting

How to pass Shell variables to sqlplus use them as parameters

Hi, I am trying to pass some of the variables in my shell scripts to the sqlplus call and use them as parameters. For example, I would like to replace the 'SAS', and '20050612' with $var1 and $var2, respectively, how can I do that? --------------------------------------------------------... (1 Reply)
Discussion started by: Jtrinh
1 Replies
Login or Register to Ask a Question