LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2010, 11:06 AM   #1
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Rep: Reputation: 15
Using sed to remove all but the last 17 characters on a line


Hi All

I'm having a bit of a headbanger trying to work this one out. I'm trying to remove all of the characters on a line apart from the last 17. For example, I need to change this:

Code:
00000000000000000089;0bbfaeb8
01000000000000000089;0bcb5948
00000000000000000089;0bcc4c40
0000008A;061b4c10
0000008A;061fb2c8
0000008A;27a67210
0000008A;27b38590
0000008D;27a670d0
0000008D;27b547b8
0000008E;172fb478
0000008E;1734cdc8
0000000000000000000000000000000000000000008F;05f75cb0
0000000000000000746F72730479706520330000008F;05faf640
0000008F;17483e58
0000008F;174d3c28
00000090;176439f0
00000090;17694698
...in to this:

Code:
00000089;0bbfaeb8
00000089;0bcb5948
00000089;0bcc4c40
0000008A;061b4c10
0000008A;061fb2c8
0000008A;27a67210
0000008A;27b38590
0000008D;27a670d0
0000008D;27b547b8
0000008E;172fb478
0000008E;1734cdc8
0000008F;05f75cb0
0000008F;05faf640
0000008F;17483e58
0000008F;174d3c28
00000090;176439f0
00000090;17694698
I don't suppose anyone has any ideas? I was really looking for a syntax like the one for tail, where you can say "all of the bottom of the file apart from the first 2", for example:

Code:
$ tail -n +2 ./file
If anyone has any ideas I'd love to hear them!

Thanks, Simplified
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-03-2010, 11:19 AM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
I'd personally just use cut and rev--
Code:
rev filename.txt | cut -c1-17 | rev
or alternate just pipe each line to rev | cut -c1-17 | rev to correct that line.

edit: examples--

Code:
core:~/test/50$ cat test
00000000000000000089;0bbfaeb8
01000000000000000089;0bcb5948
00000000000000000089;0bcc4c40
0000008A;061b4c10
0000008A;061fb2c8
0000008A;27a67210
0000008A;27b38590
0000008D;27a670d0
0000008D;27b547b8
0000008E;172fb478
0000008E;1734cdc8
0000000000000000000000000000000000000000008F;05f75cb0
0000000000000000746F72730479706520330000008F;05faf640
0000008F;17483e58
0000008F;174d3c28
00000090;176439f0
00000090;17694698
core:~/test/50$ rev test | cut -c1-17 | rev
00000089;0bbfaeb8
00000089;0bcb5948
00000089;0bcc4c40
0000008A;061b4c10
0000008A;061fb2c8
0000008A;27a67210
0000008A;27b38590
0000008D;27a670d0
0000008D;27b547b8
0000008E;172fb478
0000008E;1734cdc8
0000008F;05f75cb0
0000008F;05faf640
0000008F;17483e58
0000008F;174d3c28
00000090;176439f0
00000090;17694698
core:~/test/50$ for i in $(cat test); do echo $i | rev | cut -c1-17 | rev; done 
00000089;0bbfaeb8
00000089;0bcb5948
00000089;0bcc4c40
0000008A;061b4c10
0000008A;061fb2c8
0000008A;27a67210
0000008A;27b38590
0000008D;27a670d0
0000008D;27b547b8
0000008E;172fb478
0000008E;1734cdc8
0000008F;05f75cb0
0000008F;05faf640
0000008F;17483e58
0000008F;174d3c28
00000090;176439f0
00000090;17694698
core:~/test/50$
You could redirect it to a new file (>test2) or pipe it onto other things (|grep -i blah) or whatever.

Last edited by rweaver; 06-03-2010 at 11:34 AM.
 
2 members found this post helpful.
Old 06-03-2010, 11:35 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

A sed solution was asked

sed 's/\(.*\)\(.\{17\}$\)/\2/' infile

Hope this helps.
 
2 members found this post helpful.
Old 06-03-2010, 11:50 AM   #4
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks!

Hi druuna and rweaver!

Thanks so much for the replies! I particularly like the use of the "rev" command - hadn't heard of that one, like it!

All the best, Simplified
 
Old 06-03-2010, 07:11 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
I agree that sed was asked for, but thought I would throw a grep into the mix
Code:
grep -o -E ".{17}$" file
 
2 members found this post helpful.
Old 06-04-2010, 03:33 AM   #6
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks Grail! Like you're thinking... I guess that's why I love *nix!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 12 01-16-2023 10:21 AM
bash script to remove first characters from every line (00) Linux - General 8 08-01-2011 10:28 AM
Trying to use sed to remove last line if it contains a certain string. slaxative Linux - Software 1 03-18-2008 02:13 AM
[SOLVED] sed: How to remove the end of a line? angel115 Programming 2 10-01-2007 10:29 AM
Getting last characters of a line with sed command LULUSNATCH Programming 4 12-21-2005 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration