Friday, May 30, 2008

Replace text based on position : sed


In one of my old post I discussed how to replace text from a particular position to another position (e.g. replace character position 7-8 with text "JK" )

Now something similar.

The input file is like this:


$ cat math.txt
4.569+12+4-9
2.3456-4.5+12
56+12+78+1234


Suppose I need to replace any '+' to '~' if it appears in 8 to 10 positions, and not all other '+'

i.e. Required output:

4.569+12~4-9
2.3456-4.5+12
56+12+78~1234


Awk code:


$ awk '{
x=substr($0,1,7)
y=substr($0,8,2)
gsub(/+/,"~",y)
z=substr($0,10)
print x y z
}' math.txt



Reference:
- awk substr function
- replace text from a particular position to a particular postilion.

Wednesday, May 28, 2008

Associative array in awk

Associative arrays are one of awk's most powerful features. I have already made some posts on this: awk report generation, count number of occurrence of the patterns in a particular field etc.

Here some more examples for you. To Start with a simple but good example:

sal.txt contains the working details of employees for a particular week(Name Day Post EffValue)


$ cat sal.txt
Jex Mon clerk 12001
Aji Tue sales 13003
Jex Wed clerk 13123
Salna Thu sales 34000
Aji Mon sales 13123


Now if you want to count how many times each employee worked in that week (Basically counting the number of occurrences of each "first field" in sal.txt)


$ awk '{count[$1]++}END{for(j in count) print j,count[j]}' sal.txt
Aji 2
Jex 2
Salna 1


And if you want add the EffValues(4th field) of each employee separately in that week


$ awk '{arr[$1]+=$4} END {for (i in arr) {print i,arr[i]}}' sal.txt
Aji 26126
Jex 25124
Salna 34000


Combining the above 2 together(Name NumDays TotalEff):


$ awk '{a[$1]++;b[$1]=b[$1]+$NF}END{for (i in a) print i,a[i],b[i]}' sal.txt
Aji 2 26126
Jex 2 25124
Salna 1 34000


Was that good ? Here is some more.

status_nw1.txt contains information about some hosts (Time Status IP ICost)


$ cat status_nw1.txt
09:21:10 UP 172.21.67.60 20
09:21:11 UP 172.21.67.62 30
09:21:12 NA 172.21.67.60 10
09:21:12 DN 172.21.67.60 40
09:21:13 UP 172.21.67.66 45
09:21:14 DN 172.21.67.62 90
09:21:15 NA 172.21.67.66 35
09:21:16 NA 172.21.67.66 15
09:21:16 DN 172.21.67.66 25



Problem1:
Find the number of occurrences of each unique ip(3rd field) while adding their ICost(last field) values (As we did above in the previous example)


$ awk '{a[$3]++;b[$3]=b[$3]+$NF}END{for (i in a) print i,a[i],b[i]}' status_nw1.txt
172.21.67.60 3 70
172.21.67.62 2 120
172.21.67.66 4 120


Problem2:
To calculate the number of occurrences of each status(2nd field) corresponding to each IPs(3rd field)


$ awk '{count["Number of "$2" for "$3]++}END{for(j in count) print j":",count[j]}' status_nw1.txt
Number of UP for 172.21.67.66: 1
Number of NA for 172.21.67.60: 1
Number of DN for 172.21.67.60: 1
Number of NA for 172.21.67.66: 2
Number of DN for 172.21.67.62: 1
Number of UP for 172.21.67.60: 1
Number of UP for 172.21.67.62: 1
Number of DN for 172.21.67.66: 1


Problem2A:
As above, but only for status=NA


$ awk '$2=="NA" {count["Number of "$2" for "$3]++}END{for(j in count) print j":",count[j]}' status_nw1.txt
Number of NA for 172.21.67.60: 1
Number of NA for 172.21.67.66: 2


Problem3:
As you can see all the times(1st field) in status_nw1.txt are in the hour:min of 09:21, now if you have to find out Number of status=NA for the time "09:21" (not 09:21.XX), here is a way:


$ awk '$2=="NA" {count["Number of "$2" for "substr($1,1,5)]++}END{for(j in count) print j"(time):",count[j]}' status_nw1.txt
Number of NA for 09:21(time): 3


I got a solution in unix.com which solves Problem2 and Problem3 together in a single script, here it is:


