Monday, May 26, 2014

[Solved] Linux Commands Challenge : 003

Fancy your Linux skills ? Take this challenge and answer these simple 5 questions on Linux Shell Scripting.

Update : Added solutions


  1. What command(s) would you use to extract the 3 words in the string “The boy laughed” ?
      • There are many variants for this. One of the solutions is as follows :
      • echo $string | cut -d” “ -f1 ; echo $string | cut -d” “ -f2 ; echo $string | cut -d” “ -f3
      • Do let me know about other possible ways to solve this through your comments to this post

  2. What command(s) would you use to fetch only the 6th and 7th lines of a text file : poem.txt
      • This also has many variants. One of the solutions is as follows :
      • head -n 7 poem.txt | tail -2
      • Do let me know about other possible ways to solve this through your comments to this post

  3. What command(s) would you use to delete all empty/blank lines in a file : data.txt
      • grep -v ‘^$’ data.txt

  4. What does the following sed command do ? : sed -n ‘/[0-9]/p’ data.txt
      • Prints only those lines that have a numerical in them.

  5. What command(s) would you use to count the number of files ending with .log in /var/log directory and also have the word “error” in their contents ?
      • grep -l error /var/log/*.log

No comments:

Post a Comment