- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Pulling the first and last character/number fr...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-16-2008 03:31 PM
тАО06-16-2008 03:31 PM
# cat file
foobar23
I want the output to be:
f3
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-16-2008 04:13 PM
- Tags:
- cut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-16-2008 05:07 PM
тАО06-16-2008 05:07 PM
Re: Pulling the first and last character/number from a string.
Now I am trying to figure out how to make that all a variable that I can embed into a script..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-16-2008 06:21 PM
тАО06-16-2008 06:21 PM
Re: Pulling the first and last character/number from a string.
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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-17-2008 02:52 AM
тАО06-17-2008 02:52 AM
Re: Pulling the first and last character/number from a string.
# 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-17-2008 05:36 AM
тАО06-17-2008 05:36 AM
Re: Pulling the first and last character/number from a string.
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.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-17-2008 06:55 AM
тАО06-17-2008 06:55 AM
Re: Pulling the first and last character/number from a string.
WORD=`cat filename`
FIRSTCHAR=`echo $WORD | cut -c 1`
LASTCHAR=`echo $WORD | rev | cut -c 1`