$ awk '{a[$4 " in " $5]++
b[$4" for "$1":"$2]++;next}
END{
for ( i in a )
print "No of "i":" a[i]
print "---------------------------"
for ( i in b )
print "No of "i"(time):" b[i]

}' FS="( )|(:)" status_nw1.txt
No of UP in 172.21.67.66:1
No of DN in 172.21.67.60:1
No of DN in 172.21.67.62:1
No of NA in 172.21.67.60:1
No of DN in 172.21.67.66:1
No of UP in 172.21.67.60:1
No of NA in 172.21.67.66:2
No of UP in 172.21.67.62:1
---------------------------
No of UP for 09:21(time):3
No of DN for 09:21(time):3
No of NA for 09:21(time):3


Related Post to awk substr function as I used above:

- Printing last 2 characters using awk substr function
- About awk substr function

Print last command line argument - bash

This is a small script to show how we can print the last command line argument passed to a script. Also printing the previous to last argument.


$ cat lcoml.sh
#!/bin/sh

eval last_arg=\$$#
echo Last Command Line Arg: $last_arg

((b4_pos=$#-1)); eval b4_last=\$$b4_pos
echo Previous to last arg: $b4_last


Executing:

$ ./lcoml.sh A 2 B 3.4 D
Last Command Line Arg: D
Previous to last arg: 3.4



Similar Post:
How to access last argument of the previous command executed in bash prompt

Tuesday, May 27, 2008

Print last 2 characters using awk substr

I have already discussed about awk substr function in one of my previous post.
Now using that I have to extract the last two characters of each line from details.out


$ cat details.out
200KG
2567.89KG
132.3MG
1200MG
Id250d hn


i.e. output required:


KG
KG
MG
MG
hn


Using awk:

$ awk '{ print substr( $0, length($0) - 1, length($0) ) }' details.out


Using sed:

$ sed 's/^.*\(..\)$/\1/' details.out


Similar Post:

- removing last two characters from the above file.

Print first or last occurrence of pattern range - awk

As you know:

We can print section of file between two regular expressions (inclusive)

by


$ awk '/firstpattern/,/secondpattern/' file


Now if I have to print the first or last occurrence of the pattern range, here is the way:

Input File:


$ cat myfile5.txt
This is a test file to print the last and first occurrence of the text between two patterns

{ BASH
SH }

Dummy line 1
{ BASH
SH
}

New to Bash Scripting ?
{
BASH
SH
}

{
BASH
SH }

Ok, about to complete

{ BASH SH }

End of the test file. Try your code now to print the same.



#Printing first occurrence of the text(s) between two patterns ({ and })

$ awk '/{/ {a=""}
#Printing first occurrence
/{/,/}/ {a=(a=="") ? $0 : a RS $0}
/}/ {exit}
END {
print a
}' myfile5.txt


Output:

{ BASH
SH }



#Printing last occurrence of the text(s) between two patterns ({ and })

$ awk '/{/ {r=""}
#Printing last occurrence
/{/,/}/ {r=(r=="") ? $0 : r RS $0}
END {
print r
}' myfile5.txt


Output:

{ BASH SH }

Monday, May 26, 2008

Count number of occurrences using awk

Input File: resVI.txt is a portion of the overall results of the class VI annual sports.


$ cat resVI.txt
AA:100m:Monday
DD:200m:Monday
AA:400m:Friday
AA:LOngJump:Tuesday
CC:HighJump:Wed
DD:1000m:Wed
BB:60kgarmrest:Mon


Now we have to calculate how many prizes each of the students(first field) won.
i.e.
Output Required:

AA (3 prizes)
BB (1 prizes)
CC (1 prizes)
DD (2 prizes)


Just we have to calculate the count of occurrences of each first filed in resVI.txt, as each line in resVI.txt corresponds to a prize in a particular category of sports.

Awk code using array:


$ awk '{count[$1]++}END{for(j in count) print j,"("count[j]" prizes)"}' FS=: resVI.txt


Individual steps would have been like this:


$ awk '$1 ~ /AA/ {++c} END {print c}' FS=: resVI.txt
3

$ awk '$1 ~ /BB/ {++c} END {print c}' FS=: resVI.txt
1

$ awk '$1 ~ /CC/ {++c} END {print c}' FS=: resVI.txt
1

$ awk '$1 ~ /DD/ {++c} END {print c}' FS=: resVI.txt
2


Similar Post of mine: Number of files modified in each month using awk

Sunday, May 25, 2008

Index of substring - awk

index is a built-in function within awk


index(s1,s2) -- The first location of s2 within s1; 0 if not found


Suppose:


$ s1="Trying to see if 172.22.23.12 is reachable; ping successful"


And substring s2:


$ s2="ping successful"


Now to find the index of s2 in s1:


$ awk -v a="$s1" -v b="$s2" 'BEGIN{print index(a,b)}'
45


Using index with awk substr function:

We will extract the pattern "ing" from the substring s2 using awk substr function like this:


$ awk -v a="$s1" -v b="$s2" 'BEGIN{print substr(b,2,3)}'
ing


Now we will find the first index where "ing" occurs in parent string s1 like this:


$ awk -v a="$s1" -v b="$s2" 'BEGIN{print index(a,substr(b,2,3))}'
4

Saturday, May 24, 2008

Grep based on position - BASH

Input File:


$ cat myfile3.txt
ABCDEF#XYZ
ASD#DFGH90
DFGT90#SWER
ASW#90234A
SAD123FGT2


As you cane see except the last line all the lines contain # in myfile3.txt.
But I have print the lines that contain # as the 4th character(position)

i.e. required output:


ASD#DFGH90
ASW#90234A


Here is the way:

$ egrep '^.{3}#' myfile3.txt

Combine 4 rows into 1 row : awk & paste

Input File:

$ cat myfile2.txt
242313
35.6
5.8
1987
543434
45.6
5.11
1985
427672
67
6
1982


Required: Combine every 4 line as 1 single line

i.e. required output:

ID Wt Ht DOB
242313 35.6 5.8 1987
543434 45.6 5.11 1985
427672 67 6 1982


Using paste:

$ echo "ID Wt Ht DOB" ;< myfile2.txt paste - - - -


Using Awk:

$ awk 'BEGIN {print "ID\tWt\tHt\tDOB"} {printf("%s",NR%4 ? $0"\t" : $0"\n")}' myfile2.txt


Explaination:

- NR being the line number.
- NR%4, if not true add a tab, if true add a newline(\n)


A similar post, if I had to make a paragraph of every 4 rows in myfile2.txt, here is a way

Replace digits with serial numbers : awk

Some reference:

substr(s,p,l) -- The substring of s starting at p and continuing for l characters
RSTART -- The location of the data matched using the match built-in function
RLENGTH -- The length of the data matched using the match built-in function


This is a good page for all awk built-in functions, variables

Input file:

$ cat myrec.dat
AA=[98345];
BB=[23333];
CC=[593503];
DD=[32445];
EE=[249];


Requirement:
- Replace the digits inside [] with some serial numbers starting from 5000 in each line

i.e. Required Output:

AA=[5001];
BB=[5002];
CC=[5003];
DD=[5004];
EE=[5005];


Awk solution:

$ awk 'BEGIN { rval=5000 }
{
rval += 1;
match($0, /([0-9]+)];$/);
aval = substr($0, RSTART, RLENGTH-2)
gsub(aval, rval);
print
}' myrec.dat


