macos - livro - servidor debian pdf
Substituir vírgula com nova linha no sed no MacOS? (8)
Eu tenho um arquivo de id que são separados por vírgulas. Estou tentando substituir as vírgulas por uma nova linha. Eu tentei:
sed 's/,/\n/g' file
Mas isto não está funcionando. o que estou perdendo?
Aparentemente é a chave!
$ sed 's/, /\r/g' file3.txt > file4.txt
Transformou isso:
ABFS, AIRM, AMED, BOSC, CALI, ECPG, FRGI, GERN, GTIV, HSON, IQNT, JRCC, LTRE,
MACK, MIDD, NKTR, NPSP, PME, PTIX, REFR, RSOL, UBNT, UPI, YONG, ZEUS
Para isso:
ABFS
AIRM
AMED
BOSC
CALI
ECPG
FRGI
GERN
GTIV
HSON
IQNT
JRCC
LTRE
MACK
MIDD
NKTR
NPSP
PME
PTIX
REFR
RSOL
UBNT
UPI
YONG
ZEUS
Apenas para esclarecer: o man-page do sed no OSX (10.8; Darwin Kernel Version 12.4.0) diz:
[...]
Expressões regulares de Sed
The regular expressions used in sed, by default, are basic regular expressions (BREs, see re_format(7) for more information), but extended
(modern) regular expressions can be used instead if the -E flag is given. In addition, sed has the following two additions to regular
expressions:
1. In a context address, any character other than a backslash (``\'') or newline character may be used to delimit the regular expression.
Also, putting a backslash character before the delimiting character causes the character to be treated literally. For example, in the
context address \xabc\xdefx, the RE delimiter is an ``x'' and the second ``x'' stands for itself, so that the regular expression is
``abcxdef''.
2. The escape sequence \n matches a newline character embedded in the pattern space. You cannot, however, use a literal newline charac-
ter in an address or in the substitute command.
[...]
então eu acho que tem que usar tr - como mencionado acima - ou o bacana
sed "s/,/^M
/g"
nota: você tem que digitar < ctrl> -v, < return> para obter '^ M' no vi editor
FWIW, a linha a seguir funciona no Windows e substitui ponto e vírgula nas variáveis do meu caminho por uma nova linha. Estou usando as ferramentas instaladas no meu diretório git bin.
echo %path% | sed -e $'s/;/\\n/g' | less
Isso funciona no MacOS Mountain Lion (10.8), no Solaris 10 (SunOS 5.10) e no RHE Linux (Red Hat Enterprise Linux Server versão 5.3, Tikanga) ...
$ sed 's/{pattern}/\^J/g' foo.txt > foo2.txt
... onde o ^J
é feito fazendo ctrl + v + j . Lembre-se do \
antes do ^J
PS, eu sei que o sed no RHEL é GNU, o MacOS sed é baseado no FreeBSD, e embora eu não tenha certeza sobre o Solaris sed, acredito que isso funcionará praticamente com qualquer sed. YMMV tho '...
Para torná-lo completo, isso também funciona:
echo "a,b" | sed "s/,/\\$(echo -e '\n\r')/"
Se o seu uso de seds tende a ser expressões de substituição inteiramente (como o meu tende a ser), você também pode usar o perl -pe
$ echo 'foo,bar,baz' | perl -pe 's/,/,\n/g'
foo,
bar,
baz
Use tr
vez disso:
tr , '\n' < file
nós podemos fazer isso em sed também.
você pode tentar isso
sed -i~bak 's/\,/\n/g' file
-i - irá alterar a modificação no arquivo. Tenha cuidado ao usar esta opção.
Espero que isso ajude você.