Linux Command Tips

Searching and Replacing in VI:

The formal syntax for searching is:


For example, suppose you want to search some text for the string “cherry.” Type the following and press ENTER:


The first match for “cherry” in your text will then be highlighted. To see if there are additional occurrences of the same string in the text, type n, and the highlight will switch to the next match, if one exists.

The syntax for replacing one string with another string in the current line is


Here “pattern” represents the old string and “replace” represents the new string. For example, to replace each occurrence of the word “lemon” in a line with “orange,” type:


The syntax for replacing every occurrence of a string in the entire text is similar. The only difference is the addition of a “%” in front of the “s”:




  • sed command
    sed -i ‘s/original/new/g’ file.txt


  • sed = Stream EDitor
  • -i = in-place (i.e. save back to the original file)
  • The command string:
    • s = the substitute command
    • original = a regular expression describing the word to replace (or just the word itself)
    • new = the text to replace it with
    • g = global (i.e. replace all and not just the first occurrence)


  • file.txt = the file name

EX.  “sed -i ‘s/”//g’ file.txt”  will remove all the double quotes in the file.txt. Don’t know why it’s not working sometime.

Also We can use sed like this way:

marco@imacs-suck: ~$ echo "The slow brown unicorn jumped over the hyper sleeping dog" > orly
marco@imacs-suck: ~$ sed s/slow/quick/ < orly > yarly
marco@imacs-suck: ~$ cat yarly
The quick brown unicorn jumped over the hyper sleeping dog

Another way which may make more sense than < strin and > strout is with pipes!

marco@imacs-suck: ~$ cat yarly | sed s/unicorn/fox/ | sed s/hyper/lazy/ > nowai
marco@imacs-suck: ~$ cat nowai 
The quick brown fox jumped over the lazy sleeping dog
  • tr command

tr ‘,’ ‘~’ < GeoIPCity.csv >newResult.csv

this will replace all the , in the GeoIPCity.csv file to the ~ and write them to the newResult.csv



  • split command:
    • spli by size and integrate them into original file.
      • split -b 1G -d bigfile bigfile-part
      • cat bigfile-part* > bigfile 
    • another way to split by the file size:
      • split --bytes=1024m original_filename /destination/path/prefix
      • cat /source/path/prefix* > original_filename
    • split by line number:
      • split –l <lineNumber.EX. 10000> original_filename /destination/path/file_prefix


Find most frequent words in a file:

    • Combination of tr, sort, uniq, head
      • tr -cs “[:alnum:]” “\n” < newText | sort  | uniq -c | sort -nr | head -3” will find the three most frequent words in that newText file.
      • Result:

10 the
10 sleeping
10 quick

    • PS. “tr -cs “[:alnum:]” “[\n*]” < newText |  sort  | uniq -c | sort -nr | head -3” also works for that.
    • Or  “$ sed -e ‘s/\s/\n/g’ < test.txt | sort | uniq -c | sort -nr | head -10″

Count the Specified word’s count from a file:

Count the word ‘quick’ from the file newText with different approaches:

  • cat newText | grep -c quick
  • grep -c ‘quick’ newText
  • grep -o -w ‘quick’ newText | wc –w
  • tr -cs “[:alnum:]” “\n” < newText | sort -r | uniq -c | grep quick   [Most Complicated Approach :0]

Explanation for grep options:

  • -w, –word-regexp         force PATTERN to match only whole words
  • -o, –only-matching       show only the part of a line matching PATTERN
  • -v, –invert-match        select non-matching lines


Change lower letters to the capital letters:

  • tr -s “[:upper:]” “[:lower:]” < cap


Remove all non-printable characters from myfile.txt
$ tr -cd "[:print:]" < myfile.txt

Remove all two more successive blank spaces from a copy of the text in a file called input.txt and save output to a new file called output.txt
$ tr -s ' ' ' ' < input.txt > output.txt


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s