Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Replace text script


  • Please log in to reply

#1
JMedina

JMedina

    New Member

  • Member
  • Pip
  • 4 posts
Hello,

I'm in need of a script that can be run from the command prompt on a windows machine that will do the following.

Process all text files in a Dir.
As far as the processing goes I just need it to find specific text in the file and replace it with some other text.
Once processed I need the file ext to be changed from .txt to .csv.
Then the final thing would be to move the processed file to a sub directory.

Thank ahead of time for any assistance.


Jason M.
  • 0

Advertisements


#2
MS-Free

MS-Free

    Member

  • Member
  • PipPipPip
  • 425 posts

As far as the processing goes I just need it to find specific text in the file and replace it with some other text.

"Just", you say.

Seriously though, too bad your using Windows, as the Windows command prompt doesn't have that kind of functionality by default. If this was a *nix based operating system you could "grep" the files.

If your OS is XP, there is a command findstr which has similar functionality to the *nix grep.

findstr information straight from the Horse's mouth (Straight from Microsoft.

For the second part of your request, changing the line, you may have to determine a line number, reading them one by one and sending them to your new file, and then when you come to the line that needs changing, make the necessary changes.

This can be done. It may not be the shortest script ever, but it can be done. I'm fairly confident in that.

Changing the filename and moving it might be the easiest parts...
  • 0

#3
JMedina

JMedina

    New Member

  • Topic Starter
  • Member
  • Pip
  • 4 posts
If there is a way to do this via Unix commands I am all ears. I know enough to manipulate existing code to do what I want, but I am no programmer unfortunately. :)

Thanks is advance.

Jason M.
  • 0

#4
MS-Free

MS-Free

    Member

  • Member
  • PipPipPip
  • 425 posts
Well... I was just thinking you could use some sort of sed or awk statement... Sorry I can't tell you the specific syntax you'd use for either of those, as those statements are confusing to me as well...

Let's see if I can construct some sort of theoretical Bash script for you...

#!/bin/bash
cd DIRECTORYGOESHERE
# Replace DIRECTORYGOESHERE with the directory
File=$(find -name '*.txt')
Len=${#File[@]}
for (( i=0; i< $Len; i++ )); do
   sed 's/TEXTTOFIND/REPLACEMENTTEXT/g' ${File[i]}
   if ($(grep "TEXTOREPLACE" ${File[i]}) != ""); then
     new=`echo ${File[i]} | sed \e 's/txt/csv/g'`
     mv $filename $new
     mv $new SUBDIRECTORY
   fi
done
That would be in theory, how you might be able to write it as a bash script. I was going to comment through the entire thing... but then I got lazy :) - just respond with any questions on that script and I'll try to sort it out for you.

Does that fulfill your requirements?

P.S. Sorry for the delayed response, I almost wasn't going to respond, but then I realized I didn't give you all the tools you'd need to do it with Windows.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP