In some script, it reads the directory name and get things like
66.9090_89.4450_168.0250_ABC3/
I need to extract the thing "ABC3" so I try
sed -i -e "s/开发者_Go百科_/ /g" temp_direc
so I get
66.9090 89.4450 168.0250 ABC3/
and then
read LABEL <<< $(awk '{print $4}' temp_direc)
and now I get
ABC3/
but I do not know how to remove the final "/". Furthermore, I need later to do this
echo $A" "$LABEL
being $A some string like 45.56
and I would like the output to be
45.56 ABC3
but I really get
ABC3/6
so it seems the carriage control character is somehow embedded there. How could I get rid of this and get my desired output?
A pure sed
solution would be:
sed 's#.*_\([^_/]*\)\(/\|$\)#\1#'
resp. with GNU sed
:
sed -r 's#.*_([^_/]*)(/|$)#\1#'
I chose the #
as a delimiter (instead of the usual /
) because there's a /
in the pattern which otherwise would have to be escaped.
Try simplier:
echo '66.9090_89.4450_168.0250_ABC3/' | cut -d"_" -f4 | cut -d"/" -f1
which yields ABC3
Not 100% sure what you're after here. But to get the ABC3 you have 90% of it already:
$echo '66.9090_89.4450_168.0250_ABC3/' | awk -F'_' '{print $4;}' | sed 's/\///'
ABC3
An awk only solution:
read LABEL <<< $( echo '66.9090_89.4450_168.0250_ABC3/' \
| awk -F '[_/]' '{printf("%s", $4);}' )
Opss ... I supposed that the field is ever the forth, the following line has not this limitation:
read LABEL <<< $( echo '66.9090_89.4450_168.0250_ABC3/' \
| awk -F '[_/]' '{printf("%s", $(NF -1));}' ) ; echo $LABEL
echo '66.9090_89.4450_168.0250_ABC3/' | sed -e 's/^.*_//' -e 's/.$//'
To get output like ABC3/6
indicates your $A
ends with a carriage return. You can use something like dos2unix
on your input file, or use tr
to remove the carriage returns:
A=$'45.56\r'
B="66.9090_89.4450_168.0250_ABC3/"
a=$( echo "$A" | tr -d $'\r' ) # remove all carriage returns
b=${B##*_} # remove up to (and including) the last underscore
b=${b%/} # remove a trailing slash
echo "$a $b" # ==> "45.56 ABC3"
精彩评论