Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

removing lines by sed

Writer Matthew Barrera

I have text inside some lines which is inside at below, and to extract Some lines using that command;

cat meyveler.txt
elma armut ve ayva daha cok kış meyveleridir
ancak,
yaz mevyeleride oldukca lezzetli oldugu gözden kacmamalıdır.
ayrıca tüm bunlara istinaden tüm calısmalat gözden gecirilebilir olmalıdır.
12.ayın gelmesiyle beraber ayva ve Nar sezonu hareketlenmeye,
Bus senenin bitisiylede Hamsiler hareketlenmeye baslayacaktır.
=tabi bu işlemler bu sekilde akıp gider

And to extract some lines using that command;

cat meyveler.txt | egrep "^[^a-z].*$"
12.ayın gelmesiyle beraber ayva ve Nar sezonu hareketlenmeye,
Bus senenin bitisiylede Hamsiler hareketlenmeye baslayacaktır.
=tabi bu işlemler bu sekilde akıp gider

Now i try to delete this line by this but i m getting error, what should i use for that?

cat meyveler.txt | egrep "^[^a-z].*$" | xargs -Iline sed -i "/line/d"
3

2 Answers

Given a file meyveler.txt, you can delete all lines that start not with a lowercase letter a-z with sed simply like this:

sed '/^[^a-z]/d' meyveler.txt

The above command will read from your file and print the filtered output to the terminal. If you want to edit the file directly in-place, add the -i flag. You can preserve the original file as backup if you specify a suffix (e.g. -i.bak will keep the original data as meyveler.txt.bak):

sed -i '/^[^a-z]/d' meyveler.txt

You don't need cat to read the file and you don't need grep or egrep to filter them. All this functionality is built into sed.

3

My solution is:

grep '^[a-z]' meyveler.txt

Which only returns the lines matched with your desired pattern, there is no need to use cat, egrep, xargs or sed.


Explanation of error:

You don't have any file to process using sed so you can't use -i.

  • -i edits the files in their place

Actually you are piping your file meyveler.txt contents as a stream to the sed command, and sed will edit it in the stdout.

  • Note that your cat command is unnecessary, xargs too.

However as steeldriver mentioned that, you are looking for:

egrep "^[^a-z].*$" meyveler.txt | xargs -Iline sed -i "/line/d" meyveler.txt
12

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy