Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a line from a file using a variable line number

Tags:

regex

bash

sed

awk

This is probably a simple one to answer, but I'm stuck, so here goes.

sed '3d' filename    # (or something like that)

I'm having trouble trying to use a $VARIABLE instead of the number.

Anyone know how to get this to work, or any alternative options?

like image 254
paultop6 Avatar asked Jan 19 '26 08:01

paultop6


2 Answers

Did you mean this:

bash$ VARIABLE=3
bash$ sed "${VARIABLE}d" filename

(I'm not sure if this is correct use of the sed command, I just know that's how you would use a variable next to a letter in bash syntax.)

like image 111
too much php Avatar answered Jan 21 '26 05:01

too much php


$ variable=3
$ awk -vvar="$variable" 'NR!=var' file

using the shell(bash)

variable=3
i=1
while read -r line
do
  [ "$i" -ne "$variable" ] && echo "$line"
  ((i++))
done <"file" > newfile
like image 30
ghostdog74 Avatar answered Jan 21 '26 03:01

ghostdog74



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!