This post attampts to test your Unix skills in the form of a question/answer based Unix tutorial on Unix command lines. The commands discussed here are particulary useful for the developers working in the middle-tier (e.g. ETL) systems, where they may need to interact with several *nx source systems for data retrieval.
Q1-> Printing the first line of a file?
There are many ways to do this. However the easiest way to display the first line of a file is using the [head] command.
$> head -1 abc.txt
The number 1 tell the head command to display only the one line from top.If you set it to 5 it will display top 5 lines.By default the head command display top 10 lines.
Another way can be by using [sed] command. .
$> sed '2,$ d' abc.txt
How does the above command work? The 'd' parameter basically tells [sed] to delete all the records from display from line 2 to last line of the file (last line is represented by $ symbol).
Q2-> Printing the last line of a file?
The easiest way is to use the [tail] command.
$> tail -1 abc.txt
If you want to do it using [sed] command, here is what you should write:
$> sed -n '$ p' test
From our previous answer, we already know that '$' stands for the last line of the file. So '$ p' basically prints (p for print) the last line in standard output screen. '-n' switch takes [sed] to silent mode so that [sed] does not print anything else in the output.
Q3->How will you list only directories in a perticular location?
There are two ways to do this. first one is using the [find] command.Suppose you want to list all the directories in current dir i.e [.]
$>find . -type d
It will list all the directories as well as sub directories in the current directory.However if youwish to list only the directories not the sub-directories inside those dir then
$> ls -lrt * | grep ^d
Here first we are long listing all the files,directories in the current location and then passing the result to [grep] which then filtering all the directories using "^d".
Note** "^" means start of the line.
No comments:
Post a Comment