ファイルのすべての行の特定のフィールドからのみ長いプレフィックスを削除しますか?

Nov 21 2020

次の行(スペースで区切られた3つのフィールド)を含むファイルがあります。

component1 /dev/user/test 12344
component2 master abcefa123
component3 trunk 72812
component4 /branch/user/integration bc89fa
component5 trunk 989091 
component6 integration/test bc7829ac
component7 /branch/dev/user/various eded34512

フィールド2を操作して、長いプレフィックスを切り取り($ {string ## *}を使用してbashで行うのと同じ)、次の結果を得る必要があります。

component1 test 12344
component2 master abcefa123
component3 trunk 72812
component4 integration bc89fa
component5 trunk 989091 
component6 test bc7829ac
component7 various eded34512

どうしたらいいのかわからない。

回答

2 RavinderSingh13 Nov 21 2020 at 12:27

最初の解決策: GNUで示されているサンプルを使用して、以下を作成し、テストしてみてくださいawk

awk '{num=split($2,arr,"/");$2=arr[num]} 1' Input_file

2番目の解決策:または、表示されているサンプルを使用して、フィールド区切り文字をスペースまたはとして設定してみてください/

awk -F'[ /]' '{print $1,$(NF-1),$NF}' Input_file

3番目の解決策(を使用sed):を使用してsed、次のように試すことができます:

sed 's/\([^ ]*\).*\/\(.*\)/\1 \2/' Input_file

説明(最初の解決策):上記の詳細な説明を追加します。

awk '                   ##Starting awk program from here.
{
  num=split($2,arr,"/") ##Splitting 2nd field into array arr with / as field separator.
                        ##num is number of total elements of array arr.
  $2=arr[num]           ##Assigning last element of arr with index of num into 2nd field.
}
1                       ##Mentioning 1 will print the current line.
' Input_file            ##mentioning Input_file name here.
2 Daweo Nov 21 2020 at 12:57

私はAWKそのために次のように使用しfile.txtます。内容を次のようにします。

component1 /dev/user/test 12344
component2 master abcefa123
component3 trunk 72812
component4 /branch/user/integration bc89fa
component5 trunk 989091 
component6 integration/test bc7829ac
component7 /branch/dev/user/various eded34512

その後

awk '{sub(/^.*\//, "", $2);print}' file.txt

出力:

component1 test 12344
component2 master abcefa123
component3 trunk 72812
component4 integration bc89fa
component5 trunk 989091 
component6 test bc7829ac
component7 various eded34512

説明対象の列の最初から最後まで/(したがってエスケープする必要がある\)すべてを空の文字列に置き換えてから、単純に置き換えますprint

(GNU Awk 5.0.1でテスト済み)

Luuk Nov 21 2020 at 12:27

awkを使用したソリューション:

awk '{ split($2,s,"/"); $2=s[length(s)]; print }' inputfile

split($2,s,"/")アレイに第varableを分割します

$2=s[length(s)]; 2番目の変数に配列の最後の値を割り当てます

print 完全な行を印刷します。

RamanSailopal Nov 21 2020 at 12:27
awk '{ split($2,map,"/");$2=map[length(map)] }1' file

awkを使用します。/を区切り文字として使用して、2番目のスペース区切りフィールドをmapという配列に分割します。$ 2をマップ配列の最後の要素に置き換えます。省略形1で行を印刷します。

RamanSailopal Nov 21 2020 at 12:43

sedの使用:

sed -rn 's/(^.*)([[:space:]])(.*\/)?(.*)([[:space:]])(.*$)/\1 \4 \6/p' file

各行を正規表現に基づいてセクションに分割し、関連するセクションをその行に置き換えて、結果を出力します。

αғsнιη Nov 21 2020 at 13:03

使用sed

sed -E 's/^([^ ]* )([^/]*\/)*/\1/' infile
CarlosPascual Nov 22 2020 at 10:08

また、これawkはループを使用していますwhile

awk '{while ( n=split($2,a,/\//) ) {$2=a[n];print;next}}' file
component1 test 12344
component2 master abcefa123
component3 trunk 72812
component4 integration bc89fa
component5 trunk 989091
component6 test bc7829ac
component7 various eded34512