Saving an AWK match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Saving an AWK match
# 1  
Old 03-16-2011
Saving an AWK match

Is it possible to save the result of an AWK match to use later in a BASH script.

Thanks,
Jordon
# 2  
Old 03-16-2011
Hi jhirshon,

Within awk script it could be like this:
Code:
echo "Country
State
City" | awk '/State/{Var=$0;print Var}'
State

Outside awk program, you could store the result of an awk program in a variable $Var and after print its content like this:
Code:
Var=$(echo "Country
State
City" | awk '/State/{print}')
echo "My awk match is" $Var
My awk match is State

Hope it helps.
# 3  
Old 03-17-2011
Thanks cgkml;

I'm trying to save the result from AWK match(s,r) to use later in a script. Should your example work for that?

Thanks again,
Jordon
# 4  
Old 03-18-2011
Quote:
Originally Posted by jhirshon
Thanks cgkml;

I'm trying to save the result from AWK match(s,r) to use later in a script. Should your example work for that?

Thanks again,
Jordon
Hi Jordon,

Yes, a example storing the output of match(s,r) in a variable coud be like this:
Code:
Var=$(echo "How are you?, I hope you are fine" | awk '{print match($0,/hope/)}')
echo "The regex \"hope\" within string begins at position" $Var
The regex "hope" within string begins at position 17

or
Code:
Var=$(echo "How are you?, I hope you are fine" | awk '{print match($0,/hope/),"and its span is",RLENGTH}')
echo "The regex \"hope\" within string begins at position" $Var
The regex "hope" within string begins at position 17 and its span is 4


or
Code:
Var=$(echo "How are you?, I hope you are fine" | 
awk '{match($0,/hope/); print "The regex \"hope\" within string begins at position",RSTART,"and its span is",RLENGTH}')
echo $Var
The regex "hope" within string begins at position 17 and its span is 4

Hope it helps.

Regards
# 5  
Old 03-19-2011
cgkmal;

Thanks. Your response was very helpful..

Jordon
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk saving field of first file into array

Hello guys, I just start trying out AWK and encounter a problem, I try to think a bit but seems my way is incorrect. I have two input file, with the first file has only one field, the second file has 3 fields, I suppose to do stuffs to them by writing an awk program, kinda sort them out. Since I... (15 Replies)
Discussion started by: RozenKristal
15 Replies

5. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

6. Shell Programming and Scripting

Saving file content in arrays using AWK

Hi, im new to shell scripting. i have a query for which i have searched your forums but coulndt get what i need. i have a file that has two records of exactly the same length and format and they are comma seperated. i need to save the first and the second columns of the input file to 2 different... (11 Replies)
Discussion started by: atikan
11 Replies

7. Shell Programming and Scripting

Saving ranges using awk

Hi All, Is there a way to save a range in variable for later printing? for example write somthing like this: awk ' /pattern1/,/pattern2/{f=range} /pattern3/{print f} ' I don't know excatly what "range" could be but is there a way to do this? (8 Replies)
Discussion started by: ghoda2_10
8 Replies

8. Shell Programming and Scripting

subtracting 1.5 from column using awk and saving the changes

# foreach sub ( sub001 ) sub=sub001 cd /mnt/stor/smith/recog/$sub/event_files/timecorrected/ awk '{$1-1.5}' $sub_CR end What im trying to do is: 1. open a 1D file that consists of lists of integers in rows a columns 2. subtract 1.5 from each integer in the first column 3. save the file... (1 Reply)
Discussion started by: ac130pilot
1 Replies

9. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. UNIX for Dummies Questions & Answers

Extracting lines and saving - awk

Hi All, I am trying to extract lines bsed on pattern matching../mp straight-flow/ Extracted output should be saved in meta_string , but the code is not working in that manner,saving repeated lines. can anyone please suggest where am i going wrong. /mp straight-flow/ {... (6 Replies)
Discussion started by: madhaviece
6 Replies
Login or Register to Ask a Question