Saturday, April 27, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 16865  / 2 Years ago, thu, february 17, 2022, 2:57:15

I am using this command to copy certain line from one file to another.Its working fine.No issue with it.



sed -f <(sed -e '1,10d; 12,$d; x; s/.*/10a/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt


The problem is instead of 10, I want to use variable VAR1 (where var1=10). The $VAR1 is not working.



I tried this command:



sed -f <(sed -e '1,$VAR1d; 12,$d; x; s/.*/10a/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt


Please help me.


More From » command-line

 Answers
4

The shell doesn't expand variables inside single quotes. You need to use double quotes. Also, as Danatela says, you also need curly brackets in this case. Since the shell will then attempt to expand the $d too, you need to escape the $.



sed -f <(sed -e "1,${VAR1}d; 12,$d; x; s/.*/10a/;p; x" ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt


I'm not sure if there are other parts in the quotes that will also need escaping since you use double quotes now (e.g. *?), so you can always switch between double and single quotes instead, using the former only when necessary.



sed -f <(sed -e '1,'"${VAR1}"'d; 12,$d; x; s/.*/10a/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt

[#29220] Saturday, February 19, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eving

Total Points: 162
Total Questions: 102
Total Answers: 112

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
1 Year ago
;