Results 1 to 6 of 6
hi guys, i am trying to write a shell script using bash shell. i want to write a script that can read multi labels which are placed on the top ...
- 04-02-2009 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
script to read multi labes
hi guys, i am trying to write a shell script using bash shell. i want to write a script that can read multi labels which are placed on the top of a file (i used head command )and move the file in an appropriate category. so far i could only write a script which reads single labels only.this is the code i have
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>
#!/bin/bash
#This script reads the labels of documents and put them in their approriate label directory
for i in ./features/*;
do
head -1 $i > HeadFile.txt
FILE=HeadFile.txt
thfivecatDIR=category35
zerocatDIR=category0
if grep "35" $FILE;
then
mv $i $thfivecatDIR
#echo "there are $(ls -l $thfivecatDIR | wc -l) files of category35"
elif grep "0" $FILE;
then
echo "category 0 found"
mv $i $zerocatDIR
else
echo"doesnt belong to any category"
fi
done
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>
so how do i get it to read multi labels and move the files with more than one label to the directory multilabelDIR ?
for example if i have
0 35 place it in a multi label directory
0 place it in a 0label directory
i doubt whether i ve clarified my point enough
OH another problem with that script above is if i have a label 108 it places it in a olabel directory which is not my intention. I know its because i am using grep command , so is there any advice on which command to use.
help pleaseee
thank u in advance
- 04-02-2009 #2Just Joined!
- Join Date
- Oct 2004
- Posts
- 62
Hi desp_to_learn.
you are right... you didn't clarify enough!
Do you mean that in the 1st line of FILE you can have both "0" and "35"?
In this case your main problem (to capture only one label) is easily explained:
The solution is... (in the correct way to display a code)if "grep" finds "35", it doesn't test also for "0"
As you can see... I don't use the "sha-bang" (#!/bin/bash) becauseCode:# grep_labels.sh # This script reads the labels of documents and # put them in their approriate label directory for i in ./features/*; do head -1 $i > HeadFile.txt # ERROR! head -n 1 $i > HeadFile.txt # capture the 1st line FILE=HeadFile.txt thfivecatDIR=category35 zerocatDIR=category0 if grep "35" $FILE; # ERROR! if grep -w "35" $FILE then # ... match only whole words mv $i $thfivecatDIR # echo "there are $(ls -l $thfivecatDIR | # wc -l) files of category35" # test also for "0" if grep -w "0" $FILE; then echo "category 0 found" mv $i $zerocatDIR fi elif grep "0" $FILE; # elif grep -w "0" $FILE; then # ... to avoid 108! echo "category 0 found" mv $i $zerocatDIR else echo "doesn't belong to any category" fi done
I run the script with "bash grep_labels.sh".
A warning... I didn't test my solution.
I wouldn't use a script but a one-liner expression using find/exec...
Bye.
- 04-03-2009 #3Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
thank you a million fiomba, it is now working half way to my intention.
now my question is how do i get it to place the documents into their category according to the second label?
for example;
if there are two labels say
0 35
it should place it to the 35 label directory.(the script i have now will place it to directory 0label)
if there is only one label say
0
it should place it to the 0 label directory.
Also since i have a long list of different labels how can i use "case statement" to make my code clean. i tried but the script wouldnt run at all.OR is there any idea how to make it more read-friendly??
thank you
- 04-03-2009 #4Linux Enthusiast
- Join Date
- Aug 2006
- Location
- Portsmouth, UK
- Posts
- 539
If your happy that the first line will always contain the labels you need to filter, awk maybe a solution for you for example:
$NF basically prints the last field in the line.Code:[<user>@centos5 ~]$ echo "0" | awk '{ print $NF }' 0 [<user>@centos5 ~]$ echo "0 35" | awk '{ print $NF }' 35
So something along the lines of
Code:for i in $(ls -1 ./features/); do folder=$(head -n1 $i | awk '{ print $NF }'); mv ./features/$i category$folder/. done;RHCE #100-015-395
Please don't PM me with questions as no reply may offend, that's what the forums are for.
- 04-03-2009 #5Just Joined!
- Join Date
- Oct 2004
- Posts
- 62
Hi desp_to_learn,
... always the same! very thrifty when you clarify...
So (as I have understood) there are only 2 cases:
If it is so... (I'm not sure...):- "0" --> single label to be moved in 0 label dir
- "0" and "35" --> multi label to be moved in 35 label dir
The usual warning... I didn't test my solution.Code:# grep_labels.sh # This script reads the labels of documents and # put them in their approriate label directory for i in ./features/*; do head -n 1 $i > HeadFile.txt FILE=HeadFile.txt thfivecatDIR=category35 # multi label zerocatDIR=category0 # single label if grep -w "0" $FILE; then # test multi label (test also for "35") if grep -w "35" $FILE; then echo "category 35 found... multi label!" mv $i $thfivecatDIR else echo "single label" mv $i $zerocatDIR fi else echo "doesn't belong to any category" fi done
About the "case statement" you have only to google "bash case esac", but
I suggest to stick to the structure if/elif/elif..../else (well indented)
to simulate the "case"...
Bye.
- 04-03-2009 #6Linux Enthusiast
- Join Date
- Aug 2006
- Location
- Portsmouth, UK
- Posts
- 539
Your also probably better off using find instead of ls for example:
Will return the list of "regular" files in ./features ( directories exlcuded ) the printf %f\n returns only the filename with the directory part striped off.Code:find ./features -type f -printf "%f\n"
RHCE #100-015-395
Please don't PM me with questions as no reply may offend, that's what the forums are for.


Reply With Quote