Gawk Question


 
Thread Tools Search this Thread
Operating Systems SCO Gawk Question
# 1  
Old 09-12-2014
Gawk Question

I am trying to use gawk to search a file and put the second value of the string into a string.

Code:
          gawk -F: '$1~/CXFR/ {print $2}' go.dat

go.dat
====================
Code:
HOME      :/
CTMP       :/tmp
CUTL       :/u/rdiiulio/bin
CWRK      :/u/work
CXFR       :/u/xfer

====================

So when I type the command manually on the screen, I get the string
'/u/xfer'. However, when I use a shell script to do the same thing, it does not work. Here is the command in the shell script:

Code:
          gawk -F: '$1~/$chkstr/ {print $2}' go.dat

I set the variable 'chkstr' to '$1' and it does not work. Anybody have a answer to this? Any help will be very much appreciated.

Last edited by Don Cragun; 09-12-2014 at 10:25 PM.. Reason: Add CODE tags.
# 2  
Old 09-12-2014
Quote:
Originally Posted by trolley
I am trying to use gawk to search a file and put the second value of the string into a string.

Code:
          gawk -F: '$1~/CXFR/ {print $2}' go.dat

go.dat
====================
Code:
HOME      :/
CTMP       :/tmp
CUTL       :/u/rdiiulio/bin
CWRK      :/u/work
CXFR       :/u/xfer

====================

So when I type the command manually on the screen, I get the string
'/u/xfer'. However, when I use a shell script to do the same thing, it does not work. Here is the command in the shell script:

Code:
          gawk -F: '$1~/$chkstr/ {print $2}' go.dat

I set the variable 'chkstr' to '$1' and it does not work. Anybody have a answer to this? Any help will be very much appreciated.
You can't mix shell variables and awk variables inside an awk script. And, a shell variable won't be expanded inside single quotes.

Making some wild guesses, try:
Code:
chkstr="$1"
gawk -v chkstr="$chkstr" -F: '$1~/chkstr/ {print $2}' go.dat

# 3  
Old 09-12-2014
Tried your suggestion and it did not work.
# 4  
Old 09-12-2014
Quote:
Originally Posted by trolley
Tried your suggestion and it did not work.
That sure helps us track down the problem...

Show us your exact script.

Show us the exact command you used to invoke it.

On the other hand, it looks like I left in too much of your original code. Try:
Code:
chkstr="$1"
gawk -v chkstr="$chkstr" -F: '$1~chkstr {print $2}' go.dat

(i.e., remove the / characters surrounding chkstr in the awk script).

Last edited by Don Cragun; 09-12-2014 at 11:16 PM.. Reason: Add possible fix.
# 5  
Old 09-13-2014
That worked, thank you Don.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk and regexp

Hello, This is a problem I've worked on a while and can't figure out. There is a file.txt ..some stuff.. ] ] ..some stuff.. The Awk program is trying to extract the year portion of the birth and death ("98: and "2nd C.") using the below technique #!/bin/awk @include... (5 Replies)
Discussion started by: Mid Ocean
5 Replies

2. SCO

Need help with gawk

I am trying to use gawk to search a file and put the second value of the string into a string. gawk -F: '$1~/CXFR/ {print $2}' go.dat Below is the file 'go.dat' ==================== HOME :/ CTMP :/tmp CUTL :/u/rdiiulio/bin CWRK :/u/work CXFR :/u/xfer ... (1 Reply)
Discussion started by: trolley
1 Replies

3. Shell Programming and Scripting

Gawk and sed question

Can anyone tell me what this script does? sort file1.txt | awk -F, -f event.gawk Thank you. (2 Replies)
Discussion started by: dee campbell
2 Replies

4. Programming

need help with gawk script

hi i've already created this script. When I execute the script it takes the argument and compares it to the 3rd column of the script. What I was wondering if I could get some help with is. I want to add another column to the script and it will be the result of a set number for example, (2000 - 3rd... (3 Replies)
Discussion started by: gengar
3 Replies

5. Shell Programming and Scripting

Gawk filter

People, Ive been trying to make a script but i just cant figure it out. Problem/ Case: I have a logfile.txt that contains data. The only two things i need to filter on = $1 (date), $6 (errorcode). In the script i am trying to make, u need to fill in a date. So he searches on that date... (11 Replies)
Discussion started by: Pow3R
11 Replies

6. Shell Programming and Scripting

Gawk Help

Hi, I am using the script to print the portion of the file containing a particular string. But it is giving error "For Reading (No such file or directory). I am using cygwin as unix simulator. cat TT35*.log | gawk -v search="12345678" ' /mSOriginating /,/disconnectingParty/ { ... (1 Reply)
Discussion started by: vanand420
1 Replies

7. Shell Programming and Scripting

gawk and bash

Hi. I'm having trouble using gawk within a bash script and I can't figure out why. I have a command that takes in a data file with two columns, the first one numbers and the second words. My code takes each line, and prints the word its corresponding number of times. The code works from the... (2 Replies)
Discussion started by: cdislater
2 Replies

8. Shell Programming and Scripting

gawk script

Hey guys need your help with an gawk script... here's what I have so far gawk '^d/ {printf "%-20s %-10s %-10s %-10s %-4s%2s %5s\n",$9,$1,$3,$4,$6,$7,$8}' ls.kbr The file ls.kbr is a capture of 'ls-al' What I want gawk to do is: 1) Find only directories (this is working) 2) skip lines... (2 Replies)
Discussion started by: zoo591
2 Replies

9. Shell Programming and Scripting

gawk will work or not ?

Hai I am using bash-2.03$ bash --version GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) I am not able to use gawk command its showing command not found , why ? Eg: awk 'NR==1' fix.txt | gawk 'BEGIN { FIELDWIDTHS = "3 2" } { printf($1"|"$2); }'... (3 Replies)
Discussion started by: tkbharani
3 Replies

10. Shell Programming and Scripting

gawk HELP

I have to compare records in two files. It can be done using gawk/awk but i am unable to do it. Please help me File1 ABAAAAAB BC asa sa ABAAABAA BC bsa sm ABBBBAAA BC bxz sa ABAAABAB BC csa sa ABAAAAAA BC dsa sm ABBBBAAB BC dxz sa File 2 ABAAAAAB BC aas ba ABAAAAAB BC asa sa... (6 Replies)
Discussion started by: sandeep_hi
6 Replies
Login or Register to Ask a Question