Nawk match regex of bash variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk match regex of bash variable
# 1  
Old 02-05-2015
Nawk match regex of bash variable

Using a bash for loop to pass variables into a nawk loop to capture a string in an sftp log. Tried several different syntax methods to have the variable treated as a regex so the loop will capture the string.

Code:
for i in `cat /tmp/dar3.out.2`
do
nawk -vst=$i '$5 ~ /$st/ && /closed/ && /user/ && !/ORG/  && !/LE/ { for (x=1;x<=NF;x++) if ($x~"user") print $(x+1) } /var/adm/sftplog >> /tmp/dar3.out.cli
done

The contents of /tmp/dar3.out.2 are all sftp session log ID's.

15103
27888
23641
3161
15355
23040
26631
1065
7220
12443
8505
29111
29114
27654
11999
22904
19679

Please advise if there is a way to have
Code:
 /$st/

treated as a regex so I can pass into nawk loop.

Thanks
# 2  
Old 02-05-2015
Must be $5 ~ st
# 3  
Old 02-05-2015
If I try using that I receive the following -

Code:
+ nawk -vst=19679 '$5 ~ st && /closed/ && /user/ && !/ORG/  && !/LE/ { for (x=1;x<=NF;x++) if ($x~"user") print $(x+1) } ' /var/adm/sftplog
nawk: empty regular expression
 input record number 1, file /var/adm/sftplog
 source line number 1

# 4  
Old 02-05-2015
Try -v st=12345
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Bash to match and store line as variable

The bash below loops through a specific directory dir and finds and writes the oldest folder to a variable called $filename. #!/bin/bash # oldest folder stored as variable for analysis, version log created, and quality indicators matched to run dir=/home/cmccabe/Desktop/NGS/test find... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Using RegEx with variable within bash if [[ ]]

I stumbled upon a problem, which I simplified to this: There is a list of numbers, stored in variable $LIST, lets use `seq 5 25` for demonstration. There is a number that should be compared against this list. For demonstration I use user input - read VALUE I am trying to compare RegEx... (2 Replies)
Discussion started by: Zorbeg
2 Replies

4. Shell Programming and Scripting

Help with nawk (Exact Match)

I have a file with the contents below "lets say the name of the file is abcxyz" shown at the end of this. I am using nawk to find the exact ip address and the 6 lines after the match is found using the following nawk statement /usr/bin/nawk "/111.46.14.107/,printed==6 { ++printed; print; }"... (7 Replies)
Discussion started by: knijjar
7 Replies

5. Shell Programming and Scripting

Using BASH =~ regex to match multiple strings

I have a scripting problem that I'm trying to solve, whereby I want to match that a string contains either of three strings. I'm thinking this is probably just me not understanding how to craft the appropriate regex. However, here's what I would like to do: ] && do-something more... (10 Replies)
Discussion started by: forrie
10 Replies

6. Shell Programming and Scripting

Nawk - print only a string containing a regex.

Hi again, I'm looking for some help with nawk, I can print a line which has a regex match in it from a file using /pattern/ but I'm looking for a way to only print the $tring which contains the pattern, rather than the whole line. This $tring may be of variable length, may occur at any point... (1 Reply)
Discussion started by: spynappels
1 Replies

7. Shell Programming and Scripting

match 2 files using nawk command

i am matching two files. the files are in the format file_1.txt 1|_|X|_| 2|_|W|_| 3|_|Y|_| 4|_|Z|_| 5|_|U|_| file_2.txt W|_|A|_| Z|_|C|_| V|_|B|_| X|_|D|_|sdff|_| Y|_| file_3.txt should be in the format (4 Replies)
Discussion started by: centurion_13
4 Replies

8. Shell Programming and Scripting

Problem in splitiing file based on regex using awk/nawk

I have a file tmp.txt as shown below: Controller: 0 Disk: 0.0.0 Disk: 0.1.0 Disk: 0.2.0 Controller: 1 Volume:c1t2d0 Disk: 0.0.0 Disk: 0.1.0 Disk: 0.2.0 Controller: 2 Volume:c2t2d0 Disk: 0.2.0 Now I want to split... (4 Replies)
Discussion started by: durbam2002
4 Replies

9. Shell Programming and Scripting

NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but... My input data: (from an input file) i only show the first record in my file.. all other records are similar. mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql... (2 Replies)
Discussion started by: danmauer
2 Replies

10. Shell Programming and Scripting

How to specify 'not case sensitive' in regex (nawk, sed, patern expencions)?

Is it possible to make the search in regular exprecion or in matching parts of sed, nawk and others to IGNORE the case of the search string? I mean, like if used 'grep' with -i option: > grep -i "abc" file I would like to be able to do the same, say, by nawk: > nawk '/abc/ {print $0}'... (4 Replies)
Discussion started by: alex_5161
4 Replies
Login or Register to Ask a Question