1
1. How to print/display the first line of a file?
Explanation: $> head -1 file.txt

Directing to forums.. wait...!

2. How to print/display the last line of a file?
Explanation: $> tail -1 file.txt

Directing to forums.. wait...!

3. How to display n-th line of a file?
Explanation: $> sed %u2013n '<n> p' file.txt

Directing to forums.. wait...!

4. How to remove the first line / header from a file?
Explanation: $> sed '1 d' file.txt

Directing to forums.. wait...!

5. How to remove the last line/ trailer from a file in Unix script?
Explanation: Always remember that [sed] switch '$' refers to the last line. So using this knowledge we can deduce the below command: $> sed %u2013i '$ d' file.txt

Directing to forums.. wait...!

6. How to remove certain lines from a file in Unix?
Explanation: If you want to remove line <m> to line <n> from a given file, you can accomplish the task in the similar method shown above. Here is an example: $> sed %u2013i '5,7 d' file.txt

Directing to forums.. wait...!

7. How to remove the last n-th line from a file?
Explanation: This is bit tricky. Suppose your file contains 100 lines and you want to remove the last 5 lines. Now if you know how many lines are there in the file, then you can simply use the above shown method and can remove all the lines from 96 to 100 like below: $> sed %u2013i '96,100 d' file.txt # alternative to command [head -95 file.txt]

Directing to forums.. wait...!

8. How to check the length of any line in a file?
Explanation: $> sed %u2013n '<n> p' file.txt

Directing to forums.. wait...!

9. How to get the nth word of a line in Unix?
Explanation: Assuming the words in the line are separated by space, we can use the [cut] command. [cut] is a very powerful and useful command and it's real easy. All you have to do to get the n-th word from the line is issue the following command: cut %u2013f<n> -d' '

Directing to forums.. wait...!

10. How to reverse a string in unix?
Explanation: $> echo "unix" | rev xinu

Directing to forums.. wait...!