Try tail:
tail -n +2 "$FILE"
-n x
: Just print the last x
lines. tail -n 5
would give you the last 5 lines of the input. The +
sign kind of inverts the argument and make tail
print anything but the first x-1
lines. tail -n +1
would print the whole file, tail -n +2
everything but the first line, etc.
GNU tail
is much faster than sed
. tail
is also available on BSD and the -n +2
flag is consistent across both tools. Check the FreeBSD or OS X man pages for more.
The BSD version can be much slower than sed
, though. I wonder how they managed that; tail
should just read a file line by line while sed
does pretty complex operations involving interpreting a script, applying regular expressions and the like.
Note: You may be tempted to use
# THIS WILL GIVE YOU AN EMPTY FILE!
tail -n +2 "$FILE" > "$FILE"
but this will give you an empty file. The reason is that the redirection (>
) happens before tail
is invoked by the shell:
- Shell truncates file
$FILE
- Shell creates a new process for
tail
- Shell redirects stdout of the
tail
process to $FILE
tail
reads from the now empty $FILE
If you want to remove the first line inside the file, you should use:
tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"
The &&
will make sure that the file doesn't get overwritten when there is a problem.
Bit of awk should do it?
awk '{s+=$1} END {print s}' mydatafile
Note: some versions of awk have some odd behaviours if you are going to be adding anything exceeding 2^31 (2147483647). See comments for more background. One suggestion is to use printf
rather than print
:
awk '{s+=$1} END {printf "%.0f", s}' mydatafile
Best Answer
You can do it with
cut
:Explanation:
cut
: invoke the cut command-d " "
: use a single space as the delimiter (cut
uses TAB by default)-f
: specify fields to keep3-
: all the fields starting with field 3input_filename
: use this file as the input> output_filename
: write the output to this file.Alternatively, you can do it with
awk
:Explanation:
awk
: invoke the awk command$1=""; $2="";
: set field 1 and 2 to the empty stringsub(...);
: clean up the output fields because fields 1 & 2 will still be delimited by " "print
: print the modified lineinput_filename > output_filename
: same as above.