Eval in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Eval in awk
# 1  
Old 07-18-2013
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 shell variable to hold the same and pass that to awk using the below, but it doesnt seem to work.
Code:
z=$1

echo \$$z|sed s/','/' $'/g

collist=`echo $2|sed s/'.'/' $'`

sort -t\| -k2,4 input.txt| awk -v awkcolist="$collist" -F'|' '{if (NR==1) print $0} {x=print awkcolist} NR>1 {if ($2 $3 $4 != y)  {print $0}} {y=x}'

The reason is awkcolist is evaluated to '$2 $3 $4' and not the actual values of those columns.

Please help

[Code tags doesnt seem to work for some reason on my browser at this point of time]

Thanks

-Zulfi

Last edited by Scott; 07-18-2013 at 04:58 AM.. Reason: Code tags
# 2  
Old 07-18-2013
Without samples it's difficult to guess what you want to do. How about removing the $-signs from awkcolist, splitting it into an array, and using a for loop to run through the array elements to construct your x variable?
# 3  
Old 07-18-2013
Hi,

In the above column 2,3,4 of the file input.txt are key columns and I have to remove duplicates on these columns. Once that is done I need to make the script generic so that I will accept comma seperated list of columns and file name and script should remove duplicates over the keys.
The first code fragment is working fine when the key column numbers are har coded inside awk but when I try to pass the column list as a variable trying to imitate the first code fragment it isn't working.
# 4  
Old 07-18-2013
Quote:
Originally Posted by zulfi123786
[Code tags doesnt seem to work for some reason on my browser at this point of time]
You do not need browser support for this basic, simple, easy feature.

Type the letters [, c, o, d, e, ], !, [, /, c, o, d, e, ]

Voila, you have a ! inside code tags, like this:

Code:
!

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

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

How to correct this awk code without eval?

Hi everyone, The following piece of awk code works fine if I use eval builtin 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 ps | awk -v v1=$var '{print v1}' # output is $1,$2 ps |... (4 Replies)
Discussion started by: royalibrahim
4 Replies

3. Shell Programming and Scripting

Eval

thank you (35 Replies)
Discussion started by: ratnalein88
35 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... (13 Replies)
Discussion started by: vivek d r
13 Replies

5. 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

6. Shell Programming and Scripting

Help on eval please

Hello All, Since my variables are nested I use eval to populate the data. I have an ambiguity here when eval is used along with & say I have the below variable url="www.unix.com" , this come from function call as argument. I want to take this into another variable say... (6 Replies)
Discussion started by: sathyaonnuix
6 Replies

7. 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

8. Shell Programming and Scripting

eval

hi all, Am trying to add some code to a ksh script and i dont understand how an eval function is used : _var=$1 _conceal=$2 eval _val=\$${_var} can someone shed some light on what the eval function in the above context means/does ?? thanks. (4 Replies)
Discussion started by: cesarNZ
4 Replies

9. Shell Programming and Scripting

eval help

I am trying to expand the variable $user in my alias command and tried several variations of eval but can't seem to get it to work. The end result should be either: oracle_user='sudo su - oracle ' or oracle_user='sudo su - oracle1 ' user=$(grep '^oracle:' /etc/passwd | cut... (5 Replies)
Discussion started by: BeefStu
5 Replies

10. Shell Programming and Scripting

EVal

Hi All, I'm running some encrypted data through a script I wrote. In order to do this, I'm using eval to resolve some of my variables. At the moment, when I use eval to resolve, it strips out some of my encrypted values, and totally drops some others. For example if I have the value ab1"3 it drops... (1 Reply)
Discussion started by: Khoomfire
1 Replies
Login or Register to Ask a Question