Adding some debug:

$ awk 'BEGIN { rval=5000 }
{
rval += 1;
match($0, /([0-9]+)];$/);
aval = substr($0, RSTART, RLENGTH-2)
print "actual="aval,"replacewith="rval
gsub(aval, rval)
print
}' myrec.dat


Output:

actual=98345 replacewith=5001
AA=[5001];
actual=23333 replacewith=5002
BB=[5002];
actual=593503 replacewith=5003
CC=[5003];
actual=32445 replacewith=5004
DD=[5004];
actual=249 replacewith=5005
EE=[5005];

Check for presence in a line : Awk

Input File:


$ cat atnd.txt
AA,BB,CC,DD,EE
BB,CC,EE
AA,BB,DD
AA,CC,DD,EE


Check whether AA,BB,CC,DD,EE records are present in each line.
If present, put a (P), if not put a (A)

i.e. Required Output:


AA(P) BB(P) CC(P) DD(P) EE(P)
AA(A) BB(P) CC(P) DD(A) EE(P)
AA(P) BB(P) CC(A) DD(P) EE(A)
AA(P) BB(A) CC(P) DD(P) EE(P)


Awk solution:

$ awk 'BEGIN{FS=",";OFS=" "
n=split("AA,BB,CC,DD,EE",a,FS)}
{
for(i=1;i<=n;i++) {
s=match($0,a[i])?a[i]"(P) ":a[i]"(A) "
printf("%s",s)
}
print ""
}' atnd.txt

