take input from a variable as pattern to awk


 
Thread Tools Search this Thread
Top Forums Programming take input from a variable as pattern to awk
# 1  
Old 12-13-2011
take input from a variable as pattern to awk

Hi everyone,

Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output:
$c = "Tue Dec";
$log = ` awk '/ \$c /' in.txt`;
print $log;
# 2  
Old 12-13-2011
Try
Code:
log=`awk -vpat1="$c" '$0 ~ pat1' in.txt`;

# 3  
Old 12-13-2011
From where you want to take your contents? From the shell?

Code:
 C="Tue Dec"
 LOG=`awk '/'"$C"'/' in.txt`
 echo $LOG

or from awk itself?
# 4  
Old 12-13-2011
Thanks pfylnn for your reply.

Its a perl script. Actually my code is as below:

system("lastlog -u user > last.txt");
`sed -n '2p' last.txt > last1.txt`;
$c = `cut -c44-62 last1.txt`;
print $c;

$log = ` awk '/\$c/' in.txt`;
print $log;
# 5  
Old 12-13-2011
Oh, I see. Just remove the backslash before the $c variable:

Code:
$log = ` awk '/$c/' in.txt`;

and it will do the work.
# 6  
Old 12-13-2011
hey Pfylnn,

if i reeive backslash it gives me this error:

awk: /Tue
awk: ^ unterminated regexp
sh: line 1: /: is a directory

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

Hey carloM,

Thanks for ur reply. But the code u suggest gives me this error:

awk: ./OSS20.pl ~ pat1
awk: ^ syntax error
# 7  
Old 12-13-2011
Quote:
Originally Posted by anandrec
hey Pfylnn,

if i reeive backslash it gives me this error:

awk: /Tue
awk: ^ unterminated regexp
sh: line 1: /: is a directory
I guess this is because there is a line break in the $c variable (or maybe somewhere else). Have you checked it? If you just asign directly the value "Tue Dec" to $c it will work. So probably there is something different in $cīs contents.

[EDIT] You can 'chomp' $c before using it to remove the line break:

Code:
$c = `cut -c44-62 last1.txt`;
chomp $c;
$log = ` awk '/$c/' in.txt`;
print $log;

this way awk will not complain any more.

Last edited by pflynn; 12-13-2011 at 08:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. Shell Programming and Scripting

Variable input to awk script

Hi guys, I wrote the following function to compare two csv files column by column. However, sometimes the input needs to be sorted before parsing it to awk. I can do this by changing the awk arguments, but I would like to make this variable if possible. The below doesn't work since the... (3 Replies)
Discussion started by: Subbeh
3 Replies

3. Shell Programming and Scripting

Call a awk script with variable and input filename

HI, MY question is a very simple one: if i want to call an awk script with the input file name and also pass a variable value , then how to do it. #>awk -f my_script.awk -v variable=value my_inputfile.txt I can't do it like this. throws error: awk: my_script.awk:18:... (0 Replies)
Discussion started by: Onkar Banerjee
0 Replies

4. Shell Programming and Scripting

Passing variable as an input file to AWK comand

Hi, I would like to compare 2 files using awk, which I can do by using: awk 'NR==FNR{a;next} (NR > 32 && $2 in a) {print $0}' File1 and File2. If the name of the File1 is in another file (for example, column 4 in File 3) then how can I pass this column 4 to the awk command. Thanks in... (1 Reply)
Discussion started by: ezhil01
1 Replies

5. Shell Programming and Scripting

Variable%pattern operation within awk

Hello; I have: ll | grep -v ^d | awk '{print $9}' rcx_access_report_fid.txt rcx_access_report_hsi.txt rcx_access_report_mmm.txt rcx_access_report_qsc.txt I want to get: rcx_access_report_fid.txt rcx_access_report_hsi rcx_access_report_mmm rcx_access_report_qsc But when I try: (9 Replies)
Discussion started by: delphys
9 Replies

6. Shell Programming and Scripting

Variable as input to awk command

Hi Gurus, I need a suggestion, please help. I have a input file as below : abc.txt : * xxxx: 00000 xxxxx: 00000 xxxx: RANDOM xxx: RANDOM **************************xxxxxxx*** * abc ****************************** abc: abc: ... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. Shell Programming and Scripting

awk built-in variable for input file

Hi guys, Does awk have a built-in variable which I can use to display the input file it's currently reading? I'm currently concatenating multiple files using awk and later on do some parsing. But for now, I want to add an extra column in the main output data file - basically putting in the... (3 Replies)
Discussion started by: Det7
3 Replies

8. UNIX and Linux Applications

Input a variable and write to a file using awk

Hi I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable. My csv file looks like so: 1,1A,5 1,1B,2 1,1C,3 2,2A,7 2,2B,4 2,2C,0 3,3A,1 3,3B,6 3,3C,4 I want to... (4 Replies)
Discussion started by: ladyAnne
4 Replies

9. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

10. Shell Programming and Scripting

AWK: pattern not properly stored in variable?

Hey there, I have a table of contents file of the form 1 Title1 1.1 Subtitle1 1.1.1 Subsubtitle1 1.1.2 Subsubtitle2 ... and want to count the number of dots in the first field to find out the level of the section. I use the gsub function for the job, which works if I pass the pattern... (2 Replies)
Discussion started by: herrsimon
2 Replies
Login or Register to Ask a Question