入力データに基づいて機能します:
sed -r 's/_[0-9]+//g'
以下の行を参照してください。
kent$ echo "group0 group1 n_name_0 n_name_10 n_name_20 n_name_5 n_name_40 team0 team1"|sed -r 's/_[0-9]+//g'
group0 group1 n_name n_name n_name n_name n_name team0 team1
アップデート
新しい入力用に更新
sed -r 's/(n_name)_[0-9]+/\1/g'
テスト:
kent$ echo "group_0 group_10 n_name_0 n_name_10 n_name_20 n_name_5 n_name_40 team_20 team_1"|sed -r 's/(n_name)_[0-9]+/\1/g'
group_0 group_10 n_name n_name n_name n_name n_name team_20 team_1
アップデート
シェルスクリプトでその行を使用したいと思います。したがって、以下のテストを参照してください。
kent$ ls
test.txt
kent$ cat test.txt
group_0 group_10 n_name_0 n_name_10 n_name_20 n_name_5 n_name_40 team_20 team_1
kent$ commandSed=$(sed -r 's/(n_name)_[0-9]+/\1/g' test.txt > out.txt)
kent$ ls
out.txt test.txt
kent$ cat out.txt
group_0 group_10 n_name n_name n_name n_name n_name team_20 team_1
実際、commandSedvarはここでは意味がありません。
もし、するなら:
kent$ commandSed=$(sed -r 's/(n_name)_[0-9]+/\1/g' test.txt)
(新しいファイルにリダイレクトせずに)
kent$ echo $commandSed
group_0 group_10 n_name n_name n_name n_name n_name team_20 team_1
新しいファイルとcommandSed変数の両方で出力を取得したい場合は、teeが最適です。
kent$ commandSed=$(sed -r 's/(n_name)_[0-9]+/\1/g' test.txt|tee out.txt)
kent$ echo $commandSed
group_0 group_10 n_name n_name n_name n_name n_name team_20 team_1
kent$ cat out.txt
group_0 group_10 n_name n_name n_name n_name n_name team_20 team_1