Friday, May 23, 2008

Concatenating multiple lines - awk

The input file:


$ cat details.txt
Name:Mr Abraham
Location:
India
Name:Mrs Jeen
Location:
Holland:4354
Name:Yenn
Location:
China:3454
Beijing


Objective:
- Concate the lines into single lines such that each line starts with the pattern "Name"
i.e. Required Output:


Name:Mr Abraham Location: India
Name:Mrs Jeen Location: Holland:4354
Name:Yenn Location: China:3454 Beijing


Solution:

$ awk 'NF&&$1=RS$1' RS="Name" details.txt


A non awk way of doing the same:

$ echo `tr "\n" " " < details.txt` | sed 's/Name/%%Name/g' | tr -s "%" "\n"

Thursday, May 22, 2008

Print current,next,previous line using awk,sed - BASH

Print current line,next line,previous line using awk,sed - BASH

The sample file:

$ cat myfile.txt
AA SDF
BB DERT
CC DERA
DD TYU
EE ASDF
FF DERT


A) Print the line where pattern is found,print next line too using sed,awk,grep


i.e.
in the above file, if I search "^DD", it should print the following:

DD TYU
EE ASDF



$ awk '/^DD/{f=1;print;next}f{print;exit}' myfile.txt


One more using awk:

$ awk '
/^DD/{
print
getline
print
}' myfile.txt



$ sed -n '/^DD/{p;n;p;}' myfile.txt


$ grep -A1 "^DD" myfile.txt


B) print the line immediately after the pattern, not the line containing the pattern


i.e.

Output should be:

EE ASDF



$ awk '/^DD/{f=1;next}f{print;exit}' myfile.txt



$ awk '
/^DD/{
getline
print
}' myfile.txt



$ sed -n '/^DD/{n;p;}' myfile.txt


C) Print the line where pattern is found,print previous line too using sed,awk,grep


i.e.
in the above file, if I search "^DD", it should print the following:

CC DERA
DD TYU



$ awk '/^DD/{print x;print};{x=$0}' myfile.txt



$ grep -B1 "^DD" myfile.txt


D) print the line immediately before the pattern, not the line containing the pattern


i.e. If I search "^DD", output would be:

CC DERA



$ awk '/^DD/{print x};{x=$0}' myfile.txt

$ sed -n '/^DD/{g;1!p;};h' myfile.txt


Discussion: Use grep to print the current line (where pattern is found),its previous line, next line


$ cat myfile.txt
AA SDF
BB DERT
CC DERA
DD TYU
EE ASDF
FF DERT



$ grep -A1 -B1 "^DD" myfile.txt
CC DERA
DD TYU
EE ASDF


We can use for printing 2 lines before and 1 line after the line where pattern is found

$ grep -A1 -B2 "^DD" myfile.txt
BB DERT
CC DERA
DD TYU
EE ASDF


Similarly, printing 2 lines after and 1 line before the line where pattern is found

$ grep -A2 -B1 "^DD" myfile.txt
CC DERA
DD TYU
EE ASDF
FF DERT

Highlight a field using tput - awk

details.txt is a sample file whose last field has to be highlighted.


$ cat details.txt
Ramesh:India:IDERT23
Lenin:UK:ERT1276U
Maher:China:908TYU
Lue:Bhutan:V567UY


Required Output:


Click the picture to get a enlarge view

awk code

$ awk -v B=`tput smso` -v N=`tput rmso` '
BEGIN { FS=OFS=":" }
$NF= B $NF N {print}' details.txt


The screen shot:















Click the picture to get a enlarge view

tput smso/rmso : enables and disables standout mode for a text screen session


$ bold=`tput smso`;offbold=`tput rmso`
$ echo "BASH ${bold}SCRIPTING${offbold}"


The above will print the word SCRIPTING as highlighted.

Same like above:

$ echo "BASH `tput smso`SCRIPTING `tput rmso`"

Transpose using awk - BASH

Suppose I have to transpose(converting rows to columns and rows to columns) the following sample file.


$ cat testfile.txt
nixon:nill:xena:raj
23:24:21:12
sbank:london:japan:india


Now we have to transpose the above file. .i.e.

Required Outout:

nixon:23:sbank
nill:24:london
xena:21:japan
raj:12:india



