Problem with Variable and AWK command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Variable and AWK command
# 1  
Old 03-29-2011
Problem with Variable and AWK command

Okay, so I am trying to use a count variable to reference the column of output sent from an echo statement. So I am trying to do this

Code:
 
#!/bin/bash
CURRENT=$PWD 
VAR=3
CHANGE=`echo $CURRENT | awk -F "/" '{ print \$$VAR }'`

This instead of giving me the third instance after the "/" gives me the whole path. Why is this? How can I use the current method or is there a better way to parse the PWD String?
# 2  
Old 03-29-2011
Code:
echo $CURRENT | awk -F "/" -v var=$VAR '{ print $var}'

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 03-29-2011
Bug Thank You!

Thank you that did the trick! Smilie
# 4  
Old 03-29-2011
Another way:
Code:
echo $CURRENT | awk -F "/" '{ print $'$VAR' }'

# 5  
Old 03-29-2011
So you understand why your first attempt didn't work single quotes cause shell to not expand any variables, you needed double quotes eg:

Code:
$ VAR=7
$ echo '{ print \$$VAR }'
{ print \$$VAR }
$ echo "{ print \$$VAR }"
{ print $7 }

edit: or put the variable outside the single quotes like yinyuemi did above.

Still, escaping every character that means something to the shell is a real pain (and makes the awk script even harder to read), using -v to get variable values into awk is really the way to go.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with awk instructions and bash variable

Hi everyone, I'm trying to write a small script to automatize row data treatment. However, I got some trouble with the awk command. I want to use awk to extract a define paragraph from a text file. The first and final lines are defined externally in two variables called debut and fin. I... (2 Replies)
Discussion started by: TeaTimeSF
2 Replies

2. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

awk variable substitution problem

Hi I am having a file like this ############################## j=1 while ] do temp_5=MODULE$j awk ' $1 ~ /'${!temp_5}'/ { do something }1' file1 > file2 ((j = j +1 )) done ################### Setting the variables like this (8 Replies)
Discussion started by: kshitij
8 Replies

4. Shell Programming and Scripting

Problem using shell variable in awk if condition

Hi friends, I'm having a bit of a problem using shell variable in an awk if statement. Please note that i'm using -v option as listed in many forums but I still don't get it working. Here's my code. Kindly help as I've gone crazy trying to work this out :wall: #!/bin/bash -xv ... (4 Replies)
Discussion started by: vishwas.s
4 Replies

5. Homework & Coursework Questions

problem with printing out variable in awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: couldn't print out stored variable in awk 2. Relevant commands, code, scripts, algorithms: i have in a... (5 Replies)
Discussion started by: ymc1g11
5 Replies

6. Shell Programming and Scripting

Problem using variable inside awk

HI, This is the code I am using: awk -v aaa="connect" 'BEGIN {IGNORECASE} /aaa/,/!/ {print NR}' bb This does not throw any error but it does not work. Pls help Thanks. (4 Replies)
Discussion started by: sudvishw
4 Replies

7. Shell Programming and Scripting

awk variable problem

Hi How are you people .. I am facing another problem regarding awk below is my code -bash-3.00$ export a=10 -bash-3.00$ echo $a 10 -bash-3.00$ echo "" | nawk -v var=$a ' { if(var=10) { print "abc" } else { print "def"} } ' abc -bash-3.00$ echo "" | nawk -v var=$a ' { if(var==10)... (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

8. Shell Programming and Scripting

Export command variable exporting problem

I have a txt file from which i am assiging a value to a variable using the code in script1 script1.sh export f=$(sed -n "/Freq *=/ s/.*= *//p" ${R_path}/output.txt) echo "$f" --------> this works in script2 ( which executes the script1) eval ./script1.sh if && ; then echo... (1 Reply)
Discussion started by: shashi792
1 Replies

9. Shell Programming and Scripting

awk problem when attributing query to a variable

Hello, I'm trying to make a script which reads the PID of a process using awk. I've read the thread in this forum where it is explained. However, the problem i'm having is when attributing that PID value to a variable. I'm using AIX 5.3. The command is the following: :/home/user1>ps -ef |... (2 Replies)
Discussion started by: mambo
2 Replies

10. UNIX Desktop Questions & Answers

problem while storing the output of awk to variable

Hi, i have some files in one directory(say some sample dir) whose names will be like the following. some_file1.txt some_file2.txt. i need to get the last modified file size based on file name pattern like some_ here i am able to get the value of the last modified file size using the... (5 Replies)
Discussion started by: eswarreddya
5 Replies
Login or Register to Ask a Question