bash - Linux: Extract the first line of a file -
bash - Linux: Extract the first line of a file -
i working openwrt , very little amount of space.
trying extract first line file. line needs go variable , removed file. can utilize head
set variable can't utilize tail
because far understand have tail file > newfile
, not have room sec file.
does 1 know if improve technic?
edit: can't utilize old reply (see below) openwrt since openwrt doesn't ship ed
. shame. here 2 methods:
vi
way vi
genuine editor too, next work:
vi -c ':1d' -c ':wq' file > /dev/null
we open file vi
, , utilize commands :1d
delete first line , :wq
save , quit, redirecting output /dev/null
. cool, clean, short , simple.
oh, of course of study run:
firstline=$(head -n1 file)
before running vi
command first line of file variable firstline
.
note. on scheme little memory, , when file
huge, method fails.
dd
way dd
cool tool this. dd
methods given in other answers great, yet rely on truncate
utility not ship openwrt. here's workaround:
firstline=$(head -n1 file) linelength=$(head -n1 file | wc -c) newsize=$(( $(wc -c < file) - $linelength )) dd if=file of=file bs=1 skip=$linelength conv=notrunc dd if=/dev/null of=file bs=1 count=0 seek=$newsize
this work huge files , little memory! lastly dd
command plays role of truncate
command given in other answers.
old reply was:
you can utilize ed
this:
firstline=$(printf '%s\n' 1p d wq | ed -s file.txt)
at each call, you'll first line of file file.txt
in variable firstline
, , line removed file.
linux bash shell openwrt
Comments
Post a Comment