$ awk -F : '{
for(j=1;j<=NF;j++)
{arr[j]=arr[j]":"$j}
}
END {for(i in arr)
print arr[i]}
' testfile.txt | sed 's/^://'


Output:

raj:12:india
nixon:23:sbank
nill:24:london
xena:21:japan


The above awk code is printing the output in a different order, not in the order as they are in the input file.

Here is a better solution:


$ awk -F ":" '{
for (f = 1; f <= NF; f++)
a[NR, f] = $f
}
NF > nf { nf = NF }
END {
for (f = 1; f <= nf; f++)
for (r = 1; r <= NR; r++)
printf a[r, f] (r==NR ? RS : FS)
}' testfile.txt


Output:

nixon:23:sbank
nill:24:london
xena:21:japan
raj:12:india

Tuesday, May 20, 2008

Remove or replace newlines using sed,awk,tr - BASH


$ cat week.txt
Su
Mo
Tu
We
Th
Fr
Sa


Output Required:


- a) Remove the newlines. i.e. required output:
SuMoTuWeThFrSa

- b) Replace the newlines with "|", i.e.
Su|Mo|Tu|We|Th|Fr|Sa



Remove/Replace newlines with sed


a)
$ sed -e :a -e '$!N;s/\n//;ta' week.txt
SuMoTuWeThFrSa

b)
$ sed -e :a -e '$!N;s/\n/|/;ta' week.txt
Su|Mo|Tu|We|Th|Fr|Sa


One more way of doing. But not suitable for files with large number of records, as you see the number of N's is just 1 less than number of lines in the file.


a)
$ sed 'N;N;N;N;N;N;s/\n//g' week.txt
SuMoTuWeThFrSa

b)
$ sed 'N;N;N;N;N;N;s/\n/|/g' week.txt
Su|Mo|Tu|We|Th|Fr|Sa


Remove/Replace newlines with awk

a)
$ awk '{printf "%s",$0} END {print ""}' week.txt
SuMoTuWeThFrSa

b)
$ awk '{printf "%s|",$0} END {print ""}' week.txt
Su|Mo|Tu|We|Th|Fr|Sa|

So we need to remove the last "|" in the above output.

$ awk '{printf "%s|",$0} END {print ""}' week.txt | awk '{sub(/\|$/,"");print}'
Su|Mo|Tu|We|Th|Fr|Sa


Remove/Replace newlines with tr

a)
$ tr -d '\n' < week.txt
SuMoTuWeThFrSa

b)
$ tr '\n' '|' < week.txt
Su|Mo|Tu|We|Th|Fr|Sa|

Similarly we need to remove the last "|" from the above output:
$ tr '\n' '|' < week.txt | sed 's/|$//'
Su|Mo|Tu|We|Th|Fr|Sa

Saturday, May 17, 2008

Format lines with sed

Input file:

$ cat file.txt
+*MYVAR6255
ASDF
ASWE
+*myvar2323
ASWER
+*MyVaR2312
+*myVAr2555
ASQWE


Required Output:

MYVAR 6255
myvar 2323
MyVaR 2312
myVAr 2555

i.e.
- print the lines beginning with +*, separate [Mm][yY][Vv][Aa][rR] from the digit part in all those lines.

Sed solution:

$ sed '/^+/!d; s_\+\*\([a-zA-Z]*\)\([0-9]*\)_\1 \2_' file.txt


Explanation:

/^+/!d :: Print only those lines beginning with +*
\([a-zA-Z]*\) :: Remembering the string part of the line
\([0-9]*\) :: Remembering the digit part.
Printing them using \1 and \2 with a space in between
_ is used as delimiter instead of / to avoid confusion.


If you need more clarification, feel free to post a comment :-)

Add a decimal point

ints.txt is a file containing some integer values.


$ cat ints.txt
234
2
67
9812
12890


I had to convert all the values to 2 decimal points to right.

i.e. required output:

2.34
.02
.67
98.12
128.90


Sed solution:

$ sed 's/..$/.&/;t;s/^.$/.0&/' ints.txt


Can be done just by dividing by 100 using awk like this


$ awk '{print $0/100}' ints.txt
2.34
0.02
0.67
98.12
128.9

Friday, May 16, 2008

Generate random words in linux

/usr/share/dict/words is a standard file in linux system
The idea is to print random lines from the "words" file.
The random numbers are generated by using RANDOM variable.

