LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2017, 06:54 AM   #1
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Rep: Reputation: 3
SED Guru Needed -- Combine Two SED Commands Into One Liner


I am trying to do in-place editing of each line that begins with the letter D, keeping the last 3 characters appended to the first 15 characters. I found separate sed commands to do the separate functions but cannot figure out how to combine them into a single command line:
# To remove everything except the 1st n characters in every line:
Code:
sed -r 's/(.{15}).*/\1/' file
# To remove everything except the last n characters in every line:
Code:
sed -r 's/.*(.{3})/\1/' file
Example -- I start with this inside "file":
Code:
D some text      end
Line that wont be edited because it does not begin with D
D other text     end
Other line that wont be edited because it does not begin with D
D similar text   end
I want to end up with this inside "file":
Code:
D some text    end
Line that wont be edited because it does not begin with D
D other text   end
Other line that wont be edited because it does not begin with D
D similar text end
I'm thinking it should be possible to combine the two sed commands but I have not been able to figure out how. Any SED Gurus out there to shed some light?

Thanks!!!

Last edited by RandyTech; 03-23-2017 at 06:56 AM.
 
Old 03-23-2017, 07:08 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,160
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
There are probably several ways to combine the two. If I understand the way you've described it, then one way would be like this:

Code:
sed -r -e '/^D/s/^(.{15}).*(.{3})$/\1\2/'
The initial /^D/ means to apply the following s/// only to lines starting with D

Do you want it to suppress printing of lines that start without a D? The above lets them pass through unmodified.
 
1 members found this post helpful.
Old 03-23-2017, 07:26 AM   #3
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
Hi Turbocapitalist. Thanks for the speedy reply!! Thats not working for me. Its not appending the last 3 characters. I am using grep to test the sed command:
Code:
grep ^D file |sed -r -e '/^D/s/^(.{15}).*(.{3})$/\1\2/'
 
Old 03-23-2017, 07:30 AM   #4
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
Oh!! This is working!!

Code:
grep ^D file |sed -r -e 's/^D(.{15}).*.*(.{3})$/\1\2/'
THANKS Turbocapitalist!! ! ! !
 
Old 03-23-2017, 08:03 AM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,160
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
No worries, but the grep is unneeded and probably in the way. The /D/ takes the place of grep and applies the substitution only matching lines. Try like mentioned above

Code:
sed -r -e '/^D/s/^(.{15}).*(.{3})$/\1\2/' somefile
It works with the test data you provided in the initial post.

If you want to eliminate lines that don't start with 'D' then you can throw in a conditional branch (t) to hop over a delete command (d) :

Code:
sed -r -e '/^D/s/^(.{15}).*(.{3})$/\1\2/;t;d;' somefile
Check the manual page a lot. It starts to make sense if you treat it as a simple programming language, which it is.

Code:
man sed
 
  


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
What command should I combine with sed to do this? Henry Drexler Linux - Software 5 11-30-2011 12:19 PM
[SOLVED] sed one-liner: search text in file and replace it. if not present, insert it pkramerruiz Programming 8 10-17-2010 09:49 AM
Help with Simple sed one liner SHARPY Linux - Newbie 10 04-12-2009 12:49 PM
Sed one-liner to drop data from beginning of file? lodi Programming 6 10-29-2008 05:22 PM
Need 'sed command' guru valnar Linux - General 9 02-27-2007 06:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:58 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration