Zastąp przecinek znakiem nowej linii w sed

Mam plik identyfikatorów, które są oddzielone przecinkami. Próbuję zastąpić przecinki nową linijką. Próbowałem:

sed 's/,/\n/g' file
Ale to nie działa. Co przegapiłem?
 201
Author: WildBill, 2012-05-25

12 answers

Użyj tr zamiast:

tr , '\n' < file
 267
Author: Prince John Wesley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-25 16:27:21

Użyj $'string'

Aby dostać się do sed, potrzebujesz literalnego znaku nowego ukośnika. W bash przynajmniej $'' ciągi znaków zamienią \n na prawdziwy znak nowej linii, ale wtedy musisz podwoić ukośnik wsteczny, który sed zobaczy, aby uciec od nowej linii, np.

echo "a,b" | sed -e $'s/,/\\\n/g'
 229
Author: Walter Mundt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-16 20:14:55
sed 's/,/\
/g'

Działa na Mac OS X.

 102
Author: Max Nanasy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-07 20:24:55

Jeśli Twoje użycie sed jest wyrażeniem całkowicie substytucyjnym (tak jak moje), możesz również użyć perl-pe

$ echo 'foo,bar,baz' | perl -pe 's/,/,\n/g'
foo,
bar,
baz
 18
Author: nar8789,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-15 23:23:40

To działa na MacOS Mountain Lion (10.8), Solaris 10 (SunOS 5.10) i RHE Linux (Red Hat Enterprise Linux Server release 5.3, Tikanga)...

$ sed 's/{pattern}/\^J/g' foo.txt > foo2.txt

... gdzie ^J wykonuje się wykonując ctrl+v+j . Pamiętaj o \ przed ^J.

PS, wiem, że sed w RHEL jest GNU, MacOS sed jest oparty na FreeBSD, i chociaż nie jestem pewien co do sed Solaris, wierzę, że to będzie działać prawie z każdym sed. YMMV tho"...

 11
Author: Ramon Rey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-11 13:21:08

Najwyraźniej \r jest kluczem!

$ sed 's/, /\r/g' file3.txt > file4.txt

Przekształcił to:

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

Do tego:

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
 10
Author: Chris Seymour,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-10 17:55:16

Aby było kompletne, działa to również:

echo "a,b" | sed "s/,/\\$(echo -e '\n\r')/"
 5
Author: ryenus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-17 11:35:28

MacOS jest inny, istnieją dwa sposoby rozwiązania tego problemu z sed w mac

  • Najpierw użyj \'$'\n'' zastąp \n, może działać w MacOS:

    sed 's/,/\'$'\n''/g' file
    
  • Drugi, po prostu Użyj pustej linii:

    sed 's/,/\
    /g' file
    
  • Ps. Zwróć uwagę na zakres oddzielony przez '

 5
Author: Cnetwork,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-25 02:33:42

Choć spóźnię się na ten post, tylko aktualizuję swoje ustalenia. Ta odpowiedź jest tylko dla Mac OS X.

$ sed 's/new/
> /g' m1.json > m2.json
sed: 1: "s/new/
/g": unescaped newline inside substitute pattern

W powyższym poleceniu próbowałem z Shift + Enter dodać nową linię, która nie działała. Więc tym razem próbowałem z "ucieczką ""unescaped newline", jak powiedział błąd.

$ sed 's/new/\
> /g' m1.json > m2.json 

Zadziałało! (w Mac OS X 10.9.3)

 4
Author: user2300875,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-11 04:31:20

Dla wyjaśnienia: man-page of sed on OSX (10.8; Darwin Kernel Version 12.4.0) mówi:

[...]

Sed Wyrażenia Regularne

 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.

[...]

Więc chyba trzeba użyć tr - jak wspomniano powyżej-lub nifty

sed "s/,/^M
/g"

Uwaga: musisz wpisać ctrl > -V, return> aby uzyskać '^m' w edytorze vi

 2
Author: t3az0r,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-20 08:12:43

FWIW, następująca linia działa w systemie windows i zastępuje średniki w zmiennych mojej ścieżki znakiem nowej linii. Używam narzędzi zainstalowanych w moim katalogu Git bin.

echo %path% | sed -e $'s/;/\\n/g' | less
 0
Author: jlafay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-30 15:36:32

Możemy to zrobić również w sed.

Możesz spróbować tego,

sed -i~bak 's/\,/\n/g' file

- i-zmieni modyfikację w file.Be ostrożnie korzystać z tej opcji.

Mam nadzieję, że to ci pomoże.
 0
Author: sat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-09 18:31:18