Saturday, October 15, 2011

Unix Commands

> To sort filenames alphabetically regardless of case

% ls -1 | sort -f

ls -1 will list the files in one single column, this output is passed to sorting, where sort -f makes sure that the list is sorted not considering the case.

> Sorting lines of a file?
sort < shiyas.txt

> find the largest file in your directory
bash-3.00$ ls -s | sort -nr
ls -s list the files and folder along with size
pass this to sort numerically and in reverse order.
the first line gives you the largest file along with its size in blocks.

Here if we need to see only the highest 5 files add head -5
bash-3.00$ ls -s | sort -nr | head -5

I have a file in which there is space after each line. what should be the unix command to remove these spaces?
I i am using "uniq" command for this purpose,  it wont work, as uniq command will consider the duplication of lines only if the duplicants are adjacent. What should i do in this case?

sort text.txt | uniq > new.txt

here the sorting will sort your file having space between each line, in a manner that the blank line will come first. Supply this output to the uniq command and now the blank lines are adjacent, hence uniq command take one among them and place this new content to a new file.
Problem solved !!

I want to display the contents of my file along with the line number. How could I achieve that?
% cat -n text.txt
here the flag n will do the job of giving line numbers

We have one alternative for this
% nl text.txt
"nl" will do the same job of "cat -n"

> test.txt has 10 lines of which 5 are blank lines. When nl test.txt is fired, it is expected that the output is a numbered lines. What will be the last number? 5 or 10
ans: 5. nl by default only numbers the lines that are not blank.

So, what should I do to number the blank lines as well?
% nl -ba test.txt
will number all the lines.

Is there any alternativ for nl command?
% nl -bt test.txt
will number only printable text


I want to number all those lines which has specific pattern.
% nl -bpORA -s: shiyas_new.txt

here "bpORA" looks for the word ORA in each line
"-s:" is used for seperator which seperates number and line like 1:


search for the line in a file for a given pattern:
%grep STATUS ../bin/fatwire.sh
here STATUS is the word you are looking for in the fatwire.sh

Output:
echo "$STATUS"
export STATUS
List down all the files under the directory which has a particular pattern in it
grep 'INGESTION' * */*

* expands your search beyond the files in the current dirctory
and */* expands your search to all files contained one directory below the current point.


I have a filename called 'wha is unix.txt' and I need to display the content of this file in screen for which I am using the command
cat 'what is unix.txt',
will this work and is there any other alternative for this?

Ofcourse this will work and you have one alternative as well. The alternative is:
cat what\ is\ unix.txt
Here what we have done is, we have used the escape character which tells the unix system to interpret the space as space itself.

No comments:

Post a Comment