How to correct this awk code without eval?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to correct this awk code without eval?
# 1  
Old 11-18-2013
How to correct this awk code without eval?

Hi everyone,

The following piece of awk code works fine if I use eval builtin
Code:
var='$1,$2'
ps | eval "awk '{print $var}'"

But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result
Code:
ps | awk -v v1=$var '{print v1}'   # output is $1,$2
ps | awk -v v1=`echo $var` '{print v1}'  # output is same as above
ps | awk -v v1=$var '{print $v1}'  # output is all the fields of ps command
ps | eval "awk -v v1=$var '{print v1}'"  # output is column of comma

How to get the desire output without using eval?

Last edited by royalibrahim; 11-18-2013 at 04:50 AM..
# 2  
Old 11-18-2013
The shell substitutes variables in "quotes".
Code:
var="$1,$2"

But here you maybe want
Code:
var='$1,$2'
ps | awk '{print '"$var"'}'

?

Last edited by MadeInGermany; 11-18-2013 at 05:32 AM..
# 3  
Old 11-18-2013
Quote:
Originally Posted by MadeInGermany
Code:
var='$1,$2'
ps | awk '{print '"$var"'}'

?
Brilliant MadeInGermany!! Also, could you please help me to how to use awk variable in this scenario?
# 4  
Old 11-18-2013
The $1,$2 is a piece of awk code.
You cannot run a string (as assigned to an awk variable) as awk code.
(Unless there were an eval built into awk.)
This following awk code interprets a given string as you intend:
Code:
var='1,2'
ps | awk -v v1="$var" 'BEGIN{n=split(v1,a,",")} {for(i=1;i<=n;i++) printf "%s\t",$(a[i]); print ""}'


Last edited by MadeInGermany; 11-18-2013 at 07:36 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 11-18-2013
This works.
ps | awk "{print $var}"

Single quote does not expand variable.
This User Gave Thanks to Jotne 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

Eval and get awk output assigned to variable

I want to do 2 things in single line that is evaluating a command to get return code and store $2 of awk if the command exit code is 0. eval "ade desc ${filename}@@/<branch_name> | grep Version | awk '{print $2}' 2>&1 1>/dev/null" ret=$? echo "$ret $val" if then ... (3 Replies)
Discussion started by: ezee
3 Replies

2. Shell Programming and Scripting

Eval in awk

Hi I am trying to remove duplicates on keys in a file and so far the below seems to work sort -t\| -k2,4 input.txt| awk -F'|' '{if (NR==1) print $0} {x=$2 $3 $4} NR>1 {if ($2 $3 $4 != y) {print $0}} {y=x}' and now I want to pass comma seperated column number list to the script and use... (3 Replies)
Discussion started by: zulfi123786
3 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

4. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

6. Linux

Please correct me with my code?

Dear All On my Linux server, I need to separate the individual logs coming from various modules concurrently. Please find below a sample of the logs: But when I run it, I am receiving the following error: -bash: -f1.log : command not found -bash: $LOGFILE : ambiguous redirect Can you... (4 Replies)
Discussion started by: hadimotamedi
4 Replies

7. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. Shell Programming and Scripting

Please correct the code

Hi, Some part of output: ================ $ hwmgr show scsi SCSI DEVICE DEVICE DRIVER NUM DEVICE FIRST HWID: DEVICEID HOSTNAME TYPE SUBTYPE OWNER PATH FILE VALID PATH ------------------------------------------------------------------------- 68: ... (10 Replies)
Discussion started by: mansa
10 Replies

9. UNIX for Advanced & Expert Users

can any body correct the code

#!/bin/ksh LOG_DATE=`date +%Y%m` export LOG_DATE cd D:/Informatica/Informatica_share/SrcFiles/l # mv SITE_ACTIVITY${LOG_DATE}*.CSV D:/Informatica/Informatica_share/SrcFiles/SITE_ACTIVITY${LOG_DATE}*.CSV # mv SITE_ACTIVITY20050914114546.CSV... (2 Replies)
Discussion started by: vivekanandarpat
2 Replies

10. Shell Programming and Scripting

Can any body correct the code

#!/bin/ksh LOG_DATE=`date +%Y%m` export LOG_DATE cd D:/Informatica/Informatica_share/SrcFiles/l # mv SITE_ACTIVITY${LOG_DATE}*.CSV D:/Informatica/Informatica_share/SrcFiles/SITE_ACTIVITY${LOG_DATE}*.CSV # mv SITE_ACTIVITY20050914114546.CSV... (6 Replies)
Discussion started by: vivekanandarpat
6 Replies
Login or Register to Ask a Question