且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅从文件所有行中的特定字段中删除长前缀?

更新时间:2023-12-03 18:55:10

第一个解决方案: 您能否请尝试按照GNU awk .

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

第二种解决方案: 或仅对显示的示例尝试将字段分隔符设置为空格或/.

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

第三种解决方案(使用 sed ): 使用 sed ,您可以尝试:

3rd solution(using sed): Using sed, you could try like:

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

说明(第一种解决方案): 为此添加了详细说明.

Explanation(1st solution): Adding detailed explanation for above.

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.