- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Get 1st and LAST char
Operating System - HP-UX
1819775
Members
3241
Online
109607
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО08-14-2000 03:43 AM
тАО08-14-2000 03:43 AM
Hi
I am trying to get the first and last char from a string (variable length)
Does anyone know the way to do it ??
Thanks
Keith
I am trying to get the first and last char from a string (variable length)
Does anyone know the way to do it ??
Thanks
Keith
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2000 04:00 AM
тАО08-14-2000 04:00 AM
Solution
Keith:
To get the first and the last character of a string, use the substr and length functions of awk.
# S=abcdef
# F=`echo $S|awk '{print substr($0,1,1)}'`
# L=`echo $S|awk '{print substr($0,length($0),1)}'`
where S is your variable string,
and F is the first character returned,
and L is the last character of the string.
...JRF...
To get the first and the last character of a string, use the substr and length functions of awk.
# S=abcdef
# F=`echo $S|awk '{print substr($0,1,1)}'`
# L=`echo $S|awk '{print substr($0,length($0),1)}'`
where S is your variable string,
and F is the first character returned,
and L is the last character of the string.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2000 10:11 AM
тАО08-14-2000 10:11 AM
Re: Get 1st and LAST char
could also try sed
s=abcdef
f=echo "$s" | sed -n "s/^(.).*/1/p"
l=echo "$s" | sed -n "s/.*(.)$/1/p"
could try using [a-zA-Z] instead of just "." if matching for a letter
"." matches any single character - except newline, I think...
s=abcdef
f=echo "$s" | sed -n "s/^(.).*/1/p"
l=echo "$s" | sed -n "s/.*(.)$/1/p"
could try using [a-zA-Z] instead of just "." if matching for a letter
"." matches any single character - except newline, I think...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2000 01:32 PM
тАО08-14-2000 01:32 PM
Re: Get 1st and LAST char
you could use a more modern shell version then the '88 version that HP supplies for /usr/bin/sh and ksh. Such as:
#!/usr/dt/bin/dtksh
string=abcdef
F=${string:0:1}
L=${string:$(( ${#string} - 1 )):1}
and if you'd prefer just calling awk or sed once
echo $string |
awk '{
print substr($0,1,1) " " substr($0,length($0),1);
}' |
read F L
echo $string |
sed -n "s/^\(.\)*\(.\)$/\1 \2/p" |
read F L
#!/usr/dt/bin/dtksh
string=abcdef
F=${string:0:1}
L=${string:$(( ${#string} - 1 )):1}
and if you'd prefer just calling awk or sed once
echo $string |
awk '{
print substr($0,1,1) " " substr($0,length($0),1);
}' |
read F L
echo $string |
sed -n "s/^\(.\)*\(.\)$/\1 \2/p" |
read F L
nobody else has this problem
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP