parsing a string in a shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

parsing a string in a shell script

Shell Programming and Scripting


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-05-2004
asutoshch asutoshch is offline
Registered User
 
Join Date: Mar 2001
Last Activity: 4 January 2013, 3:32 AM EST
Location: Calcutta, India
Posts: 75
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 08-05-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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
Sponsored Links
    #3  
Old 08-05-2004
asutoshch asutoshch is offline
Registered User
 
Join Date: Mar 2001
Last Activity: 4 January 2013, 3:32 AM EST
Location: Calcutta, India
Posts: 75
Thanks: 2
Thanked 0 Times in 0 Posts
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  
Old 08-05-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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.
Sponsored Links
    #5  
Old 08-05-2004
asutoshch asutoshch is offline
Registered User
 
Join Date: Mar 2001
Last Activity: 4 January 2013, 3:32 AM EST
Location: Calcutta, India
Posts: 75
Thanks: 2
Thanked 0 Times in 0 Posts
Excellent. I sincerely thank you for the clear explanation.
Regds
Asutoshch
Sponsored Links
    #6  
Old 08-05-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
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
Sponsored Links
    #7  
Old 08-05-2004
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 112 Times in 98 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing XML using shell script zorosinister Shell Programming and Scripting 9 05-01-2012 02:53 AM
parsing using shell script LavanyaP Shell Programming and Scripting 5 05-13-2011 06:18 AM
String parsing in Korn Shell omoyne Shell Programming and Scripting 3 05-10-2010 12:29 PM
Parsing of file for Report Generation (String parsing and splitting) umar.shaikh Shell Programming and Scripting 8 03-02-2009 12:38 AM
Shell script to parsing log justbow Shell Programming and Scripting 10 12-06-2008 02:20 PM



All times are GMT -4. The time now is 01:32 PM.