Sunday, January 14, 2007

Popping the first line from a file

Another trivial issue, how do you remove the first line (pop) of a file and read it's contents in a variable. Here's what I did:

head -1 somefile > sometempfile
set /p var=<sometempfile
echo %var%
tail +2 somefile > sometempfile
mv -f sometempfile somefile

I could have also used sed instead of head & tail, it would have been something like:

sed -n "1,1p" somefile (instead of head -1)
sed "1,1d" somefile > somotherfile (instead of tail +2)

The temporary file is required in XP for setting the variable value, cause XP batch files don't allow setting the variable to the output of another command, which is kind of lame!

No comments:

Post a Comment