awk using env variable as search argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk using env variable as search argument
# 1  
Old 02-04-2009
awk using env variable as search argument

Hello,
I have a file like

Code:
was123##abcdefg abddef
was123##xuzaghg agdfgg
was133##CGHAKS DKGJG

from the file i need to print the line after ## where the serach value is passed by an env variable called luster (which is currently set to was123):



i tried using the below code but it does not work

Code:
>echo $luster
>was123
>nawk -v var=$luster '/luster/ {  print $2}' checkNewWebsphereErr.data
>

is there any other way to do that ?
# 2  
Old 02-04-2009
Try this:

Code:
nawk -F"#" -v var=$luster '/luster/ {print $NF}' checkNewWebsphereErr.data

# 3  
Old 02-04-2009
Quote:
Originally Posted by Franklin52
Try this:

Code:
nawk -F"#" -v var=$luster '/luster/ {print $NF}' checkNewWebsphereErr.data

didn't you mean this:
Code:
nawk -F"#" -v var=$luster '$0 ~ var {print $NF}' checkNewWebsphereErr.data

# 4  
Old 02-04-2009
Quote:
Originally Posted by vgersh99
didn't you mean this:
Code:
nawk -F"#" -v var=$luster '$0 ~ var {print $NF}' checkNewWebsphereErr.data

That's what I mean. Smilie

Thanks!
# 5  
Old 02-04-2009
passing variables into awk is a valuable trick -- although not necessary here:

Code:
awk '/##'$luster/ checkNewWebsphereErr.data

.... works fine.
# 6  
Old 02-04-2009
Quote:
Originally Posted by quirkasaurus
passing variables into awk is a valuable trick -- although not necessary here:

Code:
awk '/##'$luster/ checkNewWebsphereErr.data

.... works fine.
too cumbersome involving jumping back and forth from awk to shell and back, harder to read as well - I don't like this paradigm.
Just my $.02
# 7  
Old 02-04-2009
okay -- how about this:

Code:
fgrep '##'$luster file

all unix and shell.
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

Search for string in column using variable: awk

I'm interested to match column pattern through awk using an external variable for data: -9 1:751343:T:A -9 0 T A 0.726 -5.408837e-03 9.576603e-03 7.967536e-01 5.722312e-01 -9 1:751756:T:C -9 0 T C 0.727 -5.360458e-03 9.579447e-03 7.966977e-01 5.757858e-01... (7 Replies)
Discussion started by: genome
7 Replies

3. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

4. Shell Programming and Scripting

awk search using variable

Hi Forum. I have the following script that I would like to use a variable to search using awk but it's not working as expected: # Define working variables token_search_string=536088 token_search_length=16 This code example works by hardcoding 536088 in the string search: awk -v... (10 Replies)
Discussion started by: pchang
10 Replies

5. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Search the shell variable inside awk

Hai, I need to search a variable inside a file using awk in AIX. for ex: file.txt one two three four five i need to get the lines with two awk '/two/ {print}' file.txt But i need to change the two as a variable since it would be changed on runtime..i tried like below..nothing give... (3 Replies)
Discussion started by: jesu
3 Replies

7. Web Development

Deny from env=env-variable Does not work

(Above from Apache docs). On my system, using: SetEnvIf User-Agent Mozilla IsBad=1 Order allow,deny Allow from all Deny from env=IsBad ...I see that environment variable is set (using phpinfo()) but the page is still served. No errors in the Apache logs. (1 Reply)
Discussion started by: gnurob
1 Replies

8. Shell Programming and Scripting

Read env variables from argument file

Hi, I have a generic shell script (runBatchJob.sh) to read files (batchJob) with commands in them and execute the commands by reading the batchJob file as below ./runBatchJob.sh batchJob batchJob file $BATCHDIR/execute_procedure.sh $DATADIR/collectData.sh $OTHER_ENV_VAR/doSomething.sh ... (10 Replies)
Discussion started by: bOngY
10 Replies

9. Shell Programming and Scripting

Search for awk pattern in unix env variable

For a Script I need to detemine which field of the unix environment variable SHLIB_PATH has the WALTDB entry. export SHLIB_PATH=/usr/user5/WALTDB/oracle/product/10.2.0/lib32:/usr/TZD/bin.wdad/mug/oracle/lib: echo $SHLIB_PATH | awk -F: '{ print $1 }' Shure gives me the first entry, but... (6 Replies)
Discussion started by: sdohn
6 Replies

10. Shell Programming and Scripting

use awk to search a file using variable given from user

Hi! I want to print selected lines from a file using awk.The file contains 6 columns(dates,time,title,description,location and attendees). So i ask the user to insert the date that he wants to display and then search the file for the matching dates so i can print the line. echo Give the date... (7 Replies)
Discussion started by: DDoS
7 Replies
Login or Register to Ask a Question