Saturday, January 29, 2011

BASH commands : seq, fg, sort, uniq, cut, tr

Example: nano file1 - The content of the file is : 2,1,Linux,Debian,RedHat ( ',' caracter take's place of the '\n' new line caracter )
cat file1 - displays the content
1. sort file1 - sorts the content of "file1' like : 1,2,Debian,Linux,RedHat
Modify the content of the 'file1' by adding another Linux line
sort file1 - Result : 1,2,Debian,Linux,Linux,RedHat
sort file1 | uniq - Result : 1,2,Debian,Linux,RedHat. UNIQ - removes the duplicates
which sort - /bin/sort
which uniq - /usr/bin/uniq
2. cat /etc/passwd
cat /etc/passwd |cut -d: -f2 - means : extract from file 'passwd' using delimiter '-d' ':', column '-f' '2'. The results is X
cat /etc/passwd |cut -d: -f3 - return a coulmn of numbers which is the user id's of the users
cat /etc/passwd |cut -d: -f1 - return a coulmn with the user names
cat /etc/passwd |cut -d: -f1,2 - return coulmns 1 and 2
3. which tr - /usr/bin/tr - translate, squeeze, delete characters from standard input
echo TEST1 | tr A-Z a-z - replace A-Z characters to a-z characters. the result is 'test1'
echo TESST1 | tr -s A-Z - Result TEST1
echo TESST1 | tr -s A-Z | tr A-z a-z - Result test1
echo TESST1 | tr -s A-Z | tr [:upper:] [:lower:] - Result test1
echo TesT1 | tr -c A-Z z A-Z z - Result TzzTz
Example:mkdir temp2a
nano script_cmd_tr.sh - create script_cmd_tr.sh in folder temp2a
CONTENT of script_cmd_tr.sh
BEGIN of FILE
#echo 'basename $0'; #returns the name of the script 'script_cmd_tr'
#echo '$0'; #returns the name of the script with './' './script_cmd_tr'
myscriptname='basename $0'; # the name of the script will be stored in 'myscriptname'
exit;
for i in 'ls -A'
do
if [ $i = $myscriptname]
then
echo "Sorry, can't rename myself!"
elseif [ $i != $myscriptname ]
newname='echo $i | tr a-z A-Z'
mv $i $newname
fi
done
END of FILE
touch FILE{1,2,3,4,5} - creates 5 files starting with FILE1 to FILE5 in folder temp2a
./script_cmd_tr.sh - result is that changes all the file name from a-z to A-Z

No comments:

Post a Comment