Do you have a method to quickly remove the first line of a file in bash shell ? I mean using sed or stuff like that.
3 Answers
One-liners in reverse order of length, portable unless noted.
sed (needs GNU sed for -i):
sed -i 1d file
ed (needs e.g. bash for $'...' expansion and here string):
ed file <<< $'1d\nw\nq'
awk:
awk NR\>1 infile > outfile
tail:
tail -n +2 infile > outfile
read + cat:
(read x; cat > outfile) < infile
bash built-ins:
while IFS= read -r; do ((i++)) && printf %s\\n "$REPLY" >> outfile; done < infile
-
4or
ed file1 <<< $'1d\nw\nq'glenn jackman– glenn jackman2011-05-16 14:58:06 +00:00Commented May 16, 2011 at 14:58 -
1In my case, tail is 15 times slower than sed. tail needs 13.5s, sed needs 0.85s. My file has ~1M lines, ~100MB. MacBook Air 2013 with SSD.jcsahnwaldt Reinstate Monica– jcsahnwaldt Reinstate Monica2016-02-01 16:26:14 +00:00Commented Feb 1, 2016 at 16:26
-
I like the
awkcmd the best.Gabriel Staples– Gabriel Staples2021-12-06 20:20:29 +00:00Commented Dec 6, 2021 at 20:20
Using dd
fn="The-BIG-FILE.txt"
fll=$(( $(head -n 1 $fn | wc -c) + 1))
dd if="$fn" of="${fn}.out" bs=1M iflags=skip_bytes skip=$fll
echo "Files differ by $(( $(find $fn* -printf "%s - \n" ; echo "0") )) bytes. First line of $fn is $fll bytes."
Add any iflags= and oflags= you might need - with commas separating them.