OT: help with a text file

I figure this group must be full of crusty old school text file hackers... Here's the deal, I wish to change a line to something else inside a pure text file, while leaving the other lines unmolested. So something that reads the file line by line, if the line doesn't match my criteria dump the line as is into a new file, if it matches, substitute my new line, do so until the end of the file.

I used to use GAWK for stuff like this but I have forgotten it all.

TIA

Reply to
a7yvm109gf5d1
Loading thread data ...

Use search/replace in a text editor?

Crimson Editor is good.

John

Reply to
John Larkin

Guilty.

No reason not to use awk/gawk for this but if you do need to take the time to re-learn it, you may want to get up to speed with Perl instead. It's very C-like, can be made to behave awk-like, and is a really good tool for manipulating text files (as well as other handy uses).

If you're on Linux/BSD/*ix you probably already have it installed. For MS Windows, the usual Perl distro is a free version from ActiveState:

formatting link

--
Rich Webb     Norfolk, VA
Reply to
Rich Webb

UltraEdit can handle regular expressions in find/replace. I use it for converting HSpice or Spectre device libraries to a form compatible with PSpice. (Also handy for updating NewsProxy filters ;-)

...Jim Thompson

--
| James E.Thompson, P.E.                           |    mens     |
| Analog Innovations, Inc.                         |     et      |
| Analog/Mixed-Signal ASIC\'s and Discrete Systems  |    manus    |
| Phoenix, Arizona  85048    Skype: Contacts Only  |             |
| Voice:(480)460-2350  Fax: Available upon request |  Brass Rat  |
| E-mail Icon at http://www.analog-innovations.com |    1962     |
             
  My simulation, struggling to converge, is telling me something
    Maybe my idea of a ring of current mirrors is flawed ?:-)
Reply to
Jim Thompson

Ah, this needs to happen "programmatically", I need to change a line in the text file, about 3K, while in the batch file, then return the text file to the initial state. It is to trick the silly Concept HDL program from Cadence. I can get it to print the entire schematic to the PDF print driver from the command line. To achieve this I set the "input script" parameter to a tiny script that changes the plotter, prints and exits. But now everytime I run Concept it does this. So I need to change the input script back to what it was when I exit the batch file too. I'm almost there.

Reply to
a7yvm109gf5d1

On a sunny day (Tue, 12 Aug 2008 13:28:26 -0700 (PDT)) it happened snipped-for-privacy@netzero.com wrote in :

If you run Linux, try typing: man replace

# Little script 'rr' as example: grml: ~ # cat rr cp $1.txt $1.txt.original replace "it is raining" "the sun shines" -- $1.txt echo "the modified file reads:" cat $1.txt mv $1.txt.original $1.txt echo "the original is now restored and reads:" cat $1.txt

# Example text file '1.txt': grml: ~ # cat 1.txt Hello there it is raining How about that?

# Run the script: grml: ~ # ./rr 1

1.txt converted the modified file reads: Hello there the sun shines How about that? the original is now restored and reads: Hello there it is raining How about that?
Reply to
Jan Panteltje

Well then, I'd just write a PowerBasic program.

John

Reply to
John Larkin

Most "dumb" text editors are good at making changes in any part of the file. Many are DOS based and thus have a size limit of about 500K, and some run under a GUI with a larger limit (depends on the GUI, what is loaded in the GUI and how much RAM one has). I vaguely remember a pre-DOS editor that would take instructions from a file and apply then to another on a line-by-line basis, and so ther was no inherent limit as to the size of the file to be edited. Sounds like that is the type of editor you are talking about; no need for such foolishments. However, if you insist, just write the dern thing...

Reply to
Robert Baer

This is a good fit for "sed"

sed "s/old text/new text/" newfile

Reply to
MooseFET

[snip example of use]

Looks like 'replace' will work as OP desired, but the traditional way of doing the job with sed is worth considering too.

formatting link
describes sed, with examples, and
formatting link
has a version of sed for MSWindows.

Eg, to copy file s to t while converting lines reading 'xyz' to 'pqr': sed -e 's/^xyz$/pqr/' s > t

-jiw

Reply to
James Waldby

On a sunny day (Tue, 12 Aug 2008 23:18:14 -0500) it happened James Waldby wrote in :

Absolutely true, sed has it all, I use it sometimes. But the syntax will upset John Larkin.

Reply to
Jan Panteltje

