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
  • 167
  • 3
  • 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
  • 252,262
  • 96
  • 511
  • 570
James Brown
  • 34,397
  • 6
  • 36
  • 56
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
  • 20,774
  • 4
  • 49
  • 94
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
  • 19,124
  • 5
  • 38
  • 71
MrElephant
  • 210
  • 4
  • 23
0

Just add strong quotes around the quoted variable:

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