LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED Guru Needed -- Combine Two SED Commands Into One Liner (https://www.linuxquestions.org/questions/linux-newbie-8/sed-guru-needed-combine-two-sed-commands-into-one-liner-4175602386/)

RandyTech 03-23-2017 06:54 AM

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!!!

Turbocapitalist 03-23-2017 07:08 AM

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.

RandyTech 03-23-2017 07:26 AM

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/'

RandyTech 03-23-2017 07:30 AM

Oh!! This is working!!

Code:

grep ^D file |sed -r -e 's/^D(.{15}).*.*(.{3})$/\1\2/'
THANKS Turbocapitalist!! ! ! !

Turbocapitalist 03-23-2017 08:03 AM

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


All times are GMT -5. The time now is 07:30 AM.