From your headers, it looks like you are running Windows, so I assume a Windows solution would be OK.

Do you want to replace the entire line if part of it exists, or do you simply want to replace some text with some other text? If the latter, a few lines of batch should be sufficient:

@echo off setlocal del outputfile.txt for /F "delims=" %%n in (inputfile.txt) do call :fixline %%n goto :EOF

:fixline set line=%* set line=%line:lookfor=replacewith% echo %line%>>outputfile.txt goto :EOF

If your search strings contain special characters, you may run into trouble. In that case, I'd recommend a VBScript in stead. It's slightly more complicated than batch, but much more powerful and robust. If speed is an issue, I'd also recommend VBScript.

--
RoRo
Reply to
Robert Roland

VBScript ick-poo ick-poo!

If you want a language that makes this sort of job very easy goto:

formatting link

Pascal is a strongly typed language that is easy to read and code.

Reply to
MooseFET

Use SED, a streams editor in the Unix world. There's a GNU version for Windows. You need to be conversant in Regular Expression to use SED.

formatting link

-- Mark

Reply to
qrk

It seems you are confusing VBScript with a programming language. Common mistake. VBScript is a scripting language. For me, VBScript is taking over from where BATch falls short. It that context, VBScript is fantastic. Always available, and gets the job done with a minimum of effort.

In fact, seeing how ugly the batch solution turned out, I wish I had chosen VBScript for this job in stead.

The first parameter is the file to be searched. The second parameter is the destination file. The third parameter is the text to be searched for, and the fourth parameter is the text to replace the searched text with.

I have made it excessively detailed for clarity, and left out all kinds of error checks for brevity:

------------------ Option Explicit Dim fso, InputStream, OutputStream, s Dim SourceFile, DestFile, SearchFor, ReplaceWith Const ForReading = 1 SourceFile = Wscript.Arguments.Unnamed(0) DestFile = Wscript.Arguments.Unnamed(1) SearchFor = Wscript.Arguments.Unnamed(2) ReplaceWith = Wscript.Arguments.Unnamed(3) Set fso = CreateObject("Scripting.FileSystemObject") Set InputStream = fso.OpenTextFile(SourceFile, ForReading, False) Set OutputStream = fso.CreateTextFile(DestFile, True) Do While Not InputStream.AtEndOfStream s = InputStream.ReadLine s = Replace(s, SearchFor, ReplaceWith) OutputStream.WriteLine s Loop InputStream.Close OutputStream.Close

----------------

An for those who want a scripting language that is very easy to learn, there's always AutoIt3:

formatting link

--
RoRo
Reply to
Robert Roland

On a sunny day (Wed, 13 Aug 2008 21:01:51 +0200) it happened Robert Roland wrote in :

Simple? Bash: WARNING DO NOT RUN THIS, IT WILL MODIFY ALL .txt FILES!!!! for i in *.txt do echo processing $i replace "it is raining" "the sun shines" -- $i.txt done

Somebody mentioned here that there is a bash for MS windows too.

Reply to
Jan Panteltje

snipped-for-privacy@netzero.com wrote in news:19ee7cde-7010-43a4-85cd- snipped-for-privacy@l42g2000hsc.googlegroups.com:

Isn't there "file compare" software that does something similar?

Check

formatting link

Reply to
Kris Krieger

No I am not confusing VBScript with a programming language. I understand that it is a dreadful ugly thing that should never be confused with a programming language.

There is no difference between a "scripting language" and a "programming language". While "scripting languages" are generally interpreted and "programming languages" are generally compiled, this need not be the case. Lisp, basic, and APL are commonly interpreted and yet they are considered by those who use them to be programming languages.

VBScript is a pig's breakfast. It is basic trying to be more than it ever should pretend to be. I will leave the code that others can see just how completely lame it really is.

It is much harder to learn that Pascal or even plain old BASIC.

Reply to
MooseFET

sed -e 's/Old text/New text/' old_file.txt > new_file.txt

Will replace all instances of 'Old text' with 'New text' (once per line).

--
Paul Hovnanian	paul@hovnanian.com
-----------------------------------------------------------------------
Have gnu, will travel.
Reply to
Paul Hovnanian P.E.

Gawk?

Then just do a regexp thing: s/oldtext/newtext/g (using the syntax of whatever scripting language you're using.)

Good Luck! Rich

Reply to
Rich Grise

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.