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
shell script parsing with sed jjamd64 UNIX for Dummies Questions & Answers 5 12-11-2007 04:51 PM
Parsing a line in Shell Script unishiva Shell Programming and Scripting 3 11-01-2007 04:30 PM
Help in parsing a CSV file with Shell script mihirk Shell Programming and Scripting 10 06-24-2007 10:58 AM
Parsing a file in Shell Script sendhilmani123 Shell Programming and Scripting 4 11-30-2006 02:29 AM
shell script argument parsing rmjoe Shell Programming and Scripting 1 07-28-2005 03:37 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 Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 08-05-2004
asutoshch asutoshch is offline
Registered User
  
 

Join Date: Mar 2001
Location: Calcutta, India
Posts: 49
parsing a string in a shell script

I need to parse a string in a shell script. I understand there is some in built function to use that. can someone explain the syntax ?

Say, it is like this
#!/bin/ksh
read input
# input is entered as 'welcome'
#Now I want to extract say only first 4 characters or last four #characters.

Thanks
Asutosh
  #2 (permalink)  
Old 08-05-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
You can use awk for both.

e.g.
First four characters
Code:
$ echo "welcome" | awk '{ print substr( $0, 0, 4 ) }'
welc
Last four characters
Code:
$ echo "welcome" | awk '{ print substr( $0, length($0) - 3, length($0) ) }'
come
Cheers
ZB
  #3 (permalink)  
Old 08-05-2004
asutoshch asutoshch is offline
Registered User
  
 

Join Date: Mar 2001
Location: Calcutta, India
Posts: 49
Zazzybob, Thanks for your reply. It would be great if you please explain me the arguments of substr function. I could not find in man pages.
Regds
asutoshch
  #4 (permalink)  
Old 08-05-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Sure

substr( s, i, n )

Where:

s is the string you want to perform the substr operation on
i is the first character that you want to extract
n is the last character you want to match

So if you said substr( "hello", 2, 4 ) it would match the second, third and fourth character and return "ell".

Let me explain the code I gave

Code:
$ echo "welcome" | awk '{ print substr( $0, 0, 4 ) }'
In the above example, the first argument to substr is $0 (which in this case is the text piped to it - "$0" is the entire record). The second argument is 0 (I could also have used 1) because I want to match from the start of the string. I want to grab the first four characters, so the third argument is 4.

Code:
$ echo "welcome" | awk '{ print substr( $0, length($0) - 3, length($0) ) }'
The above code also makes use length(string) command to return the number of characters in the string.
So here, I'm saying "return the substring from the first character (which is the length of the string minus 3), up until the very end of the string".

To clarify this:
Code:
$ echo "welcome" | awk '{ print (length($0) - 3), length($0) }'
4 7
So in our substr, we grab from character 4 to character 7 inclusive, which are chars 4,5,6,7 - the last 4 chars.
  #5 (permalink)  
Old 08-05-2004
asutoshch asutoshch is offline
Registered User
  
 

Join Date: Mar 2001
Location: Calcutta, India
Posts: 49
Excellent. I sincerely thank you for the clear explanation.
Regds
Asutoshch
  #6 (permalink)  
Old 08-05-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Excellent. I sincerely thank you for the clear explanation.
No problem

I also missed a very simple solution too - you can use head and tail with the -c option

e.g. (on Linux)
echo "welcome" | head -c 4
echo "welcome" | tail -c 5

On HP-UX, you must use head -c -n 4

There's more than one way to do it with *nix!

Cheers
ZB
  #7 (permalink)  
Old 08-05-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
It's worth mentioning that expr has a substr function too...

$ input="welcome"
$ expr substr $input 1 4
welc
$ expr substr $input $((${#input}-3)) 4
come
Closed Thread

Bookmarks

Tags
linux

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 03:42 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