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
last char from a string broli Shell Programming and Scripting 6 12-07-2007 08:02 PM
replacing char with string phani_sree High Level Programming 1 11-20-2006 08:57 AM
string of 7 char length always... thanuman UNIX for Dummies Questions & Answers 3 04-12-2005 01:51 PM
Compare Char to String Phobos High Level Programming 3 04-09-2005 12:01 PM

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 12-17-2003
gio123bg gio123bg is offline
Registered User
  
 

Join Date: Nov 2003
Posts: 15
Unhappy Number of specific char in a string.

I wish to compute the number of dot chars in a string.
Example:
VAR="aaaa.bbbbb.cccc"

I try the shortest command to solve this test.

Thanks in advance for your help.
Regards,

Giovanni
  #2 (permalink)  
Old 12-17-2003
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
echo $VAR|awk '{print gsub("\.",0)}'
  #3 (permalink)  
Old 12-17-2003
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,937
As far as I am aware the gsub trick only works with gawk.
The AT&T version (awk/nawk) does not return the number
of substitutions. Nor is this required by POSIX or Single
UNIX Specifications.

Another way of achieving the required result is:

echo $VAR | tr -c -d '.' | wc -c

- Finnbarr
  #4 (permalink)  
Old 12-18-2003
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
The gsub() function exists in the freely available awks including POSIX awk, nawk and gawk.

Nice solution though.
  #5 (permalink)  
Old 12-19-2003
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,937
Yes, gsub() exists in all versions of g/n/awk. However, gawk
is the only version where the man page states that the
function returns the number of substitutions made.

Man pages for the other versions of awk are silent on this
issue. POSIX.1:2003 does not mandate it. This being the
case, one cannot rely on this behavour in portable code.

- Finnbarr
  #6 (permalink)  
Old 12-19-2003
gio123bg gio123bg is offline
Registered User
  
 

Join Date: Nov 2003
Posts: 15
Thanks!

On my OS (Aix v. 5.1) the command echo $VAR|awk '{print gsub("\.",0)}' returns always 1 if the VAR string contains or not a dot character.

echo $VAR|awk '{print gsub("\.",0)}' works correctly.

Giovanni
  #7 (permalink)  
Old 12-19-2003
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
echo ${VAR}|awk -F. '{print NF-1}'
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 06:15 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