removing lines by sed
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 giderAnd 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 giderNow 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.txtThe 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.txtYou 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.
My solution is:
grep '^[a-z]' meyveler.txtWhich 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.
-iedits 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
catcommand is unnecessary,xargstoo.
However as steeldriver mentioned that, you are looking for:
egrep "^[^a-z].*$" meyveler.txt | xargs -Iline sed -i "/line/d" meyveler.txt 12