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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing a string in nth line maxmave Shell Programming and Scripting 1 06-04-2008 02:32 PM
To cut end string from line visu Shell Programming and Scripting 7 12-20-2007 10:22 PM
regexp to get first line of string jonas.gabriel Shell Programming and Scripting 3 05-09-2007 12:46 AM
How I Can get String From the Line habuzahra UNIX for Dummies Questions & Answers 2 10-29-2006 03:55 AM
Add string after line X alienET Shell Programming and Scripting 2 10-13-2005 06:52 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Feb 2007
Location: Aus Syd
Posts: 10
Unhappy get the NF of a string line

Hi all,

New here and new to shell as well.
Currently I have a problem when use echo $1 | awk -F\| '{print NF}' to get the NF of input string separated by "|". There's input contains more than 100 fields, then I have error awk: record `dd|14/06/2003|ssssss...' has too many fields . What do I solve this problem?

Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-01-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,221
Use gawk, or try...
Code:
echo $(($(echo $1|tr -dc '|'|wc -c)+1))
Reply With Quote
  #3 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Feb 2007
Location: Aus Syd
Posts: 10
Thumbs up problem solved

Thanks Ygor, that solves the problem.
Reply With Quote
  #4 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Feb 2007
Location: Aus Syd
Posts: 10
Question Get field value.

since the awk can't handle 100+ fields. what do I use to get one of the field value, say field 45. Currently I am using
Code:
echo $1|awk -F\| '{print $45}' .
And I don't know why I can't get gawk command in my environment.

Thanks
Reply With Quote
  #5 (permalink)  
Old 02-01-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,221
Try...
Code:
oldIFS=$IFS
IFS='|'
set $1
IFS=$oldIFS
echo $#
echo ${45}
Reply With Quote
  #6 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Feb 2007
Location: Aus Syd
Posts: 10
Thumbs up thanks Ygor. that's really helpful


Got it. Cheers
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0