awk syntax(partial) in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk syntax(partial) in variable
# 1  
Old 04-19-2012
awk syntax(partial) in variable

if a variable has part of awk syntax stored in it. for eg: x=if($1>100)
can we substitute this variable in an awk statement.

based on above requirement can we execute something like:
Code:
awk '{x print $1}' infile

# 2  
Old 04-19-2012
No, awk doesn't have something like an "eval" command.
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 04-19-2012
As far as i know you cannot assign variables in such fashion.

Correct syntax would be :
Code:
awk 'BEGIN {a=100} $1>100 { print $1 } $1<=100 { print $1 " lower then 100" }'

or using conditional operator ..
Code:
awk ' {a=100; print ( $1 > a ) ? $1 : $1" lower then 100" }' input

Perhaps you require a function ?
Code:
function testme (field,num)
{
	print ( field > num ) ? field : field " is less then " num
}
{
testme ($1,100)
}

# 4  
Old 04-19-2012
Isn't this what your after:
Code:
x='if($1>100)'
awk '{'$x' print $1}' infile

# 5  
Old 04-19-2012
Quote:
Originally Posted by Chubler_XL
Isn't this what your after:
Code:
x='if($1>100)'
awk '{'$x' print $1}' infile

Chubler is right. You could "dynamically" create the awk program in your shell. I would suggest quoting that expansion though.

Code:
$ x='if ($1 > 100)'
$ printf "%s\n" 99 101 3 | awk '{'$x'print $1}'
awk: cmd. line:1: {if
awk: cmd. line:1:    ^ unexpected newline or end of string
$ printf "%s\n" 99 101 3 | awk '{'"$x"'print $1}'
101

You don't see this done often with code, maybe with variables... awk has a way to pass those in from the shell though:

Code:
mute@atom:~$ x=100
mute@atom:~$ printf '%s\n' 99 101 3 | awk -v "num=$x" '$1 > num'
101

# 6  
Old 04-20-2012
thanks @neutronscott your suggestion works perfectly.

I have a slightly different quest. In a recent post a poster wanted to add columns and something like Fibonacci but not exactly.
Consider this data set:
Code:
1,2,3,4
2,3,4,5
3,4,5,6
4,5,6,7
5,6,7,8
6,7,8,9,
7,8,9,10
8,9,10,11
9,10,11,12

expected output is:
Code:
1 2 3 4 
3 5 7 9 
6 9 12 15 
10 14 18 22 
15 20 25 30 
21 27 33 39 
28 35 42 49 
36 44 52 60 
45 54 63 72

If I use your suggestion and do this
Code:
$z='x1" "x2" "x3" "x4'
$y='x1+=$1;x2+=$2;x3+=$3;x4+=$4'
$awk -F, '{'"$y"'} {print '"$z"'}'  infile

I get desired results.

But if I try to construct "z" and "y" inside "awk" and then substitute them, it's not working:
Code:
awk -F, '{y="";z="";for(i=NF;i>=1;i--) {y="x"i"+=$"i";"y  ;z="x"i"\" \""z}  {'"$y"'} {print '"$z"'} }'  infile

the above command constructs "y" and "z" right, but doesn't expands as external variable does.

Last edited by 47shailesh; 04-20-2012 at 12:47 AM.. Reason: sentence correction;input
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk unique count of partial match with semi-colon

Trying to get the unique count of the below input, but if the text in beginning of $5 is a partial match to another line in the file then it is not unique. awk awk '!seen++ {n++} END {print n}' input 7 input chr1 159174749 159174770 chr1:159174749-159174770 ACKR1 chr1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk partial string match and add specific fields

Trying to combine strings that are a partial match to another in $1 (usually below it). If a match is found than the $2 value is added to the $2 value of the match and the $3 value is added to the $3 value of the match. I am not sure how to do this and need some expert help. Thank you :). file ... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Partial variable substitution in script

I have a script. filecreatenew () { touch /usr/src/$1_newfile.txt var=$1 echo $var touch /usr/src/$var_newfile_with_var.txt } filecreatenew myfile Its creating file /usr/src/myfile_newfile.txt as the variable $1 is correctly used. When $ is... (2 Replies)
Discussion started by: anil510
2 Replies

4. Shell Programming and Scripting

Cleaner way to use shell variable in awk /X/,/Y/ syntax?

$ cat data Do NOT print me START_MARKER Print Me END_MARKER Do NOT print me $ cat awk.sh start=START_MARKER end=END_MARKER echo; echo Is this ugly syntax the only way? awk '/'"$start"'/,/'"$end"'/ { print }' data echo; echo Is there some modification of this that would work? awk... (2 Replies)
Discussion started by: hanson44
2 Replies

5. Shell Programming and Scripting

awk if one liner syntax for equating with a string variable

I would like to have help with syntax for using a string varaibles inside if and else in a awk one liner. Eg. I would like to say, list all the filenames that have been modified in a particular month(present in a string variable) or list all the filenames whose owner is $owns and owner group is... (3 Replies)
Discussion started by: Prev
3 Replies

6. Shell Programming and Scripting

Help with awk and accessing variable values within the syntax

I'm writing a bash script, and I am having trouble with this line: awk '{print "url = \"http://www.example.com/directory/$type/"$1"\""}' input.file > output.file Within the URL being printed, I wish the value of the variable "$type" to be printed, after being read from user input from the... (2 Replies)
Discussion started by: meridionaljet
2 Replies

7. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

8. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

9. Shell Programming and Scripting

Partial average of a column with awk

Hello, Let's assume I have 100 files FILE_${m} (0<m<101). Each of them contains 100 lines and 10 columns. I'd like to get in a file called "result" the average value of column 3, ONLY between lines 11 and 17, in order to plot that average as a function of the parameter m. So far I can compute... (6 Replies)
Discussion started by: DMini
6 Replies

10. Shell Programming and Scripting

awk partial match and filter records

Hi, I am having file which contains around 15 columns, i need to fetch column 3,12,14 based on the condition that column 3 starts with 40464 this is the sample data how to achieve that (3 Replies)
Discussion started by: aemunathan
3 Replies
Login or Register to Ask a Question