Editing a file using a script

Thanks for your input MizzGail, but I'm afraid oombera is right... I'm not sure which script you refer to when you ask if it changes when it changes from one location to another... The idea is that the script stays the same and the only input the user should have, are the new pathnames in the config (.ini) files, which are entered when oombera's script asks for them. :slight_smile:

Oombera, I tried your latest script and seems to have wiped the ini files and now they only contain LOC=/u/[DIR]/main... hehehe

Oops!! That's no good! :slight_smile:

In the last script, the one line says echo $LINE >> $TMP_00 ... that should be TMP_00 without the dollar sign ..

lol! :smiley:

well it has worked for the most part! except for:
rm: TMP_00 non-existent
rm: TMP_00 non-existent
rm: TMP_00 non-existent
rm: TMP_00 non-existent
rm: TMP_00 non-existent
rm: TMP_00 non-existent
rm: TMP_00 non-existent
rm: TMP_00 non-existent
[project4] #

and it erases everything else on the line too... :confused:

but other than that, it rocks :slight_smile:

oombera, you are using commands of the form:

sed 's/this/that/'

and you have problems if the "this" ot "that" actually is a string that contains slashes. The sed s command does not require slashes. You can pick another character. Like this:

sed 's=this=that='

So just pick a delimiter character that is not used in the strings and now you have no backslash problem.

OMG! That's great Perderabo - I didn't know that! Thanks :stuck_out_tongue:

So if you wanna just request one directory, you can just use this Ypnos:

echo -n "Enter a directory: "
read DIR
for FILE in `ls`
do
  sed -e 's=/u/.*/main/=/u/'$DIR'/main/=g' < $FILE > TMP_00
  mv TMP_00 $FILE
done

Now the user can enter any symbol except the equal sign, which should be fine..

ah ha!! it works a treat! (except for erasing the rest of the line)

thank you loads, oombera and everyone else :slight_smile:

aaight
if I take the star out, so it looks like:

sed -e 's=/u/.=/u/'$DIR'=g' < $FILE > TMP_00

then it no longer erases the rest of the line, however it also inserts the user defined directory between the / and the pre-existing directory, instead of over-writing the existing....

tried a couple of . and * combos to no avail...

Do you mean there's other stuff on the line before and/or after the directory path? Even with other stuff on the line, that script should still work..

Can you post the whole line that contains the path name?

Try inserting */ after the . (<= period)
I think you're omitting the forward slash .. that seems to hack off the rest of the line..

sDataPath=/u/ttc/data:/u/ttc/ptm:/u/ttc/pfx
sTempPath=/u/ttc/data

Those are the lines that need changing... I tried what you said and now it doesn't seem to change the files at all... :confused:

Is ttc literally what you have in the file? Cause then you could just replace ttc wherever it occurs with the directory:

...
...
sed -e 's=ttc='$DIR'=g' < $FILE > TMP_00
mv TMP_00 $FILE
...
...

Nope, ttc is just one of 150+ different names there can be. Sometimes it's ttc, sometimes it's sky, mor, hai, and so on...

this is turning out to be the never ending script :wink:

small line would do the job
cd dir-of-configs
perl -pi -e "s:/your/first/dir:/your/second/dir:g" *

or perl -pi -e "s:\/your\/first\/dir:\/your\/second\/dir:g"

not sure if it works wothout scaping the slashes

Arg .. this is the script that never ends.. it goes on and on my friends... :slight_smile:

Okay.. let's see if this works:

echo -n "Enter a directory: "
read DIR
for FILE in `ls`
do
  sed -e 's=/u/.../=/u/'$DIR'/=g' < $FILE > TMP_00
  mv TMP_00 $FILE
done
rm TMP_00

Heh, when we started this thread, you said you'd been using Unix a couple days.. now it's been a couple weeks. How do you like it so far? :wink:

Quirky but amusing, oombera :slight_smile:

Quite enjoying it actually :smiley:

Hello again :smiley:

Here's the deal, I have a file and it is of the following format:

HEAD     SOME DATA      123456789
LINE  DATA DATA DATA
LINE  DATA DATA DATA
LINE  DATA DATA DATA
HEAD     SOME DATA      999999999
LINE  DATA DATA DATA
LINE  DATA DATA DATA
LINE  DATA DATA DATA
HEAD     SOME DATA      555555555
LINE  DATA DATA DATA
LINE  DATA DATA DATA
LINE  DATA DATA DATA

Is there a script that will edit a file (which will be generated every night), take the number following 'HEAD' (see example) and add it to the start of every line which begins with the word LINE to make it look like this?

HEAD   SOME DATA        123456789
123456789 LINE  DATA DATA DATA
123456789 LINE  DATA DATA DATA
123456789 LINE  DATA DATA DATA
HEAD   SOME DATA        999999999
999999999 LINE  DATA DATA DATA
999999999 LINE  DATA DATA DATA
999999999 LINE  DATA DATA DATA
HEAD   SOME DATA        555555555
555555555 LINE  DATA DATA DATA
555555555 LINE  DATA DATA DATA
555555555 LINE  DATA DATA DATA

Thank you kindly :slight_smile:

PS Everything in this file is fixed length and there are ALOT more fields that the example I use.

If you have more fields, you'll have to change $4 to reflect the field that contains the number in it.

number=`head -n 1 text3 | awk '{print $4}'`

while read LINE
do
 echo "$LINE" | grep -q "^LINE"
 if [ $? -eq 0 ]; then
  echo "$number $LINE"
 else
  echo "$LINE"
  number=`echo "$LINE" | awk '{print $4}'`
 fi
done < yourFile

hmm...

[project4] # ./import                                       
head: cannot open text3: No such file or directory (error 2)

is it a problem that LINE is also a shell command?

I sorted out that error, but it doesn't seem to have written anything to the file :confused:

That's because the code doesn't tell it to write to a file.. this does:

number=`head -n 1 yourFile | awk '{print $4}'`

while read LINE
do
 echo "$LINE" | grep -q "^LINE"
 if [ $? -eq 0 ]; then
  echo "$number $LINE" >> resultFile
 else
  echo "$LINE" >> resultFile
  number=`echo "$LINE" | awk '{print $4}'`
 fi
done < yourFile

mv resultFile yourFile

omg oombera!!! you completely and utterly ROCK! :smiley:

Thank you thank you thank you!!!!