0

I have a batch file which uses sed to replace multiple strings in each line of a text file (hosts) then, later, prepend ::[space] at the beginning of each line. Well, that's the goal...

sed is used because every line must be kept, not just those with a match

Currently, each search/replace is an individual batch line. The challenge is :: is both a legitimate part of the text file and search terms but ALSO a reserved "word" in batch which indicates everything following is a comment.

These lines will not work properly:

sed -i s/::1//g hosts
sed -i s/:://g hosts
... some other stuff happens here like sort and awk (to dedupe)...
sed -i "s/^/:: /" hosts

The goal was to perfect the sequence of operations then change the structure such that sed is fed a text file with all the terms to be deleted so sed would need only 1 pass through the text file.

I tried to find a way to escape those :: characters but all the references I found (robvanderwoude, etc.) didn't show examples so... I tried to escape the :: by using ^:^: which caused sed to crash (I assume, the batch stops after that call.) I also tried changing those lines to : start "" "sed and arguments" which didn't prevent the parsing as a comment.

How can a batch file be written such that it will use sed with :: as part of sed's calling arguments?

shellter
  • 36,525
  • 7
  • 83
  • 90
  • replace each `:` with `\x3a`. Sadly, you've provided insufficient information about "some other stuff" and sample data/expected results to be more useful. – Magoo Mar 01 '23 at 19:23
  • you are using Windows `cmd.exe`? or ?? Good luck. – shellter Mar 01 '23 at 19:33
  • 1
    `:` (a batch file label), and `::`, (a broken batch file label, not a comment), are not special at all when used as you are doing, in another command especially when doublequoted. – Compo Mar 01 '23 at 20:11
  • @shellter, GNU sed 4.2.1 for Windows – user21313962 Mar 04 '23 at 16:09
  • @Compo, Correct. Notepad++'s simple display formatter colors what follows : as red and :: as green. I thought of that as "incorrect" and "correct." Thanks for enlightening me. – user21313962 Mar 04 '23 at 16:12
  • @Magoo, thanks. I should have stated the batch file freezes, I assume it waits for a return from sed which hangs. "some other stuff" is multiple lines of sed replacements followed by a DOS sort to...sort...followed by an awk call to delete successive duplicate lines. I should have worded the question more clearly. – user21313962 Mar 04 '23 at 16:15
  • There is no DOS involved here at all, you are using cmd.exe in Windows OS. A comment, is technically called a remark, and `REM` is the command for that. – Compo Mar 04 '23 at 16:21
  • I've just tried a batch containing ONLY `sed -i s/:://g hosts` and it substituted *nothing* for `::` in the file as expected, no freezing, just substitute one string for another throughout the target file. I'm using `GNU sed version 4.2.1`. Did you try replacing the colons with `\x3a` as I suggested? - That uses the hex `3a` character which is colon and completely eliminates any possibility that batch's use of `:` has anything whatsoever to do with the problem – Magoo Mar 04 '23 at 16:45
  • @Compo, semantics. I pre-date PCs and Windows started as shell on top of DOS so... :: runs faster and I prefer the way those lines are more easily distinguished from commands. – user21313962 Mar 04 '23 at 19:43
  • @Magoo, I can't explain why these lines didn't run the other night. I must have screwed something up as I was tinkering. I HAVE noticed every so often Notepad++ adds some invisible characters which cause batch files to fail. Maybe fat fingers insert a character or two and that's what happened. Even so, I hadn't thought about /x and it's something I'll probably want to use when I'm working with unicode files. Thanks. – user21313962 Mar 04 '23 at 19:48
  • Disk Operating System and Windows Operating System are two completely different things, _(I pre-date PC's too)_. Regardless, if you continue to *ab*use broken labels, `::`, as `REM`arks, you will, at some stage, discover that they will cause your Windows Command Script or Batch File to fail. They are not the same thing, and do not work in the same way. – Compo Mar 04 '23 at 20:33
  • @Compo, the question was why some lines weren't running in a batch file. It turns out the lines are perfectly legal, I'd screwed something else up and confused myself. :: at the beginning of a line causes the line to be skipped. I've never heard anything about it causing a batch to fail. Please point me to an example. I'd really like to see that. – user21313962 Mar 04 '23 at 23:04
  • Try some parenthesized code blocks. To learn more, read [this](https://stackoverflow.com/a/12408045/). – Compo Mar 04 '23 at 23:28
  • That's a great thread. I've added it to my reference archives. Thanks. I use :: only to separate major sections of batch files. That's probably why I've not had any problems. Just looked at notepad++'s batch.xml file and...it only supports :, not :: which explains the color coding discrepency. – user21313962 Mar 05 '23 at 05:30

0 Answers0