user1744127
user1744127

Reputation:

Optimize BASH Code to Delete First and Last Line of XML Files

How can this line from a BASH script be optimized to work faster in removing the first and last lines of a directory full of XML files?

 sed -s -i -e 1d ./files/to/edit/*.xml && sed -s -i -e '$d' ./files/to/edit/*.xml

The sed command does not have to be used. Any BASH code will work; python3 would also be nice.

Upvotes: 1

Views: 391

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185530

Try that :

sed -i '1d;$d' ./files/to/edit/*.xml

It's faster, see :

time find /usr/share/doc/x* | xargs -I% sed '1d' % && sed '$d' %
real 0m0.611s
user 0m0.033s
sys 0m0.120s

time find /usr/share/doc/x* | xargs -I% sed -e '1d' -e '$d' %
real 0m0.613s
user 0m0.027s
sys 0m0.140s


time find /usr/share/doc/x* | xargs -I% sed '1d;$d' %
real 0m0.565s
user 0m0.023s
sys 0m0.140s

Upvotes: 1

Related Questions