Re: sed in a batch(DOS) file

From: William Allen (_wa__at_email.com)
Date: 06/25/05


Date: Sat, 25 Jun 2005 16:38:18 +0100


<ehabaziz2001@gmail.com>
> I need to run a batch file to substitute a string among certain kind of
> files . How can I do it using for command espcially I have to redirect
> reults to a new file .
>
> e.g :
>
> sed -e"1,$s/management/Management/" cust.prg > cust.ppp
>
> How can I run that command among certain kinds of files?

Reply from alt.msdos.batch:

You need to specify what you mean by "certain kinds of files",
preferably with a wildcard that fits the files you mean. And it
helps to know what Operating System you are using, since
the Batch syntax varies with OS. For example, to apply the
substitution command:
  SED "s/management/Management/"

to all files matching *.PRG on a Windows 2000 or similar
system, and write the output in each case to the corresponding
 *.PPP file, you could do this:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
  @ECHO OFF
  FOR %%F IN (*.PRG) DO CALL :SUBST "%%F"
  GOTO :EOF

  :SUBST
  :: The ECHO ON is just to show you the command as it's executed
  ECHO ON
  SED "s/management/Management/" %1 >"%~n1.PPP"
  @ECHO OFF

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Screen capture with the above pasted as DEMO.CMD and a few example
files in the current folder:

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo.cmd

C:\WORK>SED "s/management/Management/" "Customer 1.prg" 1>"Customer 1.PPP"

C:\WORK>SED "s/management/Management/" "Customer 2.prg" 1>"Customer 2.PPP"

C:\WORK>SED "s/management/Management/" "Customer 3.prg" 1>"Customer 3.PPP"

C:\WORK>
============End screen capture

The redirection expands to 1> to indicate that the output is written
to channel 1 = STDOUT (captured in the PPP files). You can remove
the ECHO ON and the final @ECHO OFF if the Batch file appears
to do what you want on a few test files. They are there only so you
see the commands generated as they are executed.

Note that the wildcard shouldn't conflict with the output files (in other
words it shouldn't match them), or they'll be processed themselves
by the FOR IN DO scan.

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/


Relevant Pages

  • Re: Persisting env vars in cmd windows
    ... But the piece de resistance was the "start" command, ... - Have all code for the one project in one single batch file. ... @echo off ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Multi-zip files -- Windows server
    ... native DOS versions). ... TO BATCH OR NOT TO BATCH ... and using a batch file to try and solve the problem... ... The IBM PC operating system command line is a most ...
    (comp.sys.cbm)
  • Re: del not working in .bat file
    ... Try using the CALL command inside the batch file. ... @echo off ... executes, which calls test2.bat and that executes. ...
    (microsoft.public.vb.general.discussion)
  • Re: Batch question
    ... Does the batch file use the CALL command? ... inserting ECHO and PAUSE commands after each existing line of the ... ECHO Line 1 processed ...
    (microsoft.public.windowsxp.general)
  • Re: Booming or Fuming? Are You Alive? Or Are You Fading Away?!
    ... purely of statements processed completely by the native batch command ... > command processor, only because it comes free with the OS. ... Hey, guard, it is not PERFECT, OK? ...
    (microsoft.public.win2000.cmdprompt.admin)