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

You can use awk for both.

e.g.
First four characters

$ echo "welcome" | awk '{ print substr( $0, 0, 4 ) }'
welc

Last four characters

$ echo "welcome" | awk '{ print substr( $0, length($0) - 3, length($0) ) }'
come

Cheers
ZB

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

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

$ 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.

$ 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:

$ 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.

Excellent. I sincerely thank you for the clear explanation.
Regds
Asutoshch

No problem :slight_smile:

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

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

Also the original question does have an answer....

typeset  -L4 first4
typeset -R4 last4
x="welcome"
first4=$x
last4=$x
echo $first4 $last4

Also you can discard the first 4 or the last 4.

last_part=${x#????}
first_part=${x%????}
echo $first_part  $last_part

Does anyone know how to assign the variable from resulted from awk to the shell program?

Sample:

echo questionlabel |awk '{print length($0)}
13

I would like to use 13 as a variable in my shell program.
How do I assign 13 to a variable after this?

Thanks,

using ksh:

i=$(echo questionlabel |awk '{print length($0)}')

To parse the four firts characters of a string:

echo "welcome" | cut -c4

Hi,

Suppose I have a file such as

....
.....
.....
Start time = 10:10:00
Number of records loaded = 2074921
End time = 11:23:12
....
.....

...and I want to extract the number of records loaded i.e. 2074921, how do I go about doing it?

You should have started a new thread rather than continuing with an old thread.

If your output remains in the format you have given i.e. if it includes the line
Number of records loaded, then do this

sed -n -e 's_Number of records loaded = \([0-9][0-9]*\)_\1_p' your.input.file

Hello,

cat file | awk -F= '/^Number of records loaded/ {printf $2}'

--
Solea

Thanks a lot Vino, Solea...
guess i should haave started a new thread...
i posted it here since it was related to parsing...
sorry for breaking the thread guys...and thanks for the help..

Why are you using cat? Even if awk couldn't read files, you could do this:

awk '/^Number of records loaded/{print $NF}' <file

But the proper way is

awk '/^Number of records loaded/{print $NF}' file

@futurelet .. i agree.. that's what i settled into finally (taking a lead from solea, of course) ..

(...i guess that's all i should post in this thread lest i should be charged of hijacking other's threads... thanks a lot guys :slight_smile: )

value=`sed -n '/Number/p' file | sed -e 's/^.*= //'`
echo $value

Perderabo,

I used the ${x#????} and ${x%????} and they worked like majik ...
I wonder where I can learn more about this technique :slight_smile:

Thanks alot!

There is no better tutorial than "man ksh"........ to learn all this stuff

helped me,. thanks