Wednesday, October 26, 2005

fun with find and cut - explode text - bash programming

Using cut, you can explode strings based on delimiter.
For example running the following will list all files under the current directory recursively. Each filename in output will begin with ./

find
#output
./mash
./mash/rel-tag
./mash/rel-tag/inc_vars.php
./mash/rel-tag/testfile.php
./mash/rel-tag/orig_file.php
./mash/rel-tag/output.txt



If we want to just display the directory names under the current level, we can use:

find |cut -d "/" -f 2



The -d option for cut lets you specify the delimiter. Normally the delimiter is the tab character.


The -f option allows you to output certain fields only.

The above will show multiple directory names in order found. To view only the unique directories sorted alphabetically with a current folder, use

find |cut -d "/" -f 2 | uniq | sort


To view the total count of the directories, use

find | cut -d "/" -f 2|sort|uniq|wc -l




cut [OPTION]... [FILE]...

DESCRIPTION
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options
too.

-b, --bytes=LIST
output only these bytes

-c, --characters=LIST
output only these characters

-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter

-f, --fields=LIST
output only these fields; also print any line that contains no
delimiter character, unless the -s option is specified

-n with -b: donât split multibyte characters

-s, --only-delimited
do not print lines not containing delimiters

--output-delimiter=STRING
use STRING as the output delimiter the default is to use the
input delimiter

--help display this help and exit

--version
output version information and exit

Use one, and only one of -b, -c or -f. Each LIST is made up of one
range, or many ranges separated by commas. Each range is one of:

N Nâth byte, character or field, counted from 1

N- from Nâth byte, character or field, to end of line

N-M from Nâth to Mâth (included) byte, character or field



For more information on cut see man cut.


find fun


Find all directories and files by specifying a pattern. In this example, you are finding all mail folders on Ensim Webppliance.

find /home/virtual/site*/fst/var/mail/



More fun with find

find /home/virtual/site*/fst/var/mail/|grep -v /$|grep -v "#"|grep -v "@"
find /home/virtual/site*/fst/var/mail/|grep -v /$|grep -v "#"|grep -v "@"|wc -l
find /home/virtual/site*/fst/var/mail/|grep -v /$|grep -v "#"|grep -v "@"|more

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home