- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: can sed search for last two digits of line...
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
06-30-2005 10:28 AM
06-30-2005 10:28 AM
2088872167,15 1 01 047
similar
similar
Nothing is consistent except for the spacing.
I need to insert a space before the last two digits of the line...
2088872167,15 1 01 0 47
Can this be done
What would be the syntax...
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 10:39 AM
06-30-2005 10:39 AM
Re: can sed search for last two digits of line...
grep '47$' filename
Like the $ specifies to find the pattern at the end of a line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 10:51 AM
06-30-2005 10:51 AM
Re: can sed search for last two digits of line...
From this:
2088872167,15 1 01 047
2088872168,15 1 01 123
2088872166,15 1 01 546
2088872164,15 1 01 048
...
...
To this:
2088872167,15 1 01 0 47
2088872168,15 1 01 1 23
2088872166,15 1 01 5 46
2088872164,15 1 01 0 48
...
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 10:53 AM
06-30-2005 10:53 AM
Re: can sed search for last two digits of line...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 11:19 AM
06-30-2005 11:19 AM
Re: can sed search for last two digits of line...
create a little script called L.pl as follows
#!/usr/bin/perl
$a=
$l=length $a;
printf $l
(if your perl executable is in a different location, modify the first line accordingly)
chmod 755 L.pl
lets say your filename is MYFILE
cat MYFILE | while read line
do
len=`echo $line | ./L.pl`
let m2=$len-3
let m1=$len-2
part1=`echo $line | cut -c 1-${m2}`
part2=`echo $line | cut -c ${m1}-${len}`
echo $part1" "$part2 >> MY_new_FILE
done
again, this is coming from someone who can barely find his way through the infamous perl camelbook. I am sure there are more elaborate solutions using perl to to do the same in one command.
if your data on every line is a fixed length, you do not need the perl portion at all. Just replace that line with
len=your_line_length+1
(+1 because in perl first position is 0 not 1)
then you should be okay
Hope this helps
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 05:21 PM
06-30-2005 05:21 PM
Re: can sed search for last two digits of line...
sed -e 's/\(.*\)../\1 /g' file
will give the output of
2088872167,15 1 01 047
as
2088872167,15 1 01 0 (space)
sed -e 's/\(.*\) \(.*\)/\1/g'
will give the last coloumn
047
Now what you have to do is simply take the last coloumn get the last 2 bits and append it.
sed -e 's/\(.*\) \(.*\)/\1/g' | cut -c 2,3
will give 47
Now just append this to the end of line.
This is general also, and will help with every line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 05:26 PM
06-30-2005 05:26 PM
Re: can sed search for last two digits of line...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 05:36 PM
06-30-2005 05:36 PM
Re: can sed search for last two digits of line...
I was doing a page full of scripting for that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 05:43 PM
06-30-2005 05:43 PM
Re: can sed search for last two digits of line...
sed -e 's/\(..\)$/ \1/' file
^^^^^^^^^^^^^^^^^^blank/space before \1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 06:03 PM
06-30-2005 06:03 PM
Re: can sed search for last two digits of line...
next time will be easier.
and it is a short sed script, but doing it in the shell is probably faster
while read var
do
firstPart=${var%??}
last2=${var#$firstPart}
print $firstPart $last2
done < yourFile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 06:29 PM
06-30-2005 06:29 PM
Re: can sed search for last two digits of line...
Can you explain that a bit to me.
I haven't done that type of scripting.
A bit of guidance required.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2005 07:41 PM
06-30-2005 07:41 PM
Solution# cat > testfile
2088872167,15 1 01 047
2088872168,15 1 01 123
2088872166,15 1 01 546
2088872164,15 1 01 048
# perl -pe 's/(..)$/ $1/g' testfile
2088872167,15 1 01 0 47
2088872168,15 1 01 1 23
2088872166,15 1 01 5 46
2088872164,15 1 01 0 48
or
# sed 's/\(..\)$/ \1/g' testfile
2088872167,15 1 01 0 47
2088872168,15 1 01 1 23
2088872166,15 1 01 5 46
2088872164,15 1 01 0 48
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2005 03:55 AM
07-01-2005 03:55 AM
Re: can sed search for last two digits of line...
Thanks!