0
  1. / tmp/txtをファイルします
  2. ファイルの内容:aaa aaa aaa _bbb bbb bbb
  3. ファイル/tmp/ txt_leftを保存する必要があります:aaa aaa aaa
  4. ファイル/tmp/ txt_rightを保存する必要があります:bbb bbb bbb

!!! 変数を使用せずに解決策を探す注意!!!

4

4 に答える 4

2
awk -F '_'  '{print $1> "/tmp/txt_left"; print $2 > "/tmp/txt_right" }' /tmp/txt
于 2013-03-02T15:18:26.813 に答える
1

sed 方法:

より短く、より速く:

sed -ne $'h;s/_.*$//;w /tmp/txt_left\n;g;s/^.*_//;w /tmp/txt_right' /tmp/txt

説明: 次のように記述できます。

sed -ne '
    h;        # hold (copy current line in hold space)
    s/_.*$//; # replace from _ to end of line by nothing
    w /tmp/txt_left
              # Write current line to file
              # (filename have to be terminated by a newline)
    g;        # get (copy hold space to current line buffer)
    s/^.*_//; # replace from begin of line to _ by nothing
    w /tmp/txt_right
              # write
 ' /tmp/txt

バッシュとして

これは実際の変数ではありません。最初の引数要素を使用してジョブを実行し、終了したら引数リストを復元します。

set -- "$(</tmp/txt)" "$@"
echo >>/tmp/txt_right ${1#*_}
echo >>/tmp/txt_left ${1%_*}
shift

引数行の最初の場所で文字列のシフトを解除し、引数行よりも操作を行うので$1変数shiftは使用されず、問題なく、引数行は元の状態に戻ります

...そしてこれは純粋なbash解決策です;-)

于 2013-03-02T16:05:46.807 に答える
1

アンダースコアにスリットを入れて、線をカットしてみることができます

Cat /tmp/txt | cut -d_ -f 1 > txt_left
于 2013-03-02T15:17:10.860 に答える
0

bash プロセス置換、tee、およびカットを使用する:

tee -a >(cut -d _ -f 0 > /tmp/txt_left) >(cut -d _ -f 1 >/tmp/txt_right) < /tmp/txt
于 2013-03-03T14:58:44.367 に答える