The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
egrep syntax question DKNUCKLES Shell Programming and Scripting 1 11-18-2007 04:46 PM
Simple Awk Question: If Syntax natdeamer Shell Programming and Scripting 1 09-10-2007 12:53 PM
C-shell: variable syntax question alex_5161 SUN Solaris 0 01-30-2007 02:43 PM
Question: non-recursive find syntax alexkav UNIX for Dummies Questions & Answers 6 03-10-2005 07:46 AM
awk syntax question hcclnoodles Shell Programming and Scripting 2 10-28-2004 12:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-22-2007
prkfriryce prkfriryce is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 44
yet another awk field syntax question

I am trying to print the remaing fields and field numbers beginning with a field 'xyz'

Code:
#cat abc
test1:test2:xyz:test3:test4:test5

#cat def
test1:test2:test3:xyz:test4:test5
desired output is to be able to print NF and any trailing fields separated by':'

test3 3
or
test4 3
or
test5 3

likewise,

test4 2
or
test5 2

placeing a field separator as 'xyz' illiminates using ':' as a second separator or is that possible?
  #2 (permalink)  
Old 03-22-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
awk -F: -v OFS="\nor\n" ' { sub(".*xyz:",""); for(i=1;i<=NF;++i) $i=$i" "NF ;print }  ' file
  #3 (permalink)  
Old 03-22-2007
prkfriryce prkfriryce is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 44
I'm gonna use sed for the time being, but would like to know the correct awk syntax as well

cat abc | sed 's/\(.*\)xyz://' | awk -F: '{print $1,NF}'

edit*

Quote:
Originally Posted by anbu23
Code:
awk -F: -v OFS="\nor\n" ' { sub(".*xyz:",""); for(i=1;i<=NF;++i) $i=$i" "NF ;print }  ' file
thanks, i'll try it
  #4 (permalink)  
Old 03-22-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by prkfriryce
I'm gonna use sed for the time being, but would like to know the correct awk syntax as well

cat abc | sed 's/\(.*\)xyz://' | awk -F: '{print $1,NF}'

edit*



thanks, i'll try it
You can do everything in awk.Check my previous post
  #5 (permalink)  
Old 03-22-2007
prkfriryce prkfriryce is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 44
Quote:
Originally Posted by anbu23
You can do everything in awk.Check my previous post
yeah works well, a little too well! i was looking for a more simplified version by printing the fields individually, but the 'sub' syntax is what i was looking for. I'll tweek it for future applications.

thanks again
Sponsored Links
Closed Thread

Bookmarks

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 05:50 PM.


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