Link to home
Start Free TrialLog in
Avatar of singsunn
singsunn

asked on

need help with a bash script !

I need to achieve the following using a bash script.
I have a text file I need to read. The Portion of the text file I am interested reads something like this :  This fund is pledging up to $300 towards the cost of health care for John Smith.
I need to read the name of the patient into a variable $name = John Smith. The two words after ,towards the cost of health care for should be stored in my $name variable for later use.
I have tons of such files in  /patients/txt/ directory.. so this is what i want to achieve

LIST=`ls *.txt`
for $i in $LIST
do
...open txt file
...read the name into $name [ explained above ]
...there is another directory with pdfs, and every txt file being read, has a corresponding pdf file with the same name (besides extension) in the pdf directory. [ Eg: /patients/pdf/pg_0001.pdf  AND /patients/txt/pg_0001.txt ]
...i want to rename pg_0001.pdf with $name, with a underscore between first and last name: john_smith.pdf

done

SO in the end I will have read all my text files in the txt folder, and renamed all my pdf's based on the name i read.

Eg:

TEXT FILE          $name READ          FILE RENAMED
pg_0001.txt                John Smith             pg_0001.pdf to John_Smith.pdf
pg_0002.txt                Jane Doe               pg_0002.pdf to  Jane_Doe.pdf
pg_0003.txt                Mary Johnson        pg_0003.pdf to Mary_Johnson.pdf
.
.
.
and so on...

My directory structure is as follows:

under, patient/txt/
pg_0001.txt , pg_0002.txt, pg_0003.txt .........

under, patients/pdf/
pg_0001.pdf, pg_0002.pdf, pg_0003.pdf .........


Thanks !
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of singsunn
singsunn

ASKER

how can i open the file for reading  ?
awk will do it for you.
Thanks , This is working great but one more thing before i close this:
The name i read in the NAME variable sometimes have special characters like ( / , : ) which cannot be used to rename the pdf file. These occur in some of the file ( because the OCR didnt do a very good job while converting the pdf-image the a text file.)

How can i ignore all those characters in NAME that cant be used to rename a file. ?
sometimes the name are like Jo/n Smith, In this case I would like to use Jon Smith ignoring the /. The characters I have seen so far are / and :  If there is a way to replace all the special characters with null , great otherwise .. i might have to type in all such characters...
You could try this:

PDFFILE_NEW="/patients/pdf/"$(echo $NAME | tr -d '[:punct:]' | tr -d '[:cntrl:]' | sed 's/ /_/g').pdf
THANKS !! WORKS AWESOME !!
very prompt and to the point. thanks a lot