[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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 !
0
singsunn
Asked:
singsunn
  • 5
  • 3
1 Solution
 
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
 
singsunnAuthor Commented:
how can i open the file for reading  ?
0
 
woolmilkporcCommented:
awk will do it for you.
0
Cyber Threats to Small Businesses (Part 2)

The evolving cybersecurity landscape presents SMBs with a host of new threats to their clients, their data, and their bottom line. In part 2 of this blog series, learn three quick processes Webroot’s CISO, Gary Hayslip, recommends to help small businesses beat modern threats.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now