- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Using sed instead of cut utility
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
Forums
Discussions
Discussions
Discussions
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
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
09-28-2006 12:53 AM
09-28-2006 12:53 AM
Using sed instead of cut utility
I have a UNIX shell script which is to extract the timestamp values into their separate components such as the year, month, day, hour, min, secs.
The timestamp is denoted by the variable $timestamp below. The $timestamp denotes the UNIX epoch time since 1970.
timestamp=1159445287
From the $timestamp value, I've converted the UNIX timestamp into a format of year, month, day, hour, min, seconds into the variable
$newTimeToSecs.
newTimeToSecs=060922113366
Now, I'm attempting to extract values of the year, month, day, hour, min, seconds from the
variable "$newTimeToSecs" and place them into separate variables. I've used the 'cut' utility to extract them.
year=`/usr/bin/echo $newTimeToSecs |/usr/bin/cut -b1-2`
month=`/usr/bin/echo $newTimeToSecs |/usr/bin/cut -b3-4`
day=`/usr/bin/echo $newTimeToSecs |/usr/bin/cut -b5-6`
hour=`/usr/bin/echo $newTimeToSecs |/usr/bin/cut -b7-8`
MIN=`/usr/bin/echo $newTimeToSecs |/usr/bin/cut -b9-10`
secs=`/usr/bin/echo $newTimeToSecs |/usr/bin/cut -b11-12`
However, I'd would like to know how I could used the 'sed' utility to perform the same task.
year=`echo newTimeToSecs | sed -e 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1/'`
month=`echo newTimeToSecs | sed -e 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\2/'`
day=`echo newTimeToSecs | sed -e 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\3/'`
hour=`echo newTimeToSecs | sed -e 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\4/'`
min=`echo newTimeToSecs | sed -e 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\5/'`
seconds=`echo newTimeToSecs | sed -e 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\6/'`
Could anyone show me a better way using 'sed' to achieve the same task?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 01:04 AM
09-28-2006 01:04 AM
Re: Using sed instead of cut utility
year=$(echo $newTimeToSecs | sed -e 's/^\([0-9]\{2\}\).*/\1/')
month=$(echo $newTimeToSecs | sed -e 's/[0-9]\{2\}\([0-9]\{2\}\).*/\1/')
etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 01:09 AM
09-28-2006 01:09 AM
Re: Using sed instead of cut utility
For something like this, I'd use Perl or 'awk'. With Perl you could 'unpack' or use 'substr' to perform the extractions. With 'awk' you have 'substr'.
Otherwise, my choice would be to use 'cut'. It is the most natural corrollary.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 01:25 AM
09-28-2006 01:25 AM
Re: Using sed instead of cut utility
last few lines of section 36.23.4 in http://www.sm.luth.se/~alapaa/file_fetch/unixcdbookshelf/upt/ch36_23.htm seems to suggest you could assign values to multiple variables in one go, with eval.
I have never used this method.
I tend to choose the easiest path to a solution, which in this case seems to be cut.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 02:51 AM
09-28-2006 02:51 AM
Re: Using sed instead of cut utility
what about
echo $newTimeToSecs | sed 's/\(..\)/\1 /g' | read year month day hour min seconds
This will not work in a (Linux-)Pdksh or bash, but in the HP-Posix shell and other KSHs.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 03:51 AM
09-28-2006 03:51 AM
Re: Using sed instead of cut utility
date +'%y %m %d %H %M %S' | read yr mo da hr mn sec
change %H to %I for 12hr clock
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 03:54 AM
09-28-2006 03:54 AM
Re: Using sed instead of cut utility
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 04:26 AM
09-28-2006 04:26 AM
Re: Using sed instead of cut utility
year=`echo $newTimeToSecs | sed 's/^\(.\{2\}\).*$/\1/p'`
month=`echo $newTimeToSecs | sed 's/^..\(.\{2\}\).*$/\1/p'`
day=`echo $newTimeToSecs | sed 's/^....\(.\{2\}\).*$/\1/p'`
hour=`echo $newTimeToSecs | sed 's/^.\{6\}\(.\{2\}\).*$/\1/p'`
min=`echo $newTimeToSecs | sed 's/^.\{8\}\(.\{2\}\).*$/\1/p'`
seconds=`echo $newTimeToSecs | sed 's/^.\{10\}\(.\{2\}\)$/\1/p'`
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 08:34 PM
09-28-2006 08:34 PM
Re: Using sed instead of cut utility
on my Hp-UX 11i:
% newTimeToSecs=060922113366
%>echo $newTimeToSecs | sed 's/\(..\)/\1 /g' | read yy mm dd hh mm ss
% echo $yy $mm $dd $hh $mm $ss
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2006 11:38 PM
09-28-2006 11:38 PM
Re: Using sed instead of cut utility
I attempted your suggested method but I noticed it duplicated the values being output.
mz$ echo 1159528031 | sed 's/^..\(.\{2\}\).*$/\1/p'
59
59
mz$ echo 1159528031 |sed 's/^....\(.\{2\}\).*$/\1/p'
52
52
mz$ echo 1159528031 |sed 's/^.\{6\}\(.\{2\}\).*$/\1/p'
80
80
mz$ echo 1159528031 |sed 's/^\(.\{2\}\).*$/\1/p'
11
11
mz$ echo 1159528031|sed 's/^.\{8\}\(.\{2\}\).*$/\1/p'
31
31
Could you point out where did I go wrong?
Thanks
Danny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2006 01:19 AM
09-29-2006 01:19 AM
Re: Using sed instead of cut utility
did you try my solution?
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2006 02:56 AM
09-29-2006 02:56 AM
Re: Using sed instead of cut utility
>I attempted your suggested method but I noticed it duplicated the values being >output.
>mz$ echo 1159528031 | sed 's/^..\(.\{2\}\).*$/\1/p'
>59
>59
Provide the "-n" switch to all the sed commands for removing duplicates i.e.
mz$ echo 1159528031 | sed -n 's/^..\(.\{2\}\).*$/\1/p'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2006 05:39 AM
09-29-2006 05:39 AM
Re: Using sed instead of cut utility
in Sandmans solution you can just drop the 'p(rint)' directive AND the '-n' option:
Use
echo 1159528031 | sed 's/^..\(.\{2\}\).*$/\1/'
59
mfG Peter