need help with a bash script !

Posted on 2011-10-04
Last Modified: 2013-12-26
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 txt file 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


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.


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 !
Question by:singsunn
    LVL 68

    Accepted Solution


    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:


    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.



    Author Comment

    how can i open the file for reading  ?
    LVL 68

    Expert Comment

    awk will do it for you.

    Author Comment

    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. ?

    Author Comment

    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...
    LVL 68

    Expert Comment

    You could try this:

    PDFFILE_NEW="/patients/pdf/"$(echo $NAME | tr -d '[:punct:]' | tr -d '[:cntrl:]' | sed 's/ /_/g').pdf

    Author Comment


    Author Closing Comment

    very prompt and to the point. thanks a lot

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now