awk on a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk on a string
# 1  
Old 07-24-2008
awk on a string

Hi

I am trying to do
Code:
awk -F: '{print $3}' a:b:c

to get c.

awk however expects a file:


Code:
awk: can't open a:b:c

any idea?

Last edited by radoulov; 07-24-2008 at 09:28 AM.. Reason: added code tags
# 2  
Old 07-24-2008
Please place your code between code tags.
You can select your code and click on the # symbol above the edit window or place your code between code brackets as follow:

HTML Code:
[code]
awk -F: '{print $3}' file
[/code]
# 3  
Old 07-24-2008
Quote:
Originally Posted by melanie_pfefer
[...]awk however expects a file:
It's the expected behavior. If you really need to do something like this with AWK, you should write something like this:
Code:
awk 'BEGIN { 
  split(ARGV[1], t, ":")
  print t[3]
  }' a:b:c

# 4  
Old 07-24-2008
For a string try this:

Code:
assuming: var="a:b:c"
echo "$var" | awk -F: '{print $3}'

# 5  
Old 07-24-2008
More possibilities:
Code:
echo 'a:b:c' | sed 's/.*://'

Or:
Code:
echo 'a:b:c' | awk -F: '$0=$3'

# 6  
Old 07-24-2008
Actually,
it could be easily done in shell without any external command
(unless, as already stated, there is another reason to use AWK in this particular case):

Code:
% s=a:b:c
% (IFS=:;set -- $s;printf "$3\n")
c

With Z-Shell:
Code:
zsh-4.3.4% print ${${(s.:.)s}[3]}
c

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