Help with awk issue


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with awk issue
# 1  
Old 05-29-2015
Help with awk issue

OK, so I am trying to use awk as a method of accessing a table stored in a file to then provide the capability of a look up table.

The table is stored in a file named "/Users/jhaney/Desktop/assetTypeMapping.tsv" and looks like this:

Code:
aCategory	aLetter	aNumber
AssetCat1	A	123		
AssetCat2	B	124				
AssetCat3	C	125

The file is tab delimited because there is all likelihood that some of the fields will contain space characters and records are separated by UNIX new line characters.

-----------------------
proof of concept:

Code:
#!/bin/sh
#basefilename= echo | basename "$filePath"
luVal="WebAssets"
inputFile="/Users/jhaney/Desktop/assetTypeMapping.tsv"
myResult=`echo | awk -v lookupVal="$luVal" '$1 == lookupVal { print $2 }' "$inputFile"`
echo "$myResult"

This script works great and does exactly what I want it to do.

-----------------------
The problem:

As aforementioned, I would like the freedom of allowing space characters in the data table. However, awk considers spaces to be field separators.

I have seen examples of how to set the field separator:

Code:
awk 'BEGIN { FS = "tab character would go here" } ; { print $2 }'

But I just can't figure out how to combine the two ideas together.

I tried:
Code:
myResult=`echo | awk 'BEGIN { FS = "	" } ; { -v lookupVal="$luVal" $1 == lookupVal { print $2 }}' "$inputFile"`

But I keep getting awk syntax errors no matter how I combine the args.


So, basically, I want to search the first field of each line for "lookupVal" and, when found, return the second (or third, or fourth) field in that record.



Can anyone set me straight?

Last edited by Scott; 05-29-2015 at 03:16 PM.. Reason: Please use code tags
# 2  
Old 05-29-2015
FS is a variable as any other in awk, so you can set it like any other, e.g. your lookupval. There's several ways to do this (cf man awk):
Code:
awk -v FS="<TAB>" -v lookupVal="$luVal" '...'

or
Code:
awk -v lookupVal="$luVal" 'BEGIN { FS = "<TAB>" } ...'

or
Code:
awk -v lookupVal="$luVal" '... ' FS="<TAB>" ...

. For FS, this one has survived from old days:
Code:
awk -F"<TAB>" ...

What I don't understand is why you echo nothing into a pipe for awk's stdin and then use a file to work upon?
# 3  
Old 05-29-2015
Help with awk issue

Because I'm a noob. by all means tell me how I ought to be doing it, I want to learn.
# 4  
Old 05-29-2015
Well, first of all, there's man pages and info online documentation that is consulted again and again, even by experienced IT people. Fora like this are willing to give you hints and help when you're stuck.

For above question, consider using either of the examples:
Code:
myResult=$(awk -F"<TAB>" -v lookupVal="$luVal" '$1 == lookupVal { print $2 }' "$inputFile")

<TAB> stands for a single <TAB> character (not sure if ALL awks accept the "\t" notation), and the deprecated backticks have been replaced by the recommended $(...) command substitution syntax.
# 5  
Old 05-29-2015
I did try some research

I did in fact, read the man pages and online examples, but there were so many differences to approach and syntax that I just got pretty mixed up.

Thanks for your assistance.
# 6  
Old 05-29-2015
Note: all awks (except old awk that nobody uses) accept the \t notation, so -F'\t' should work
# 7  
Old 05-29-2015
I just got it to work:
Code:
myResult=$(awk -F"\t" -v lookupVal="$luVal" '$1 == lookupVal { print $2 }' "$inputFile")

I had to use "\t" for the tab character syntax.

Thanks for all your help.

James

Moderator's Comments:
Mod Comment Code tags, please...


---------- Post updated at 03:04 PM ---------- Previous update was at 02:24 PM ----------

I am working OSX. When I create my data table in Excel and save it as a tab delimited file, the default line delimiter is a MAC Carriage return "\r"

I looked through the man page and some other sources on line and I don't see an option to set the record separator "-RS" from the command line the same way we used "-F" to set the field separator.

Here is my working code (Assuming UNIX line feeds)