The script:


#!/bin/sh

WORDFILE="/usr/share/dict/words"
NUMWORDS=5

#Number of lines in $WORDFILE
tL=`awk 'NF!=0 {++c} END {print c}' $WORDFILE`

for i in `seq $NUMWORDS`
do
rnum=$((RANDOM%$tL+1))
sed -n "$rnum p" $WORDFILE
done


Executing:


$ ./rword.sh
McLuhan's
Minnie
chants
biceps's
circumcision's


Generate random numbers in BASH:

To generate 5 random numbers between 1 and 100:


$ for i in `seq 5`; do echo $((RANDOM%100+1)); done
42
11
74
47
24


More about RANDOM number generation can be found here

Remove grep from grep output of ps command

You might always see the "grep" as the list of processes when you grep for a particluar process.

$ ps -ef | grep xscreensaver
jsaikia 4050 1 0 May14 ? 00:00:00 xscreensaver -nosplash
jsaikia 14282 14245 0 16:54 pts/2 00:00:00 grep xscreensaver

If you want not to print it the output, you can do "grep -v grep".

$ ps -ef | grep xscreensaver | grep -v grep
jsaikia 4050 1 0 May14 ? 00:00:00 xscreensaver -nosplash

One more way:

$ ps -ef | grep [x]screensaver
jsaikia 4050 1 0 May14 ? 00:00:00 xscreensaver -nosplash

And if you want to make that a alias kind of function in your .bash_profile, make an entry like this in your .bash_profile


PS(){
ps -ef | grep "$@" | grep -v 'grep'
}


so that

$ PS xscreensaver
jsaikia 4050 1 0 May14 ? 00:00:00 xscreensaver -nosplash

Thursday, May 15, 2008

awk substr function

substr(string, start, length)
This returns a length-character-long substring of string, starting at character number start. The first character of a string is character number one. For example, substr("washington", 5, 3) returns "ing". If length is not present, this function returns the whole suffix of string that begins at character number start. For example, substr("washington", 5) returns "ington". This is also the case if length is greater than the number of characters remaining in the string, counting from character number start.

The descriptions about the "AWK Built-in Functions for String Manipulation" can be found here:

The following example will clear your understanding about the same function.

$ cat file.txt
FAN01MMAS2not set ahxpx0c1
FAN04MMAS4not set ptyx0c2
FAN02MMAS5not set 01cx67u
FAN06MMAS6not set opertxh2
FAN07MMAS2not set 9mcxh
FAN03MMAS8not set ptyx0c2

Required Output:
--------------------
FAN01 (not set) MMAS2 ahxpx0c1
FAN04 (not set) MMAS4 ptyx0c2
FAN02 (not set) MMAS5 01cx67u
FAN06 (not set) MMAS6 opertxh2
FAN07 (not set) MMAS2 9mcxh
FAN03 (not set) MMAS8 ptyx0c2

$ awk '{

one=substr($0,1,5)
two=substr($0,6,5)
three=substr($0,11,7)
rest=substr($0,19)
printf ("%s (%s) %s %s\n", one, three, two, rest)
}' file.txt

Sort and Add Prefix - BASH newbie

Suppose the input file 01.txt contains some numbers in random order.

$ cat 01.txt

4
50
3
1
8
2

Output Required: (A single line with all the numbers sorted and with a prefix age=)
--------------------
age=1,age=2,age=3,age=4,age=8,age=50

AWK solution:
------------------
$ sort -n 01.txt | awk '{print "age="$0}' | tr '\n' ',' | awk '{sub(/,$/,"");print}'

SED solution:
---------------------
$ sort -n 01.txt | sed 's/[0-9].*/age=&,/' | tr -d "\n" | sed 's/,$//'

Things to learn from above:
--------------------------------
1) use of & with sed.
2) use of tr command
3) awk sub function

Wednesday, May 14, 2008

Enable color support of ls - BASH

Normal "ls" does not show the file names in different color conventions. We have to add a separate option with 'ls'.

ls='ls --color=auto'

Make it a part of your ~/.bashrc in a more practical way like this:

In your .bashrc, make a entry like this:

# enable color support of ls
$ [ "$TERM" != "dumb" ] && eval "`dircolors -b`" && alias ls='ls --color=auto'

The details of TERM variable can be found here










(Click the picture to enlarge)

© Jadu Saikia http://unstableme.blogspot.com