Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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