awk - sub() function and NR==var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - sub() function and NR==var
# 1  
Old 12-23-2011
awk - sub() function and NR==var

Why does this work:
Code:
awk 'NR==3 sub(FS $NF,x)' inputfile

But not this:

Code:
var=3
awk -v i=$var 'NR==i sub(FS $NF,x)' inputfile

How would you do the same thing as the first line of code, but with a variable?
# 2  
Old 12-23-2011
try with

Code:
 
i="$var"

# 3  
Old 12-23-2011
@itkamaraj: That wouldn't make a difference. At least not on my GNU Bash.

@locoroco: The basic structure of an awk statement is pattern { action }.
1. You sub statement is not enclosed in curly braces.
2. What is the output of the awk statement? Its not printing anything. How did the first statement work?
3. sub (/old/, "new", target) --> this is how sub works. It'll replace the first occurrence of "old" with "new" in target. If target not specified, then it'll consider $0, the whole line.

Here's an example:
Code:
$ cat input
hello world
welcome to earth
$
$ x=2
$
$ awk -v var=$x 'NR==var { sub(/welcome/, "hello"); print }' input
hello to earth
$
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Cannot resolve $var in awk

My script ---------- for i in `cat n`;do export k=`echo "CSN: "$i` //combining CSN: and value from n echo "$k" awk ''{print "CSN: "$0;}'{_=29}_&&_--' file1|tail -1 >> file2 done In the above script i cannot able to resolve $k in awk command file n contains ------------ 0000 1111... (0 Replies)
Discussion started by: Mohana29_1988
0 Replies

3. Shell Programming and Scripting

awk: compose regex in variable and then use its contents like $0 ~ var

A question to the awk pundits: I was thinking about composing a regex in a variable and then use its contents like $0 ~ var instead of $0 ~ /r/. Sort of indirection. Did someone run into this? Is it possible at all? (3 Replies)
Discussion started by: RudiC
3 Replies

4. Shell Programming and Scripting

[awk] split file1 and save it as var from file2

I have 2 files: file_1: file_2: expected result: name file: "artV1" "artV2" etc. I have: but why don;t work save to file 'out'?? (3 Replies)
Discussion started by: ffresz
3 Replies

5. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

6. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

7. UNIX for Advanced & Expert Users

Function call not return value into var

hi friends, I writing a script, one of the function there is: ################################### ########### Return 1 if has subfolders and 0 otherwise ################## # Get one Argument - Folder name Has_Subfolders() { count=0 cd $1 for zont in `ls -l | grep drw | awk... (4 Replies)
Discussion started by: katzs500
4 Replies

8. UNIX for Dummies Questions & Answers

Function call not return value into var

hi friends, I writing a script, one of the function there is: ################################### ########### Return 1 if has subfolders and 0 otherwise ################## # Get one Argument - Folder name Has_Subfolders() { count=0 cd $1 for zont in `ls -l | grep drw | awk... (1 Reply)
Discussion started by: katzs500
1 Replies

9. Shell Programming and Scripting

use var in gsub of awk

Hi all, This problem has cost me half a day, and i still do not know how to do. Any help will be appreciated. Thanks advance. I want to use a variable as the first parameters of gsub function of awk. Example: { ... arri]=gsub(i,tolower(i),$1) (which should be ambraced by //) ... } (1 Reply)
Discussion started by: summer_cherry
1 Replies

10. Shell Programming and Scripting

Var substitution in awk - not working as expected

countA=`awk '/X/''{print substr($0,38,1)}' fName | wc -l` countB=`wc -l fName | awk '{print int($1)}'` echo > temp ratio=`awk -va=$countA -vc=$countB '{printf "%.4f", a/c}' temp` After running script for above I am getting an error as : awk: 0602-533 Cannot find or open file -vc=25. The... (3 Replies)
Discussion started by: videsh77
3 Replies
Login or Register to Ask a Question