awk search using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search using variable
# 1  
Old 07-13-2015
awk search using variable

Hi Forum.

I have the following script that I would like to use a variable to search using awk but it's not working as expected:

Code:
# Define working variables
token_search_string=536088
token_search_length=16

This code example works by hardcoding 536088 in the string search:
awk -v token_search_string=$token_search_string -v token_search_length=$token_search_length '/536088/ {
    match($0, /token_search_string/); print substr($0, RSTART, token_search_length);
}' abc.txt


This code example doesn't work if I reference the token_search_string variable
awk -v token_search_string=$token_search_string -v token_search_length=$token_search_length '/token_search_string/ {
    match($0, /token_search_string/); print substr($0, RSTART, token_search_length);
}' abc.txt

What am I missing here?

Thanks for your help.
# 2  
Old 07-13-2015
Quote:
Originally Posted by pchang
Hi Forum.

I have the following script that I would like to use a variable to search using awk but it's not working as expected:

Code:
# Define working variables
token_search_string=536088
token_search_length=16

This code example works by hardcoding 536088 in the string search:
awk -v token_search_string=$token_search_string -v token_search_length=$token_search_length '/536088/ {
    match($0, /token_search_string/); print substr($0, RSTART, token_search_length);
}' abc.txt


This code example doesn't work if I reference the token_search_string variable
awk -v token_search_string=$token_search_string -v token_search_length=$token_search_length '/token_search_string/ {
    match($0, /token_search_string/); print substr($0, RSTART, token_search_length);
}' abc.txt

What am I missing here?

Thanks for your help.
Awk does not do variable expansion within regex //
It is, literally, looking for the characters: token_search_string

Try
Code:
awk -v var=$var '$0 ~ var'

when you need to search in that way.

Try
Code:
awk -v token_search_string=$token_search_string -v token_search_length=$token_search_length '{
    match($0, token_search_string); if( RSTART>0 ){print substr($0, RSTART, token_search_length)}
}' abc.txt


Last edited by Aia; 07-13-2015 at 09:25 PM..
# 3  
Old 07-13-2015
As match returns zero when no match is found and position when one is, you could do:

Code:
awk -v token_search_string="$token_search_string" -v token_search_length=$token_search_length '
match($0, token_search_string)  {
    print substr($0, RSTART, token_search_length)
}' abc.txt

# 4  
Old 07-13-2015
Thanks Guys for all your suggestions.
# 5  
Old 07-17-2015
Quote:
Originally Posted by Chubler_XL
As match returns zero when no match is found and position when one is, you could do:

Code:
awk -v token_search_string="$token_search_string" -v token_search_length=$token_search_length '
match($0, token_search_string)  {
    print substr($0, RSTART, token_search_length)
}' abc.txt

I have been told that my token_search_string has to be a list of entries.

How can I define the token_search_string containing a list of values such as 536088, 547698 and still have the above awk script work?

Do I need to loop thru the list of values and call the awk script for each value?

Thanks.
# 6  
Old 07-17-2015
In the shell
Code:
token_search_string='536088|547698'

These 2 Users Gave Thanks to Aia For This Post:
# 7  
Old 08-05-2015
This is my updated code but having an issue:

Code:
token_search_string='536088|536000|536001'
token_search_length=16

awk -v token_search_string="$token_search_string" -v token_search_length=$token_search_length '
match($0, token_search_string)  {
print substr($0, RSTART, token_search_length) }'
/data/data2/staging/CREDIT_CARDS/TSYS/V888test.20150101.12010

Code:
Input file "V888test.20150101.12010" contains the following record with the search strings italics bolded:

000000000501AT31 0600 0000000 C  2015140 19066035360887343045008   5360887343045008   75360885140140111111121 DBT N      BOPURCHASE
          CAN0000000000000000                                                  00D        N   PR        00
    00000000000120000000000000000 0000000000    00000    1750010100010001000000006170000000124000112422                        N1       0000000 0000000008
2.48 20181350000 2015140 0000000010100  00000000101.00 00000000000.00 20151400000 0000000000000 0368968 2015140 00.000 0.0000 0000000 0000000 N         00
00000.00            0000000 00000000000.00CAN00103                                 0            0   00000001.000000000 00000000.0000000000 00000000001.01
             N        0000

Code:
Output is extracting value 5360887343045008 but not 5360885140140111.

How can I update the code so that it will extract multiple search strings within same record?

Please help.

Thanks

Last edited by pchang; 08-05-2015 at 11:36 AM.. Reason: more details
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. Shell Programming and Scripting

Search for string in column using variable: awk

I'm interested to match column pattern through awk using an external variable for data: -9 1:751343:T:A -9 0 T A 0.726 -5.408837e-03 9.576603e-03 7.967536e-01 5.722312e-01 -9 1:751756:T:C -9 0 T C 0.727 -5.360458e-03 9.579447e-03 7.966977e-01 5.757858e-01... (7 Replies)
Discussion started by: genome
7 Replies

3. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

4. UNIX for Advanced & Expert Users

Pass variable to awk command search string

I must have forgot how to do this, but, I am attempting to enter a variable into an awk / gawk search pattern. I am getting a value from user input to place in a specific section of a 132 character string. my default command is .... gawk --re-interval '/^(.{3}P .{4}CYA.{8}1)/' ... (3 Replies)
Discussion started by: sdeevers
3 Replies

5. Shell Programming and Scripting

How to search, replace and multiply variable within awk?

I have a file that reports the size of disks GB's or TB's - I need the file to report everything in MB's. Here is an extract of the file - the last column is the disk size. 19BC 2363 20G 1AA3 2363 2.93T 1A94 2363 750G Whenever I come across a G I want to delete the G and multiply by... (2 Replies)
Discussion started by: kieranfoley
2 Replies

6. Shell Programming and Scripting

Search the shell variable inside awk

Hai, I need to search a variable inside a file using awk in AIX. for ex: file.txt one two three four five i need to get the lines with two awk '/two/ {print}' file.txt But i need to change the two as a variable since it would be changed on runtime..i tried like below..nothing give... (3 Replies)
Discussion started by: jesu
3 Replies

7. Shell Programming and Scripting

Using a script variable in awk search patterns

Hi all, In a script like : job_date=.... ls -l 2>/dev/null | awk -v var =$job_date ' /Name\.Version\.+\.xml$/ { How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.+\.xml$/" Thanks in advance (12 Replies)
Discussion started by: abhinav192
12 Replies

8. Shell Programming and Scripting

use awk to search a file using variable given from user

Hi! I want to print selected lines from a file using awk.The file contains 6 columns(dates,time,title,description,location and attendees). So i ask the user to insert the date that he wants to display and then search the file for the matching dates so i can print the line. echo Give the date... (7 Replies)
Discussion started by: DDoS
7 Replies

9. Shell Programming and Scripting

awk using env variable as search argument

Hello, I have a file like was123##abcdefg abddef was123##xuzaghg agdfgg was133##CGHAKS DKGJG from the file i need to print the line after ## where the serach value is passed by an env variable called luster (which is currently set to was123): i tried using the below code but it... (7 Replies)
Discussion started by: amit1_x
7 Replies

10. Shell Programming and Scripting

Using awk to search variable length records

New to awk and need some help. I have a script that I would like to make more compact. I want to read a file and grab every field, from every record, except the last field. The records are variable length and have varying number of fields. A record will have at least two fields, but can have... (9 Replies)
Discussion started by: synergy_texas
9 Replies
Login or Register to Ask a Question