sed used in loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed used in loops
# 1  
Old 10-01-2008
sed used in loops

Hello:

This is my first post here, regarding my first attempt at shell scripting.I was given a large amount of word documents which I need to first make into text files, and then change the commercial names to generic names (they are lists of therapeutic drugs), as well as remove some unnecesary leading inventory numbers. After reading some tutorials, I wrote a bash script that uses a loop to batch convert the files, and another to batch replace the commercial names for generic names. The first one works well, and is like so:

Code:
#!/bin/bash
#Convert antibiotic totals from word to text

#general sales, single digit months
count=1
num=107
while [ $count -lt 10 ]; do
        /usr/bin/wvText /media/KINGSYON/rep_2007/rfarag10_0$num.doc /media/KINGSYON/rep_2007/rfarag10_0$num.txt
        num=$((num + 100))
        count=$((count + 1))
done

#general sales, double digit months
num=1007
while [ $count -lt 13 ]; do
        /usr/bin/wvText /media/KINGSYON/rep_2007/rfarag10_$num.doc /media/KINGSYON/rep_2007/rfarag10_$num.txt
        num=$((num + 100))
        count=$((count + 1))
done

where the original files are sequentially named for the months of 2007.

Using these files, I wrote the second script:

Code:
#!/bin/bash

count=1
num=107
while [ $count -lt 10 ]; do
        sed s/CEFAXONA/ceftriaxona/g /media/KINGSYON/rep_2007/rfarag10_0$num.txt > /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/TERBAC/ceftriaxona/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/ROCEPHIN/ceftriaxona/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/CEFTREX/ceftriaxona/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/CLAFORAN/cefotaxima/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/FORTUM/ceftazidima/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/FOTEXINA/cefotaxima/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/IZADIMA/ceftazidima/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/CEDAX/ceftibuten/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/MAXIPIME/cefepime/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
        sed -i s/DENVAR/cefixima/g /media/KINGSYON/rep_2007/totalgen10_0$num.txt
num=$((num + 100))
        count=$((count + 1))
done

... and this works as well, producing the desired output files with the commercial names replaced, only it produces a lot of error messages:
Quote:
sed: -e expression #1, char 11: unterminated `s' command
sed: -e expression #1, char 11: unterminated `s' command
sed: -e expression #1, char 29: unterminated `s' command
sed: -e expression #1, char 21: unterminated `s' command
which repeat in the same pattern 36 times (I think)
So my first question is: how am I invoking sed wrong?
And the second, how could I automate the reading of a list of paired medicines so I don't have to write the whole list into the script?

Thanks in advance

Edit:
Ok, it seems that all those mistakes ceased appearing after I removed the spaces included in the regular expresions or replacements from s/REGEXP/REPLACEMENT/g
Now, there is only one left... but I've searched the code line by line and I don't see it (it's 2 am... I'm tired), so, is there a way to trace the exact sed call that does this?

Edit:
Ok, bash -x helped me locate the missing space, so, any ideas on automating brand name replacement?

Edit:
Ok, thanks to Loriel, I can use something like
Code:
brand=`cat lista.txt | awk '{ print $'$inner' }'`

where $inner is a variable that gets updated as the loop goes by... now to test this

Last edited by riayi; 10-01-2008 at 07:46 AM.. Reason: Partially solved the problem
# 2  
Old 10-01-2008
For the first problem, you may want to explicitly use single quotes around the sed substitution to see if that helps:

Code:
sed -i 's/SUBSTITUTE_ME/WITH_THIS/g' filename

With the second, and actually it plays in with the first, why not just have a script that makes all of the substitutions and then you can pass it a filename?

First, you'd need to have a file that was easy to parse that listed the drugs and what their generic substitutions should be. I'm hoping you have a substition list that you read from already, and that it's in an easy format like I outline below. We can then just run through each line of that file, generate the same sed command that you were writing by hand, and apply it to the file specified on the command line.

sed -i 's/commercial_drug/generic_replacement/g' $filename

Let's assume you have a file that looks like this:

CEFTREX ceftriaxona
TERBAC ceftriaxona
MAXIPIME cefepime
(etc.. etc..)

It's pretty easy to do the generating of your commands:

Code:
#!/bin/bash

#- $IFS is the "internal field seperator" which by default is set to a space.  It needs to be set it to a newline ("<enter>") 
export IFS="
"

filename=$1 #- $filename is the file that the substitutions should take place

if test -z "$1" # test to make sure the file specified exists
then
    echo "No file specified!"
    echo
    exit 0
fi

if [ -w $filename ] #- need write permissions to do the substitutions
then
    for i in $(cat drug_substitutions) #- drug_substitutions is the file that lists <OLD DRUG> <NEW DRUG> 
    do 
        old=$(echo $i | awk '{print $1}'); 
        new=$(echo $i | awk '{print $2}'); 
        sed -i "s/$old/$new/g" $filename
    done
else
    echo "$filename does not exist or you do not have write privileges! Exiting!"
    echo;
    exit 0
fi

I added a couple of really simple checks that you should probably elaborate on if this is going to see any sort of production use. It worked fine in my testing here. My test looked like this:
Code:
(bill@shady) ~ > cat test_sub 
TERBAC is the first test
oh yea, also testing CEFTREX
but MAXIPIME is our last test

(bill@shady) ~ > cat drug_substitutions 
CEFTREX   ceftriaxona
TERBAC     ceftriaxona
MAXIPIME  cefepime

(bill@shady) ~ > ./sed_example.sh test_sub 

(bill@shady) ~ > cat test_sub 
ceftriaxona is the first test
oh yea, also testing ceftriaxona
but cefepime is our last test

Let me know if this helps at all.

Enjoy!

(More about IFS and how to use it here: Using the Bash IFS variable to make for loops split with non whitespace characters | mindspill.net)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

3. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

4. Shell Programming and Scripting

Two Loops

Hi please help. I have a file with two columns. I want to insert the value of the first column and second column in different sections of a line. code: for line in `cat $1`; for x in `cat $1 |awk '{print $2}'`; do do echo "print $line and then print $x" done done It works... (8 Replies)
Discussion started by: cinderella
8 Replies

5. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

6. Shell Programming and Scripting

Refining if loops using sed/awk

hi All, I have the following two requirements: case 1: In a file i have the below code: if ((a>b)) a=b; else a = c; by using some means i need to convert the line to the following output: Output required: case 2: In a file i have the below code: if (a>b) a=b; else a... (4 Replies)
Discussion started by: engineer
4 Replies

7. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

8. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

9. Shell Programming and Scripting

Loops within loops

I am running on HPUX using ksh. I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem. Begin code> <Some... (8 Replies)
Discussion started by: bthomas
8 Replies

10. UNIX for Dummies Questions & Answers

While loops

Hi all, I am an amateur k shell scripter and came across something I need clarification on. while ; do various commands done What is the "" condition testing for in the above while loop? THx, Bookoo (5 Replies)
Discussion started by: bookoo
5 Replies
Login or Register to Ask a Question