parsing a string in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing a string in a shell script
# 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 07: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 07: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 07: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 07:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with understanding this regex in a Perl script parsing a 'complex' string

Hi, I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Parsing XML using shell script

Well, issue is i have to parse this script to get the VERSION: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleAllowMixedLocalizations</key> ... (9 Replies)
Discussion started by: zorosinister
9 Replies

3. Shell Programming and Scripting

parsing using shell script

I have a file parameters.txt which contains 151524 151525 I have another file OID.csv which contains NE Version Object Type ID SDK param name Object OID test1 Start: 4.2 End: 4.2 pan 151524 speed ... (5 Replies)
Discussion started by: LavanyaP
5 Replies

4. Shell Programming and Scripting

XML parsing using shell script

I have a xml file like this <bul:collectionStrategy name="strategy1"> <bul:collectionTemplateGroup name="15min group"/> <bul:collectionTemplateGroup name="hourly group"/> </bul:collectionStrategy> <bul:CollectionTemplateGroup name="hourly group" > ... (2 Replies)
Discussion started by: LavanyaP
2 Replies

5. Shell Programming and Scripting

String parsing in Korn Shell

Hi everybody, I have a string stored in a variable called record: record="SNMPv2-SMI::ent.9.9.43.1.3.9.2 = Timeticks: (177330898) 20 days, 12:35:08.98" I want to write some regular expressions good for Korn Shell to extract the number between parenthesis, in this case 177330898, and put it in... (3 Replies)
Discussion started by: omoyne
3 Replies

6. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

7. UNIX for Dummies Questions & Answers

shell script parsing with sed

#I'm quite new to scripting and my boss has asked me to solve a simple problem and sadly, I can't figure out how to do it. Any help is appreciated. :confused: #The following is a small shell script and the output that it produces for google.com. #!/bin/sh whois $1 | grep "Name Server"... (5 Replies)
Discussion started by: jjamd64
5 Replies

8. Shell Programming and Scripting

Parsing a line in Shell Script

I have record line somthing like below with first line showing char spacing not real record line 1 | 2 | 3rd Field--------------|-4th field--| This is charcatersapcing of line DF20000000000000000130.7890000000750 I shoudl get two line from above line 1st line should 1 | 2 | 3rd... (3 Replies)
Discussion started by: unishiva
3 Replies

9. Shell Programming and Scripting

Parsing a file in Shell Script

Hi, I have a requirement. I have an application which can take a file as inputs. Now the file can contain any number of lines. The tool has to pick up the first uncommented line and begin processing it. For example the file could be like this: #MANI123|MANI1234 #MANI234|MANI247... (4 Replies)
Discussion started by: sendhilmani123
4 Replies

10. Shell Programming and Scripting

shell script argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies
Login or Register to Ask a Question