Code:
assetTypeHandling=$(awk -F"\t" -v lookupVal="$assetType" '$1 == lookupVal { print $2,$3,$4,$5 }' "$assetTypeMapFile")

I tried
Code:
awk -RS"\r" -F"\t" -v ...

and
Code:
awk RS="\r" -F"\t" -v ...

But both of those result in:
Code:
awk: syntax error at source line 1


Is there some way to feed this line of code "\r" as the record separator?

Last edited by Scott; 05-29-2015 at 04:26 PM.. Reason: Please use code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - issue to get the right IP

On AIX 5.3 and AIX 6.1, I have this script for checking printers being pingable or not. for i in `lsallq` do echo "Queue Name: " $i echo "----------------------------------------" for j in `lsallqdev -q $i` do echo " Device Name:" $j hname=`echo... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

2. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

3. Shell Programming and Scripting

awk issue

Hi all, i am trying to use below command to see the output of hardware inventory, but i only see 2 first line no output of the command. awk '/Hardware/ {print $0}' XXX_result.txt Hardware inventory: Hardware inventory: any idea how to see whatever is under hardware inventory. i... (11 Replies)
Discussion started by: Jared
11 Replies

4. Shell Programming and Scripting

issue trying to use awk

Hi Gurus, I am facing a similar issue usiung an awk command. Below is my requirement: ---DATA--- A;F;G A;D;E A;D;E B;Z;P C;Z;Q Expected: A F<TAB>G D<TAB>E D<TAB>E B D<TAB>E (1 Reply)
Discussion started by: rajangupta2387
1 Replies

5. Shell Programming and Scripting

AWK Issue

Hey, this is my code, cat $fulltrpath | while read line do inputfile=$(sed 1q $fulltrpath | awk '{ FS = "\t"; print $2$1}') outputpath=$(sed 1q $fulltrpath | awk '{ FS = "\t"; print $3 }') echo $inputfile echo $outputpath cp $inputfile $outputpath let path++ done if i... (1 Reply)
Discussion started by: inshafccna
1 Replies

6. Shell Programming and Scripting

awk NR issue

Hi guys, i am trying to analyze a text file using awk and am not able to solve this issue. This is the piece of code that I have written BEGIN { ## Time to count MACs -> 5 seconds. TIME_LIMIT = 5; k = 50000; } ## For every line. { time_in_seconds = $1... (2 Replies)
Discussion started by: jamie_123
2 Replies

7. Shell Programming and Scripting

Issue with AWK

I have this input file 0FB7,1083,Synchronized,FriNov121655,2816_7RAID5,05F:1_10F:1,10000000NoneNone,DC_db00p01 0FB7,1150,Split,MonApr180658,2816_7R5GC,N/A,N/A,N/A 06C4,0710,Synchronized,WedMar91105,2816_7RAID5,04E:1_11E:1,10000000NoneNone,DL_nb00p25... (1 Reply)
Discussion started by: greycells
1 Replies

8. Shell Programming and Scripting

Issue in awk

In the following code, Im trying to imbed many statements in a single awk statement. But it gives an error on that, for i in `less usage_types_dwh.txt` do cd /u01/app/evident/analysis_lab/usg_type grep $i svc_type.txt | head -1 | awk 'BEGIN {FS=","} {print $1 "==" $2 ":" $3 ":" $4;... (2 Replies)
Discussion started by: alishehzadpaul
2 Replies

9. Shell Programming and Scripting

Awk issue

Can someone please explain below code. $LIST|awk ' /^$/ { next } substr($0,1,4)=="Exiting" { mk = 1; next } mk==1 { print $3,$7,$10,$14; exit } Cheers, gehlnar (5 Replies)
Discussion started by: gehlnar
5 Replies

10. Shell Programming and Scripting

Awk issue

Hi, # grep "^Listen" httpd.conf | awk '{print $2}' FrontEnd_1_IP:8081 FrontEnd_2_IP:8081 8081 8082 8083 # I need to get the values one at a time but I just can't manage to do that. Thanks, Bianca (20 Replies)
Discussion started by: potro
20 Replies
Login or Register to Ask a Question