parsing a string in a shell script


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-05-2004
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
Code:
#!/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

Last edited by rbatte1; 01-26-2017 at 06:21 AM..
# 2  
Old 08-05-2004
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  
Old 08-05-2004
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
Sure
Code:
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.

Last edited by rbatte1; 01-26-2017 at 06:22 AM..
# 5  
Old 08-05-2004
Excellent. I sincerely thank you for the clear explanation.
Regds
Asutoshch
# 6  
Old 08-05-2004
Quote:
Excellent. I sincerely thank you for the clear explanation.
No problem Smilie

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

e.g. (on Linux)
Code:
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

Last edited by rbatte1; 01-26-2017 at 06:23 AM..
# 7  
Old 08-05-2004
It's worth mentioning that expr has a substr function too...

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


Last edited by rbatte1; 01-26-2017 at 06:23 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
HTML parsing with UNIX shell script alvinoo UNIX for Dummies Questions & Answers 7 06-10-2015 02:54 AM
DB2 in awk or PARSING VALUE in Shell Script Nandy Shell Programming and Scripting 1 11-07-2014 04:45 AM
Parsing line with name:value pairs in shell script tasmac Shell Programming and Scripting 9 04-03-2013 08:01 PM
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
XML parsing using shell script LavanyaP Shell Programming and Scripting 2 05-13-2011 04:25 AM
XML parsing in a shell script. dashok.83 Shell Programming and Scripting 1 09-05-2010 11:13 AM
String parsing in Korn Shell omoyne Shell Programming and Scripting 3 05-10-2010 12:29 PM
Parsing XML using Shell Script Kage Musha Shell Programming and Scripting 1 01-18-2010 09:33 AM
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
shell script parsing with sed jjamd64 UNIX for Dummies Questions & Answers 5 12-11-2007 03:51 PM
Parsing a line in Shell Script unishiva Shell Programming and Scripting 3 11-01-2007 04:30 PM
Parsing a file in Shell Script sendhilmani123 Shell Programming and Scripting 4 11-30-2006 01:29 AM
shell script argument parsing rmjoe Shell Programming and Scripting 1 07-28-2005 03:37 PM