2

Hi i have a linenumber

i=10

Now I want to delete that line with sed

sed '$i d' file 

But it looke like that this wont work..

any ideas?

tso
  • 187
  • 4
  • 13

4 Answers4

2

In awk. First test material:

$ cat > foo
1
2
3

Set the i:

$ i=2

Awk it:

$ awk -v line="$i" 'NR!=line' foo
1
3
fedorqui
  • 275,237
  • 103
  • 548
  • 598
James Brown
  • 36,089
  • 7
  • 43
  • 59
1
sed -i.bak "${i}d" data.txt

is what you're looking for.


Notes

  • The -i option with sed is used for inplace edit. A backup with extension .bak is created.
  • The double quotes with sed expands the shell variables
sjsam
  • 21,411
  • 5
  • 55
  • 102
0

To delete second line and show result:

sed -e '2d' data.txt

So your answer is:

sed -e "$i d" file.txt > file.txt
anishsane
  • 20,270
  • 5
  • 40
  • 73
MrElephant
  • 302
  • 4
  • 26
0

Just add strong quotes around the quoted variable:

i=10
sed ''"$i"' d' file
fd0
  • 289
  • 7
  • 10