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 !
singsunnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
OK,

extracting the names can be done with awk:

NAME=$(awk -F"towards the cost of health care for|\\\." '/towards the cost of health care for/ {print $2}' $i)

The name of the PDF file can be obtained like this:

PDFFILE="/patients/pdf/"${i%".txt"}.pdf

The new name of the PDF file can be constructed this way:

PDFFILE_NEW="/patients/pdf/"$(echo $NAME | sed 's/ /_/g').pdf

Please let me know if you have difficulties making a script based on the above.

wmp

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
singsunnAuthor Commented:
how can i open the file for reading  ?
0
woolmilkporcCommented:
awk will do it for you.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

singsunnAuthor Commented:
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. ?
0
singsunnAuthor Commented:
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...
0
woolmilkporcCommented:
You could try this:

PDFFILE_NEW="/patients/pdf/"$(echo $NAME | tr -d '[:punct:]' | tr -d '[:cntrl:]' | sed 's/ /_/g').pdf
0
singsunnAuthor Commented:
THANKS !! WORKS AWESOME !!
0
singsunnAuthor Commented:
very prompt and to the point. thanks a lot
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.