一連の行を stdout に出力する関数が bash にあります。それらを区切り文字で1行にまとめたいと思います。
前:
one
two
three
後:
one:two:three
これを行う簡単な方法は何ですか?
使用するpaste
$ echo -e 'one\ntwo\nthree' | paste -s -d':'
one:two:three
そして別の方法:
cat file | tr -s "\n" ":"
これはあなたのために働くかもしれません:
paste -sd':' file
@glennJackmanの修正を逐語的に取る
awk '{printf("%s%s", sep, $0); sep=":"} END {print ""}' file
またはbashを指定したように
while read line ; do printf "%s:" $line ; done < file | sed s'/:$//'
これがお役に立てば幸いです
楽しみのために、ここに bash のみの方法があります。
echo $'one\n2 and 3\nfour' | { mapfile -t lines; IFS=:; echo "${lines[*]}"; }
出力
one:2 and 3:four
グループ化は、{}
配列変数を参照するすべてのコマンドが同じサブシェルで実行されるようにすることです。パイプラインが終了すると、変数は存在しなくなります。
http://www.gnu.org/software/bash/manual/bashref.html#index-mapfile-140
入力.txt
one two three
Perl ソリューション : dummy.pl
@a = `cat /home/Input.txt`;
foreach my $x (@a)
{
chomp($x);
push(@array,"$x");
}
chomp(@array);
print "@array";
次のようにスクリプトを実行します。
$> perl ダミー.pl | sed 's//:/g' > Output.txt
出力.txt
一二三