我有一个csv文件,其值如下:
Wt-Do-U-Do-Wit-The-Black,black
Yay-Its-Your-Birthday-Black,black
You-Are-My-Sunshine-Happy-Birthday-Red,red
You-Are-Special-Navy-Blue,navy-blue
You-Dont-Look-A-Day-Over-Fabulous-Green,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday-Pink,pink
我想在颜色名称出现之前拆分每个字符串。例如:
str1=Wt-Do-U-Do-Wit-The
str1=Yay-Its-Your-Birthday
str1=You-Are-My-Sunshine-Happy-Birthday
str1=You-Are-Special
str1=You-Dont-Look-A-Day-Over-Fabulous
str1=You-My-Friend-Are-Fabulous-Happy-Birthday
搜索我正在使用的字符串: -
if [ "$string" == *"Black"* ] && [ "$string" == *"White"* ] ; then
echo "It's there!"
else
echo "SOrry"
fi
搜索正常。但是我该怎么分裂字符串呢?
我使用的另一种方式:
colour_arr[0]='Red'
colour_arr[1]='Black'
colour_arr[2]='Navy-Blue'
colour_arr[3]='White'
inarray=$(echo ${colour_arr[@]} | grep -o "$string" | wc -w)
echo "$inarray"
但这不起作用。
答案 0 :(得分:2)
你可以使用sed
;灵感来自this answer
由于您已正确解析字符串,因此我将问题简化了一点;使用此输入文件:
This is red colour
Ball is black colour
some more words before red and more after
表示字符串的第二部分;从颜色名称开始:
sed -n -e 's/^.*\(\(red\|black\).*\)/\1/p' test
给出:
red colour
black colour
red and more after
和
sed -n -e 's/\(^.*\)\(\(red\|black\).*\)/\1/p' test
给出:
This is
Ball is
some more words before
我不会解释所有选项;因为他们在我提到的答案中得到了很好的解释。您可以使用:
在bash变量上使用sedleftpart=$(sed -n -e 's/\(^.*\)\(\(red\|black\).*\)/\1/p' <<< $INPUT_STRING)
OP改变输入格式后编辑: 我的回答仍然适用;用红色替换红色。其余的都适用。
答案 1 :(得分:1)
为您的新输入
<强>输入强>
$ cat f2
Wt-Do-U-Do-Wit-The-Black,black
Yay-Its-Your-Birthday-Black,black
You-Are-My-Sunshine-Happy-Birthday-Red,red S
You-Are-Special-Navy-Blue,navy-blue
You-Dont-Look-A-Day-Over-Fabulous-Green,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday-Pink,pink
输出(使用gawk
)
$ awk 'BEGIN{IGNORECASE=1;FS="[ ,]";OFS=","}match($1,$2){print "str1="substr($1,1,RSTART-2)}' f2
str1=Wt-Do-U-Do-Wit-The
str1=Yay-Its-Your-Birthday
str1=You-Are-My-Sunshine-Happy-Birthday
str1=You-Are-Special
str1=You-Dont-Look-A-Day-Over-Fabulous
str1=You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday
对于您的旧输入
<强>输入强>
$ cat f
"This is red colour",red
"Ball is black colour",black
"Tshirt is white colour",white
"Shoes are blue colour",blue
"This is green colour",green
<强>输出强>
$ awk 'BEGIN{FS=OFS=","}{gsub(/"/,"");match($1,$2);print "str1="substr($1,1,RSTART-1),"str2=" substr($1,RSTART) }' f
str1=This is ,str2=red colour
str1=Ball is ,str2=black colour
str1=Tshirt is ,str2=white colour
str1=Shoes are ,str2=blue colour
str1=This is ,str2=green colour
答案 2 :(得分:0)
OneLiner使用awk(gnu用于IGNORECASE)
awk -F ',' '# sepeartor of field is coma
# before first line
BEGIN{
# define case compair behaviour (ignoring the case)
IGNORECASE=1
}
# for each line
{
# substitute the pattern ( minus than field 2 content, so the color, at the end) in fields 1 by "" (remove)
sub( "-" $NF "$", "", $1)
# print the new content of filed 1 with str1= before
print "str1="$1
}' YourFile
自评论代码
SELECT SUBSTR(a.nm,1,10),
SUBSTR(lead(a.sn) over(order by a.sn ),1,5)
FROM
(SELECT 1 AS seq,'avrajit' nm,'Roy1' sn FROM dual
UNION ALL
SELECT 2 AS seq,'shubho' nm,'Roy2' sn FROM dual
UNION ALL
SELECT 3 AS seq,'papa' nm,'Roy3' sn FROM dual
UNION ALL
SELECT 3 AS seq,'romi' nm,'Roy4' sn FROM dual
)a;
答案 3 :(得分:0)
根据您的评论,您需要首先使用颜色&#34;虚线&#34;字段,而不是第二个字段的值(逗号分隔)。
如果第一个颜色是#34;虚线&#34;字段始终是最后一个字符串(短划线),您可以简单地使用
a="You-Are-My-Sunshine-Happy-Birthday-Red" ; awk -F- '{print $NF}' <<<"$a"
PS:您可以使用cut或awk隔离整行的第一个字段:
awk -f, '{print $1}' <<<"$fileline"
或cut -d, -f1 <<<"$fileline"
您可以将上述两项结合起来,以达到您的需求。
答案 4 :(得分:0)
保持简单:
$< input.txt
Wt-Do-U-Do-Wit-The-Black,black
Yay-Its-Your-Birthday-Black,black
You-Are-My-Sunshine-Happy-Birthday-Red,red
You-Are-Special-Navy-Blue,navy-blue
You-Dont-Look-A-Day-Over-Fabulous-Green,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday-Pink,pink
$sed -E 's/(-[^-]+)(,.*)/\2/g' input.txt
Wt-Do-U-Do-Wit-The,black
Yay-Its-Your-Birthday,black
You-Are-My-Sunshine-Happy-Birthday,red
You-Are-Special-Navy,navy-blue
You-Dont-Look-A-Day-Over-Fabulous,green
You-My-Friend-Are-Ridiculously-Fabulous-Happy-Birthday,pink
(注意:在我的操作系统上,OSX,sed -E用于扩展正则表达式。)