tr von coreutils für Windows verwenden

1500
R. Nec

Ich muss den Inhalt der Datei von \ nach \\ unter Windows in ein Skript umwandeln. Also habe ich Coreutils bekommen und versucht, tr \ \\ <file_in> file_out unter Windows CMD zu verwenden, aber \ in file_out remainde \ anstatt in \ umgewandelt zu werden. Die Datei enthält LFs (\ n) und CRLFs (\ r \ n), diese müssen jedoch unverändert bleiben. Gibt es eine Möglichkeit, tr für Änderungen zu verwenden, die ich versucht habe?

1

3 Antworten auf die Frage

0
barlop

tr deals with individual characters, it doesn't really deal with strings, though there is overlap.

you can replace multiple occurrences of a single character, with one occurrence( Replacing \\ with \), an option it calls 'shrink' but you'd want the other way. In theory one could say that involves an individual character just as much, nevertheless, tr cannot do that, it has no option to give it a character and state how many times it should appear. It has that shrink character option but not a repeat character option.

You can use sed, you may find you have to use single quotes rather than double quotes

 $ echo '\' | sed 's_\\_\\\\_g' \\ 

or sed 's/a/b/g' filename

syntax with sed with its s command, is sed "s/find/replace/" and putting a g modifier on the end will make sure it doesn't just stop at the first one, it replaces every occurrence. sed 's/find/replace/g' Normally people use / You can use _ i.e. s_a_b_g You don't put a / after the g.

As for \r\n and \n that sed line won't affect it. \r\n is not stored with an actual backslash, it's stored with the binary for the ascii codes it represents. 13 for \r and 10 for \n look at an ascii table and you'd see that.

0
R. Nec

Tr translates only single character. To replace it with more than one char it is needed to use sed. It can be found in GnuWin32 - port of linux tools for Windows. To replace \ with \\ you should use f.e.

 cat file | sed "s_\\_\\\\_g" 

The s is for separate mode, g stands for global - in default sed replaces only first occurence in row. \\\\ instead of \\ and \\\\\\\ of \\\\ because backslash has to be escaped using additional backslash.

In sed gnu Version 4.2.2 innerhalb von cygwin ausprobiert und schlägt fehl. $ cat ff | sed "s _ \\ _ \\\\\ g" `` sed: -e Ausdruck # 1, Zeichen 8: Unterbrochenes `s 'Kommando' Funktioniert aber mit einfachen Anführungszeichen. barlop vor 9 Jahren 0
0
dbenham

As others have noted, tr only transforms a single character into another single character.

You can use my JREPL.BAT regular expression text processing utility to easily and efficiently solve your problem. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.

jrepl "\" "\\" /l /f file.txt /o - 

The above will write each line with \r\n terminators.

If you must preserve the original line terminators, then you can use the /M multi-line option

jrepl "\" "\\" /l /m /f file.txt /o - 

The commands above use the /L literal switch. Without /L, the command interprets the search term as a regular expression, so you would need:

jrepl "\\" "\\" /m /f file.txt /o - 

Use call jrepl if you use the command within a batch script.

Interestingly, JREPL has a /T translate option that functions very similarly to the unix tr command. But it is not of any use in your situation.

Use jrepl /? to see the built-in documentation. Pipe the output to more if you want to limit the output to one screen at a time. I don't need more because my console window is configured with a large output buffer so that I can scroll up to see prior output.