Reputation: 647
I am looking for a solution that would allow me to find a pattern between the characters /
and s_
and move it to the end of the line.
I assume the following string:
"s_check_login_password= s_comm_type=TCPIP s_dest_address=10.55.28.125/22 s_org_address= s_net_proxy= s_ft_proxy="
From this, I want to get the value just after the /
of the s_dest_address
field: 22
, and move it to the end of the line.
I tried this:
sed 's/\([^/\]*[^ s_]* s_\)\(.*;\)/\2\1/'
but I guess it's not the good way. Is there any way to do it with sed?
Upvotes: 2
Views: 1901
Reputation: 2471
With sed
sed -E 's#(.*/)([^ ]*)(.*)#\1\3\2#' infile
/
to #
as sed expression separator so there's no need to escape it. ([^ ]*)
your expected match (22). (.*)
the rest of the string. \1\3\2
change the order in which capturing groups are returned.Upvotes: 0
Reputation: 133538
Since you have not pasted the expected output so based on your summary of question only I have written this.
awk 'match($0,/\/[^ s]*/){print substr($0,1,RSTART),substr($0,RSTART+RLENGTH+1),substr($0,RSTART+1,RLENGTH-1)}' Input_file
Adding a non-one liner form of solution too now.
awk '
match($0,/\/[^ s]*/){
print substr($0,1,RSTART),substr($0,RSTART+RLENGTH+1),substr($0,RSTART+1,RLENGTH-1)
}
' Input_file
Explanation:
awk '
match($0,/\/[^ s]*/){ ##Using match utility to match the REGEX where it should match from / to space s and if REGEX match found then do following:
print substr($0,1,RSTART),substr($0,RSTART+RLENGTH+1),substr($0,RSTART+1,RLENGTH-1) ##Printing substring from 1st character to till value of RSTART then print substring from value of RSTART+RLENGTH+1 to till end then print substring from RSTART+1 value to till RLENGTH-1 value. Basically RSTART and RLENGTH are the out of the box variable for awk which will be SET when a match is found of REGEX, where RSTART is starting index of match and RLENGTH is the length of the REGEX match.
}
' Input_file ##Mentioning Input_file name here.
Upvotes: 2