I want to remove the first line of all files in a directory with .dat extension. Can any one help me on this with a small script. I want the file names to remain same .
ls *dat|xargs -i ksh -c 'sed -e "1d" {}>{}.tmp;mv {}.tmp {}'
stty: : No such device or address
you can use
# tail +2 file.dat > newfile
or
# more +2 file.dat > newfile
to get 2nd line of file onwards. I leave it to you to do the rest. Try.
Just for fun:
awk 'FNR != 1 { print >>FILENAME".tmp";next}
{ print FILENAME
">"FILENAME".tmp" }' *dat|xargs -i mv "{}.tmp" "{}"
for files in `ls *.dat` #list of all .dat file
do
cp $files $files.bck #make a backup of the file
sed '1d' -i $files #remove the first line of the file
done
Rakesh UV
just for fun: for very large files, use awk/more instead of sed '1d' method.
# wc -l file1
8998841 file1
# time sed '1d' file1 > sedtest
real 0m28.329s
user 0m26.930s
sys 0m1.000s
# time sed -n '2,$p' file1 > sedtest
real 0m33.740s
user 0m29.842s
sys 0m1.096s
# time more +2 file1 > moretest
real 0m10.629s
user 0m8.353s
sys 0m0.856s
# time awk 'NR>1' file1 > awktest
real 0m14.618s
user 0m7.476s
sys 0m1.016s
Good point ghostdog
more command seems to yield the better performance on the overall