$ cat file1 file2 file3 > file4
In this way we can use the cat command to concatenate the files and write them into another file.
We can use rev command to reverse the word.
$ echo “Unix” | revRead also: Unix interview questions and answers part – 2
$ awk 'BEGIN {ORS=""} { for(i=NF;i > 0;i--) print $i," "; print "\n"}' filenameIf field delimiter is other than space, you need to define it in BEGIN block using FS variable.
$ ls -plL | grep -v / | awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'ls -plL This command lists everything that isn’t a directory and link (files,device files, etc.)
$ grep 'unix$' filenameThe ‘$’ symbol specifies the grep command to search for the pattern at the end of the line.
Read also: Unix interview questions and answers part – 3
$ grep -w ‘unix’ filenameThe ‘-w’ option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word.
For example: In the string “unix_os”, the pattern “unix” is found. However “unix” is not a whole word in that string.
$ sed -i '1,2 d' filenameIf you want to really delete the first 2 lines from the original file then you can make sed command interactive with the -i option.
This doesn’t echo anything, it will just modify the file in-place. If you don’t need to pipe the result to another command, this is easier.
Or
$ tail -n +3 input_file > output_fileNote that it’s +3 to eliminate first 2 lines, because +3 means “start from line 3” and tail numbers lines from 1.
Or
$ awk 'NR > 2 { print }' < file.txt
$ sed 'p' filename
Nothing will happen to the file and that will update the last modified time of the file.
$ who am i | cut -f1 -d' '