Visit Our UNIX and Linux User Community


Awk ignoring variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk ignoring variable
# 1  
Old 10-16-2009
Awk ignoring variable

If anyone knows why $cc is ignored in the following awk.
When I use a value it works ok.
cc=`grep -a "ControlID" in.file | cut -c 375-378`

$cc is equal to "XXXX"

does not work:
cat in.file|awk -v cnt=0 -v replace=" " '/$cc/{if (cnt=="0"){sub(/$cc/,replace)};cnt++} {print}' > out.file

works:
cat in.file|awk -v cnt=0 -v replace=" " '/XXXX/{if (cnt=="0"){sub(/XXXX/,replace)};cnt++} {print}' > out.file
# 2  
Old 10-16-2009
Hi.

You're using a shell variable inside awk.

Code:
awk -v cnt=0 -v replace="    " '/'$cc'/ {if (cnt=="0"){sub(/$cc/,replace)};cnt++} {print}' in.file > out.file

or

awk -v cnt=0 -v cc=$cc -v replace="    " '$0 ~ cc {if (cnt=="0"){sub(/$cc/,replace)};cnt++} {print}' in.file > out.file

# 3  
Old 10-16-2009
You've blocked the shell from expanding $cc with the single quotes:
Code:
cat in.file|awk -v cnt=0 -v replace="NEWSTUFF" "/$cc/{if (cnt=="0"){sub(/$cc/,replace)};cnt++} {print}" > out.file

Of course, now you must be careful of any currency signs ($) in your awk(1) script.
# 4  
Old 10-16-2009
Thank Guys! I'm doing some testing. Looks like it works for the first occurance in the file when the cnt=0, but when I change the... if cnt=="1" then the second does not get change... I'll do more testing.
# 5  
Old 10-16-2009
Quote:
Originally Posted by megacoder
You've blocked the shell from expanding $cc with the single quotes:
Code:
cat in.file|awk -v cnt=0 -v replace="NEWSTUFF" "/$cc/{if (cnt=="0"){sub(/$cc/,replace)};cnt++} {print}" > out.file

Of course, now you must be careful of any currency signs ($) in your awk(1) script.
It's not a great idea to use double quotes to start and end an awk script:
Code:
$ echo something | awk "{print $NF}"   
something
 
$ echo something | awk "{ if ($1 == "something") A=$1 }"
awk: { if ( == something) A= }
awk:        ^ syntax error
 
$ echo something | awk "{print "Here is " $NF}" 
awk: cmd. line:1: {print Here
awk: cmd. line:1:            ^ unexpected newline or end of string
 
$ echo something | awk "{print 'Here is ' $NF}"
awk: {print 'Here is ' }
awk:        ^ invalid char ''' in expression
 
$ echo something | awk "{print \"Here is \" $NF}"
Here is

Use single quotes and declare the shell variable to awk either as
Code:
awk -v cc=$cc '...' file1 file2 ... etc.
or
awk '...' cc=$cc file1 dd=$dd file2 ... etc.

depending on your preference and whether you have a BEGIN clause or not.

Last edited by Scott; 10-16-2009 at 07:08 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #472
Difficulty: Easy
Lisa of the K-Pop group Blackpink is the brand ambassador for HipHop Linux.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3. What is the best way to print my column of interest... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

awk search for max and min while ignoring special character

I am trying to get a simple min/max script to work with the below input. Note the special character (">") within it. Script awk 'BEGIN{max=0}{if(($1)>max) max=($1)}END {print max}' awk 'BEGIN{min=0}{if(($2)<min) min=($2)}END {print min}' Input -122.2840 42.0009 -119.9950 ... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

5. Shell Programming and Scripting

Compute average ignoring outliers of different segments within a dat file using awk

I have data files that look like this, say data.txt 0.00833 6.34 0.00833 6.95 0.00833 7.08 0.00833 8.07 0.00833 8.12 0.00833 8.26 0.00833 8.70 0.00833 9.36 0.01667 20.53 0.01667 6.35 0.01667 6.94 0.01667 7.07 0.01667 8.06 0.01667 8.10 0.01667 8.25 0.01667 8.71 0.01667 9.31... (7 Replies)
Discussion started by: malandisa
7 Replies

6. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

7. Shell Programming and Scripting

AWK - Ignoring White Space with FS

I have an AWK script that uses multiple delimiters in the FS variable. FS="+" My awk script takes a file name such as this: 12345_smith_bubba_12345_20120215_4_0.pdf and parses it out based on the under score. Each parsed field then has some code for data validation etc. This script has... (12 Replies)
Discussion started by: reno4me
12 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Using awk to obtain minimum of each column (ignoring zeros)

Hi, I have a wide and long dataset which looks as follows: 0 3 4 2 3 0 2 2 ... 3 2 4 0 2 2 2 3 ... 0 3 4 2 0 4 4 4 ... 3 0 4 2 2 4 2 4 ... .... I would like to obtain the minimum of each column (ignoring zero values) so the output would look like: 3 2 4 2 2 2 2 2 I have the... (3 Replies)
Discussion started by: kasan0
3 Replies

9. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

10. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

Featured Tech Videos