Delete lines from file with SED or AWK
39,067
Solution 1
Using sed:
Delete 1st line:
sed '1d' file-name
Delete 10th line:
sed '10d' file-name
Delete line # 5 to 10
sed '5,10d' file-name
All above sed commands will write output on stdout that you can redirect to another file if you want or use -i
flag of sed to inline edit the file.
Solution 2
With awk:
# delete line 1
awk 'NR == 1 {next} {print}' file
# delete line number stored in shell variable $n
awk -v n=$n 'NR == n {next} {print}' file
# delete between lines $a and $b inclusive
awk -v m=$a -v n=$b 'm <= NR && NR <= n {next} {print}' file
To save a few chars, {print}
can be replaced just with 1
To overwrite the original file, you have to do something like this
awk '...' file > tmpfile && mv tmpfile file
Author by
bluetickk
Updated on May 11, 2020Comments
-
bluetickk almost 4 years
Ive seen many variations, very confused on how to solve these 3 problems.
- deleting all rows except the first from a file
- deleting a row from file with a line number
- deleting rows from a file with a range of line numbers
-
bluetickk about 13 yearsok this is working but the lines are just printing out and not saving to the file?
-
Beta about 13 yearsTo delete all lines but the first,
sed '2,$d' filename
, orsed '1!d' filename
, orsed -n '1p' filename
. -
jilles about 13 yearsThe way you use it,
read -r
will still strip leading and trailing whitespace. You need to doIFS= read -r line
. Furthermore note that from a shell script this method is faster for small files because it avoids a fork, but slower for large files becauseread
is inherently inefficient and usually reads one byte at a time or does one read and lseek call per invocation and string processing in bash tends to be inefficient (less so in other shells). -
bash-o-logist about 13 years@jilles, thanks I forgot about IFS= on these cases. And yes, i do know that
read
is inefficient on large files withbash
. If OP's files are large sizes and performance is an issue, then use a better tool. -
dubiousjim almost 12 years@Beta, first two are exactly right, but the third will print the first line only.
-
anubhava almost 12 years@dubiousjim:
Print first line only
is same asdelete all lines but the first
that's whysed -n '1p'
is also correct. -
dubiousjim almost 12 years@anubhava, sorry, mental glitch. I thought you were trying to do something else; don't know on what basis I thought so.
-
Andrew Dalke almost 12 yearsTo delete line 1, use awk 'NR!=1'. The default action is to print the line. All of your '{next} {print}' terms can be removed.
-
SKJ over 11 yearsLifesaver for loading a sql dump without destroying / creating the table. Many thanks :)