Hi,

I wish to delete the last three lines of a file iteratively. I tried this in unix shell scripting but it is not working..

lines=$(wc -l < s27.cnf)
target=$((lines-2))
sed '$target,$lines d' s27.cnf > f2.cnf
mv f2.cnf s27.cnf

I am getting an error "sed: 1: "$target,$lines d": undefined label 'arget,$lines d'
So was not sure what to use.

I would appreciate help on this.

Recommended Answers

All 4 Replies

Single quotes disable substitution, so perhaps

sed "$target,$lines d" s27.cnf > f2.cnf
sed $target','$lines' d' s27.cnf > f2.cnf

Hey there,

You can also do it this way

sed -e :a -e '$d;N;2,3ba' -e 'P;D' s27.cnf >f2.cnf

Just change the 3 to hower many lines you want to delete from the bottom

Best wishes,

Mike

Thanks...it worked...

I appreciate the help

Your welcome, of course :)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.