Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a certain line in a file

Status
Not open for further replies.

ianicr

IS-IT--Management
Nov 4, 2003
230
GB
Is there a way to delete one line from a file without opening it in a text editor? Thanks.
 
Many ways.
man sed
man grep (-v option)
man awk
man ex
...

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
You can launch vi in non interactive mode, something like:
#script.sh
#
FILE=$1 #filename
LINE=$2 #line number
vi your_file <<EOF > /dev/null
:$LINE
dd
:wq!
EOF
 
ianicr,

Below are a few examples with Sed.

To delete from line 20 down in a file:
sed '20,$d' filename

To delete lines 20 to 25 in a file:
sed ’20,25d’ filename

To delete line 1 in a file:
sed '1d' filename

To delete all lines with the string “Warning”:
sed ‘/Warning/d’ filename

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top