Wednesday, October 27, 2021

[SOLVED] sed command that combine two command that are dependant on each other

Issue

I need help with a sed command that copies the line of birthday to standard output, removing all lines that start with an "A". Suppose my sample.txt file looks like this:

A birthday is very much celebrated. 
Today is my birthday.
I can celebrate my birthday. 
A man can do anything.

and this is the sample output:

Today is my birthday.
I can celebrate my birthday.

So, for the birthday line:

sed -n /birthday/p sample.txt

and for the removing the lines that starts with "A":

sed -n '/^A/!p' sample.txt

Now, i am confused on how to combine these two lines so that they can work according to the question.


Solution

What you want is the pipe operator |. It takes the output of the command one the left and 'pipes' it to the command on the right by connecting STDOUT to STDIN.

So in your case you'd do:

sed -n /birthday/p sample.txt | sed -n '/^A/!p'

Edit: formatting



Answered By - j4ckofalltrades