Perché la combinazione di modelli sed non considera "\ t" come un carattere <tab>?


1

Ho un file con alcune schede iniziali e ho provato a convertire le schede in spazi usando sed:

cat test_file | sed -e 's/[\t]/    /g'

Funziona correttamente su vari tipi di UNIX, ma sul mio Mac (Sierra) non tratta '\ t' come una escape per tab (hex 0x09), ma lo tratta come due caratteri separati.

grep gestisce correttamente la scorciatoia \ t per la scheda.

Risposte:


4

BSD sed segue il POSIX standard per BRE è, guarda man 7 re_format. In particolare il seguente:

                                             all other special charac-
 ters, including `\', lose their special significance within a bracket
 expression.

Né lo è \t trattato speciale (come il carattere di tabulazione) nel modello.

Hai le seguenti opzioni:

# Type a literal tab in the /pattern/ with the keys <control><v><tab>
's/<control><v><tab>/    /g'

# Use a character class within the bracket expression
's/[[:cntrl:]]/    /g'

# Use ansi -c quoting
$'s/\t/    /g'

# Print the tab with printf
"s/$(printf '\t')/    /g"

Mi piace l'opzione $ 's / \ t / / g'. Grazie!
jmhindle
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.