Przekształcanie wielu linii w jedną linię oddzieloną przecinkami [duplikat]
to pytanie ma już odpowiedzi tutaj :
zwięzły i przenośny "join" w uniksowej linii poleceń
(9 odpowiedzi)
Zamknięty 6 lat temu .
Mam następujące dane w wielu wierszach:
foo
bar
qux
zuu
sdf
sdfasdf
To, co chcę zrobić, to przekonwertować je do jednej linii oddzielonej przecinkami:
foo,bar,qux,zuu,sdf,sdfasdf
Jaki jest najlepszy Unix jednowierszowy do tego?
90
Author: Benjamin W., 2013-04-02
8 answers
Użycie polecenia Wklej:
paste -d, -s file
157
Author: Guru,
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-04-02 08:04:09
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-04-02 08:04:09
Plik
aaa
bbb
ccc
ddd
Xargs
cat file | xargs
Wynik
aaa bbb ccc ddd
Xargs]}
cat file | xargs | sed -e 's/ /,/g'
Wynik
aaa,bbb,ccc,ddd
cat file | xargs | sed -e 's/ /,/g'
aaa,bbb,ccc,ddd
74
Author: Serhii Kuzmychov,
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-20 06:47:20
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-20 06:47:20
Można to osiągnąć na wiele sposobów. Narzędzie, którego używasz, zależy głównie od Twoich preferencji lub doświadczenia.
Użycie polecenia tr:
tr '\n' ',' < somefile
Użycie awk:
awk -F'\n' '{if(NR == 1) {printf $0} else {printf ","$0}}' somefile
10
Author: n3rV3,
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-04-02 12:34:59
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-04-02 12:34:59
xargs -a your_file | sed 's/ /,/g'
10
Author: Serhii Kuzmychov,
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
2019-03-26 22:28:05
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
2019-03-26 22:28:05
Na podstawie Twojego przykładu wejściowego, ta linia awk działa. (bez przecinka)
awk -vRS="" -vOFS=',' '$1=$1' file
Test:
kent$ echo "foo
bar
qux
zuu
sdf
sdfasdf"|awk -vRS="" -vOFS=',' '$1=$1'
foo,bar,qux,zuu,sdf,sdfasdf
7
Author: Kent,
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-04-02 08:20:27
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-04-02 08:20:27
Perl one-liner:
perl -pe'chomp, s/$/,/ unless eof' file
Lub, jeśli chcesz być bardziej tajemniczy:
perl '-peeof||chomp&&s/$/,/' file
6
Author: choroba,
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-04-02 09:41:33
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-04-02 09:41:33
sed -n 's/.*/&,/;H;$x;$s/,\n/,/g;$s/\n\(.*\)/\1/;$s/\(.*\),/\1/;$p'
1
Author: protist,
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-05-29 18:38:18
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-05-29 18:38:18
perl -pi.bak -e 'unless(eof){s/\n/,/g}' your_file
Spowoduje utworzenie kopii zapasowej oryginalnego pliku z rozszerzeniem .bak, a następnie modyfikuje oryginalny plik
1
Author: Vijay,
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-08-07 06:32:41
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-08-07 06:32:41