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:
where the original files are sequentially named for the months of 2007.
Using these files, I wrote the second script:
... and this works as well, producing the desired output files with the commercial names replaced, only it produces a lot of error messages:
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
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
For the first problem, you may want to explicitly use single quotes around the sed substitution to see if that helps:
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:
It's pretty easy to do the generating of your commands:
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:
Let me know if this helps at all.
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)
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)
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)
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)
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)
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)
#!/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)
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)
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)
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)