Wc Command (Linux)

Useful options:

[1]  -l

Tail Command (Linux)

Options:

  • -f :
  • -n, –lines=Koutput the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth
    • Note: the ‘+’ has a significance.
      • If there is no ‘+’,  then tail counts K lines from the bottom of the file
      • If there is a ‘+’,  then tail counts K lines from the top of the file
  • -q, –quiet, –silentnever output headers giving file names

Tips:

Bash Shell Learnings

The Linux shell performs several kinds of Expansions

[0]  Word-splitting:

$ echo this is a     test
this is a test

  • word-splitting by the shell removed extra whitespace from the echo command’s list of arguments

[1]  Pathname Expansion  :  *

Note: Word-splitting and Pathname expansion can sometimes be the cause of frustration during command execution. See Quoting below, to understand how use of single / double quotes help.

[2] Tilde Expansion :  ~

[3] Arithmetic Expansion : $

  • Arithmetic expansion uses the form:  $((expression))

[4] Brace Expansion : {}

[5] Parameter Expansion : $

[6] Command Substitition : $,  “`”  (backquote)

  • Command substitution allows us to use the output of a command as an expansion
  • ls -l $(which cp)
  • ls -l `which cp`

Quoting:

Quoting is the way we can get control over the shell.

[1]  Double Quotes :

If we place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters.

The exceptions are “$”, “\” (backslash), and “`” (back- quote).

  • Because $ is an exception, it means that parameter expansion, arithmetic expansion, and command substitution are still carried out
  • However,  things like word-splitting, pathname expansion, tilde expansion, and brace expansion are suppressed.
  • This makes it favourable for using in commands like find. 

[2]  Single Quotes :

Suppress all expansions.

$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me

$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
text ~/*.txt {a,b} foo 4 me

$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER

[3]  Escaping Characters : 

  • Sometimes you only want to quote a single character. To do this, you can precede a character with a backslash, which in this context is called the escape character. Often this is done inside double quotes to selectively prevent an expansion:

$ echo "The balance for user $USER is: \$5.00"
The balance for user me is: $5.00

  • Backslash allows us to embed newlines in our command. Note that for this trick to work, the newline must be typed immediately after the backslash. If you put a space after the backslash, the space will be ignored, not the newline

$ ls -l \
> –reverse \
> –human-readable \
> –full-time
total 3.0K
-rwxrwx—+ 1 agoswami Domain Users 57 2015-12-17 17:21:02.810929400 -0800 file3.txt
-rwxrwx—+ 1 agoswami Domain Users 18 2015-12-17 17:13:41.974365200 -0800 file2.txt
-rwxrwx—+ 1 agoswami Domain Users 21 2015-12-17 18:07:29.022681300 -0800 file1.txt

  • Backslashes are also used to insert special characters into our text. These are called backslash escape characters. e.g.
    • \n : Adding blank lines to text
    • \t : Inserting horizontal tabs to text
    • \\ : Inserts a backslash

Stdout and Stderr:

 

Uniq Command (Linux)

A very interestin gthing about uniq is that it works only on adjacent matching lines. Here’s what the man pages say.

“Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output)”

Hence more often than not, it is used in conjunction with sort

$ cat file1.txt
20
19
5
49
5
200

 

$ uniq -c file1.txt
1 20
1 19
1 5
1 49
1 5
1 200
$ sort -n file1.txt | uniq -c
2 5
1 19
1 20
1 49
1 200

Sort Command (Linux)

Options:

[1] -u:   with -c, check for strict ordering; without -c, output only the first of an equal run

[2] -n: compare according to string numerical value.  Note, without using the -n option, the sorting will be on ASCII

$ cat file
20
19
5
49
200

$ sort file
19
20
200
49
5

[3] -r:  reverse the result of comparisons

[4] -k: sort via a key; KEYDEF gives location and type

[5] -t :  use SEP instead of non-blank to blank transition

Examples:

$ cat file1.txt
20
19
5
49
200

$ cat file2.txt
25
18
5
48
200

$ sort -n file1.txt file2.txt
5
5
18
19
20
25
48
49
200
200
$ sort -nu file1.txt file2.txt
5
18
19
20
25
48
49
200

Note : how the above examples are actually working on multiple files.

 

Files with multiple fields and delimiter

$ cat file

Linux,20

Unix,30

AIX,25

Linux,25

Solaris,10

HPUX,100

  • Sort the file alphabetically on the 1st field, numerically on the 2nd field:

    $ sort -t”,” -k1,1 -k2n,2 file

    AIX,25

    HPUX,100

    Linux,20

    Linux,25

    Solaris,10

    Unix,30