问题
I have a file containing the following lines (3 fields delimited by space):
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
I need to manipulate the field 2 to cut its long prefix (same as you do in bash with ${string##*}) and to get the following result:
component1 test 12344
component2 master abcefa123
component3 trunk 72812
component4 integration bc89fa
component5 trunk 989091
component6 test bc7829ac
component7 various eded34512
I have no idea how to do it.
回答1:
1st solution: Could you please try following, written and tested with shown samples in GNU awk
.
awk '{num=split($2,arr,"/");$2=arr[num]} 1' Input_file
2nd solution: OR with shown samples only try setting field separators as space or /
.
awk -F'[ /]' '{print $1,$(NF-1),$NF}' Input_file
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.
回答2:
I would use AWK
for that following way, let content of file.txt
be:
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
then
awk '{sub(/^.*\//, "", $2);print}' file.txt
outputs:
component1 test 12344
component2 master abcefa123
component3 trunk 72812
component4 integration bc89fa
component5 trunk 989091
component6 test bc7829ac
component7 various eded34512
Explanation I simply replace everything from begin up to last /
(which needs to be escaped hence \
) in column of interest with empty string, then print
it.
(tested in GNU Awk 5.0.1)
回答3:
A solution with awk:
awk '{ split($2,s,"/"); $2=s[length(s)]; print }' inputfile
The split($2,s,"/")
will split the second varable to an array
$2=s[length(s)];
will assign the second variable with the last value of the array
print
will print the comlete line.
回答4:
awk '{ split($2,map,"/");$2=map[length(map)] }1' file
Using awk. split the second space delimited field into an array called map using / as the delimiter. Substitute $2 with the last element of the map array. Print the lines with shorthand 1.
回答5:
Using sed:
sed -rn 's/(^.*)([[:space:]])(.*\/)?(.*)([[:space:]])(.*$)/\1 \4 \6/p' file
Split each line into sections based on regular expressions and substitute the line for the relevant sections, printing the result.
回答6:
Using sed
:
sed -E 's/^([^ ]* )([^/]*\/)*/\1/' infile
回答7:
And also this awk
using the loop 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
来源:https://stackoverflow.com/questions/64942896/remove-long-prefix-only-from-a-specific-field-in-all-the-lines-of-a-file