Re: Pulling the first and last character/number from a string. (232 Views)
Reply
Super Advisor
Patrick Ware_1
Posts: 216
Registered: ‎12-14-2004
Message 1 of 7 (232 Views)
Accepted Solution

Pulling the first and last character/number from a string.

Let's say I have a word "foobar23" in a file, and I want to pull the first "f" and last "3" character out of the world, how would I accomplish that?

# cat file
foobar23

I want the output to be:

f3
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 2 of 7 (232 Views)

Re: Pulling the first and last character/number from a string.

Lots of choices depending on the rules. The general way is to use cut as in:

cat file | while read TEXT
do
LEN=${#TEXT}
print $(print "$TEXT" | cut -c1,$LEN)
done
Please use plain text.
Super Advisor
Patrick Ware_1
Posts: 216
Registered: ‎12-14-2004
Message 3 of 7 (232 Views)

Re: Pulling the first and last character/number from a string.

Thanks! That worked great!
Now I am trying to figure out how to make that all a variable that I can embed into a script..
Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,084
Registered: ‎02-23-2005
Message 4 of 7 (232 Views)

Re: Pulling the first and last character/number from a string.

"man sh".

td192> echo foobar23 | \
sed -e 's/^\(.\).*\(.\)$/\1\2/'
f3

td192> x=` echo foobar23 | \
sed -e 's/^\(.\).*\(.\)$/\1\2/' `
td192> echo $x
f3

Or, the trendy, new way:

td192> x=$(echo foobar23 | sed -e 's/^\(.\).*\(.\)$/\1\2/')
td192> echo $x
f3
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 7 (232 Views)

Re: Pulling the first and last character/number from a string.

Hi Patrick:

# echo "foobar23"|perl -nle 'print substr($_,0,1),substr($_,-1)'
f3

# perl -nle 'print substr($_,0,1),substr($_,-1)' file
f3

...in in your shell script:

# THING=$(perl -nle 'print substr($_,0,1),substr($_,-1)' file)
# echo ${THING}
f3

...JRF...
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 6 of 7 (232 Views)

Re: Pulling the first and last character/number from a string.

> ...variable in the script...

cat file | while read TEXT
do
LEN=${#TEXT}
MYVAR="$(print "$TEXT" | cut -c1,$LEN)"

...more script stuff...
print "First-Last=$MYVAR"

done

The main line is the: MYVAR="$(...)" where the result of the cut command is assigned to a variable called MYVAR. Be sure to use " because there may be a leading and/or trailing space in your file. Then test or use $MYVAR with "$MYVAR" to preserve and special characters.
Please use plain text.
Honored Contributor
Honored Contributor
TTr
Posts: 3,434
Registered: ‎01-22-2008
Message 7 of 7 (232 Views)

Re: Pulling the first and last character/number from a string.

How about this

WORD=`cat filename`
FIRSTCHAR=`echo $WORD | cut -c 1`
LASTCHAR=`echo $WORD | rev | cut -c 1`
I do not reply to forumers who 1) consistently don't assign points, 2) ask for step-by-step instructions for a topic but appear to have no IT experience and make no effort to learn. Some exceptions for newcomers and special topics.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation