Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Please ask shortcut of your question inside question field. And add anything related to that question inside description field. –  Danatela Sep 30 '13 at 10:35
add comment

2 Answers

up vote 7 down vote accepted

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
share|improve this answer
add comment

You should use curly braces around variable name:

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

Thus $ will evaluate VAR1 instead of VAR1d.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.