Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete lines from file with sed\awk?

I have file, with lines, contains ip with netmask a.b.c.d/24 w.x.y.z/32 etc How to delete delete specific row? i'm using

sed -ie "s#a.b.c.d/24##g" %filname%

but after the removal is an empty string in file.

It should run inside a script, with ip as parameter and also work in freebsd under sh.

like image 670
evilmind Avatar asked Nov 27 '12 08:11

evilmind


People also ask

How do you delete a line in a file with sed?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do I remove a specific line from a file in Unix?

To Remove the lines from the source file itself, use the -i option with sed command. If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.

Can we delete content in a file by using sed command?

There is no available to delete all contents of the file. How to delete all contents of the file using sed command.

How do I delete an AWK record?

To delete line 1, use awk 'NR!= 1'. The default action is to print the line. All of your '{next} {print}' terms can be removed.


2 Answers

Sed solution

 sed -i '/<pattern-to-match-with-proper-escape>/d' data.txt 

-i option will change the original file.

Awk solution

awk '!/<pattern-to-match-with-proper-escape>/' data.txt
like image 70
mtk Avatar answered Nov 06 '22 14:11

mtk


Using sed:

sed -i '\|a.b.c.d/24|d' file

Command line arg: For the input being command line argument, say 1st argument($1):

sed -i "\|$1|d" file

Replace $1 with appropriate argument number as is your case.

like image 41
Guru Avatar answered Nov 06 '22 14:11

Guru