Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Re may not be empty

Tags:

regex

sed

I have written a small piece of code, which it take regex expression from variable and put into sed. When I run the script I get something like this.

sed: first RE may not be empty

The script delete lines according to specifications from a variable. variable could be empty.

NR=$1

sed '/'"${NR}"'/d' in.txt > out.txt

How can I fix it or is it some better function which I can use it? :)

like image 379
2 revs, 2 users 94% Avatar asked Oct 23 '25 08:10

2 revs, 2 users 94%


1 Answers

You can use:

# re is set to $1 or $^ if $1 is not provided
re="${1:-\$^}"

# $^ being an invalid regex will NOT delete anything 
sed "/$re/d" in.txt > out.txt
like image 192
anubhava Avatar answered Oct 25 '25 23:10

anubhava



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!