awk a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk a string
# 1  
Old 06-22-2009
awk a string

Hi
Code:
#!/usr/bin/bash
ITEM=apple
cat file1.tsv | awk -F "\t" '($3 == $ITEM) {print $2}'

awk: illegal field $(), name "ITEM"
 input record number 1, file
 source line number 1

Here is file1.tsv
Code:
type category_id item code remarks
fruits 1 apple 345 567
fruits 3 banana 44 555

Smilie
# 2  
Old 06-22-2009
Use the -v option for shell variables, read your awk documentation.

Regards
# 3  
Old 06-22-2009
Code:
awk -v ITEM=$ITEM '($3 == ITEM) {print $2}' file1.csv

your single quotes mask the $, ond so the shell can't substitute this variable
# 4  
Old 06-22-2009
Quote:
Originally Posted by funksen
Code:
awk -v ITEM=$ITEM '($3 == ITEM) {print $2}' file1.csv

your single quotes mask the $, ond so the shell can't substitute this variable
Smilie Thanks a lot

But What about the delimiter?
# 5  
Old 06-22-2009
Code:
awk -F '\t' '$3 == ITEM { print $2 }' ITEM=apple infile

# 6  
Old 06-22-2009
this will also work i guess....
Code:
awk -F"\t" '$3 == '$ITEM'{print $2}' filename

# 7  
Old 06-22-2009
Quote:
Originally Posted by vidyadhar85
this will also work i guess....
Code:
awk -F"\t" '$3 == '$ITEM'{print $2}' filename

Hm, did you try it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

3. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

4. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

5. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

6. UNIX for Dummies Questions & Answers

awk for string between > and /n

Hi, I have a file that looks something like the following: >IGHV4-4*02 caggtgcagctgcaggagtcgggcccaggactggtgaagccttcggggaccctgtcc ctcacctgcgctgtctctggtggctccatcagcagtagtaactggtggagt tgggtccgccagcccccagggaaggggctggagtggattggggaaatctatcatagt gggagcaccaactacaacccgtccctcaagagtcgagtcaccatatcagta... (3 Replies)
Discussion started by: jyu429
3 Replies

7. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

8. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

9. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

10. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies
Login or Register to Ask a Question