Click to See Complete Forum and Search --> : replace characters within a text file.


Eddie Fantastic
03-11-2005, 07:59 AM
Hi,
I'm new to this whole linux game. Look after a windows 2000 network. I've introduced a couple of linux servers onto the network to deal mainly with internet/email side of things.

My problem, we have a very old character based backend software system. At the end of each night it produces a csv file of delivery note data. Now, sometimes one of the columns within this file contains a "#" character when it ought to be numeric, this then has a knock on effect and delivery notes won't get printed until I go into this csv file and do a search and relpace on the "#" character and replace it with a space.

The csv file sits on a windows share, I've managed to mount this as a local drive on the linux box running centos3.3 using smbclient.

What i would ideally want to do is write a script that checks the /winshare dir for a file named daily.csv removes all instances of "#" within this file, then writes a new clean file called daily2.csv to the same dir and deletes the original daily.csv file.
I guess I would then have to add this to cron or something to have it check the dir every minute or so.

Any ideas would be helpful.
Thanks

DrChuck
03-11-2005, 08:40 AM
You could use sed to do the actual filtering. The simplest manual implementaion would be:
sed -e 's/#//g' --in-place=.bak daily.csv
This comand edits the file in place, but makes a backup as well. Check the man page for details on tuning this for your specific case.
Look into cron to kick-off an automatic process.

hope this helps,
drChuck

the.spike
03-11-2005, 08:42 AM
The very simplest way to replace the #'s would be to run the following.

sed 's/\#/ /g' <input_file> > <new_file_name>

Obviously there's no error checking here and if there is a # that should be there that will go too. You now have to build the rest of the shell script round that command. I leave that as an exercise for the reader.

Check out "man sed" to find out about the stream editor.

Note : You cannot redirect the output to the same named file otherwise you will lose your data.

spike...

<edit>Bugger.. someone beat me by a minute </edit>

Eddie Fantastic
03-11-2005, 10:22 AM
Thanks to both of you, it worked a treat.

Eddie Fantastic
03-11-2005, 11:01 AM
Sorry, I'm totally new to this scripting lark.
The following works, but I'd really like to use a simple if statement that asks wether or not a DAILY.CSV exists in the /cardiff/acco dir, if so, run the script, if not, do nothing.
Since this needs to run constantly (once a minute at least), and needs to scan more files than just this one, it would prevent unnecessary work.
Thanks again.



#!/bin/sh

sed 's/\#//g' /cardiff/acco/DAILY.CSV > /cardiff/acco/DAILY2.CSV

rm -f /cardiff/acco/DAILY.CSV

Eddie Fantastic
03-11-2005, 11:08 AM
I guess it doesn't really matter, since if the file doesn't exist it simply wont run. Which appears to work fine.
But I wouldn't mind knowing how I could accomplish this, merely out of curiousity.

ph34r
03-11-2005, 11:16 AM
if [ -f /path/to/daily.csv ]
......
fi

Check the exact syntax for the if-then in bash, check the bash cheat sheet nhf or the bash howto.