The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 07:11 AM
Search a string and append text after the string kesu2k Shell Programming and Scripting 8 07-18-2008 08:35 PM
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 04:17 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 09:53 AM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-24-2008
melanie_pfefer melanie_pfefer is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 234
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 (permalink)  
Old 07-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,334
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 (permalink)  
Old 07-24-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
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 (permalink)  
Old 07-24-2008
mph mph is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 58
For a string try this:


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

  #5 (permalink)  
Old 07-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,334
More possibilities:

Code:
echo 'a:b:c' | sed 's/.*://'

Or:

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

  #6 (permalink)  
Old 07-24-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
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

Closed Thread

Bookmarks

Tags
awk, input

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0