Twosingleton
Twosingleton

Reputation: 71

linux truncate first line - is there a better method

There is the following which will truncate a file to take the first line and overwrite it, I wonder if there is a cleaner way then to do this:

touch temp.txt; cat versions.txt | head -1 > temp.txt; mv temp.txt versions.txt

Note that this does not work:

cat versions.txt | head -1 > versions.txt

and the touch is not necessary on most systems

Upvotes: 2

Views: 349

Answers (4)

iruvar
iruvar

Reputation: 23374

Here's one way of doing it in-place

ex -c ':2,$d' -c ':wq' versions.txt

Upvotes: 4

wwn
wwn

Reputation: 593

Ho about this

ONE=`awk 'NR==1 {print;exit}' versions.txt`&& echo $ONE>versions.txt 

Upvotes: 3

anubhava
anubhava

Reputation: 784898

Use sed like this:

sed -i.bak '1d' file

Upvotes: 3

user000001
user000001

Reputation: 33307

You could do it with sed in one command:

sed -i -n 1p versions.txt

Upvotes: 3

Related Questions