Insert external variable in a AWK pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert external variable in a AWK pattern
# 1  
Old 11-13-2009
CPU & Memory Insert external variable in a AWK pattern

Dear all,

¿How can i insert a variable in a AWK pattern?

I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain.
Code:
cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/  { print "\n"$0 }'

I know that this command makes right what i want to do, but mi intention is make a generic script whit a input parameter instead of 'tux' constant. Can you help me please?

Thanks

Last edited by radoulov; 11-13-2009 at 06:09 AM.. Reason: Added code tags.
# 2  
Old 11-13-2009
Something like this ?

Code:
awk -v var_name="value"  '##use the variable as you wish' input_filename.txt

# 3  
Old 11-13-2009
No way, if i put this in the command does not works, it only takes the variable in the print field.

Doesn't works:

Code:
cat file.txt  | awk -v var_name=$id 'BEGIN {RS="\\n\\n"} /var_name/  { print "\n"$0 }'

Works, but i dont want use the variable there

Code:
cat file.txt  | awk -v var_name=$id 'BEGIN {RS="\\n\\n"} /tux/  { print var_name "\n"$0 }'


Last edited by radoulov; 11-13-2009 at 06:08 AM.. Reason: Use code tags, please!
# 4  
Old 11-13-2009
You need:

Code:
$0 ~ var_name

not:

Code:
/var_name/


And in this case you don't need to call external commands like cat.
# 5  
Old 11-13-2009
Still not working...

Code:
cat file.txt | awk 'BEGIN {RS="\\n\\n"} $0 ~ '$id'  { print "\n"$0 }'

cat file.txt | awk 'BEGIN {RS="\\n\\n"} $0 ~ $id  { print "\n"$0 }'

cat file.txt | awk -v var_name=$id 'BEGIN {RS="\\n\\n"} $0 ~ var_name  { print "\n"$0 }'


Last edited by radoulov; 11-13-2009 at 06:24 AM.. Reason: added code tags
# 6  
Old 11-13-2009
Quote:
Originally Posted by antuan
Still not working...

cat file.txt | awk 'BEGIN {RS="\\n\\n"} $0 ~ '$id' { print "\n"$0 }'
[...]
I said:

Code:
$0 ~ var_name

not:

Code:
$0 ~ $var_name

And you need to set the variable for AWK as the previous poster pointed out:

Code:
% print -l a b | awk -vvar=a '$0 ~ var'
a
% print -l a b | awk '$0 ~ var' var=a
a


...

In your case it would be:

Code:
awk -vid=$id 'awk 'BEGIN {RS="\\n\\n"}  $0 ~ id  { print "\n" $0 }' file.txt

Or probably just:

Code:
awk '$0 ~ id' RS= id=$id file.txt


Last edited by radoulov; 11-13-2009 at 06:32 AM..
# 7  
Old 11-13-2009
i tested all cases.
note that $id is an external variable, and its what i want to put as AWK pattern.. Should I use the -v option ? Please complete my command, so I will understand you better. thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

2. Shell Programming and Scripting

awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line. I want the pattern to be the beginning of the new line. I thought sed or awk could do this, but everything I try in sed gives me a "sed... (2 Replies)
Discussion started by: Drenhead
2 Replies

3. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

4. Shell Programming and Scripting

Help needed with awk external variable

I'm trying to get the universities result data into different file, where the $9 contains unversity field and field7,4 & 5 contains the keys to sort the students by marks. How to use uni variable to match against $9 inside awk? c=0 for uni in `cat /tmp/global_rank| awk -F ',' '{print... (1 Reply)
Discussion started by: InduInduIndu
1 Replies

5. Shell Programming and Scripting

[awk] - how to insert an external variable

I want to incorporate the variable in the for statement as a column of my processed file. In the INCORRECT example below, it is $i which corresponds to the i in my for loop: for i in x86_64 i686; do awk '{ print $1" "$4" "$5" "$i }'awk $file-$i > processed-$i.log doneThanks! (3 Replies)
Discussion started by: graysky
3 Replies

6. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

7. UNIX for Dummies Questions & Answers

insert variable into awk

I'm trying to insert a filename into awk. filename="12345.wmv" I have tried this: awk '/$filename/{print $0}' infile and this: awk -v fn=$filename '/$fn/{print $0}' infile How do I insert the variable into awk? (1 Reply)
Discussion started by: locoroco
1 Replies

8. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

9. Shell Programming and Scripting

If statement in awk with external variable

So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have. # !/bin/sh echo "Enter client ID" read ID echo "Enter month (01, 02, 03)" read month echo "Enter day (03, 15)" read day echo "Enter Year (07, 08)" read... (6 Replies)
Discussion started by: doublejz
6 Replies

10. Shell Programming and Scripting

awk help (external variable)

i need help with an awk question. i am looking to have an external variable be defined outside of awk but used in awk. so if we have fields $1, $2, $3 so on and so forth, i would like to be able to dictate what field is being printed by something like $. so if i had a counter called test, make it 3... (8 Replies)
Discussion started by: pupp
8 Replies
Login or Register to Ask a Question