Showing posts with label lines. Show all posts
Showing posts with label lines. Show all posts

Saturday, May 10, 2014

Linux - Printing first or last 10 lines

There are many examples when we need to print first or last lines of some file. Commands head and tail ca help to do this.

Command head prints 10 first lines of file.

$ head file

or

$ cat file|head

There is command line argument -n, which says how many lines to be printed.

Example:

$ head -n 5 file

prints first 5 linest from file.

if number is negative, command head prints last n lines from file.

Command tail does oposite from command head: prints last 10 lines from a file.

$ tail file

same as command head there is argument -n which tells to command head how many last lines from a file is to be printed:

$ tail -n 7 file

prints last 7 lines from file.

If you want to print all lines in file excluded first n lines, command is:

$ tail -n +5 file

prints all lines in file, excluded first 5 lines.

Command tail is very useful in log analyzing, when you are not interested in whole file, but only last n lines.

Saturday, February 22, 2014

Linux How to concatenate text files

cat file1 file2 filen > outputfile

or if you want to concatenate all text files in directory:
cat *.txt > outputfile
or you can use >> operator to append to file:
cat file1 >> file2

Linux How to count words and lines in a text file

If you want to count words in some text file you could use:
wc -w <file>
example for file 2600.txt (War and Peace from Leo Tolstoy) from Guttenberg Project :
wc -w 2600.txt
output:
566321 2600.txt
 If you want to count lines in some text file you could use:
wc -l <file>
the same file for example
wc -l 2600.txt
output:
65008 2600.txt

Full options list for wc command:

  • -c or --bytes byte counts
  • -m or --chars character counts
  • -l or --lines newline counts
  • -L or --max-line-length lenght of longest line
  • -w or --words word counts

 also:
  • --help
  • --version