【问题标题】:Split string after matching the word in shell匹配shell中的单词后拆分字符串
【发布时间】:2017-06-19 03:57:05
【问题描述】:

我有一个 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"

但这不起作用。

【问题讨论】:

  • 您能否按照预期的输出提供您的准确输入,而不是发布您尝试解决问题的内容(未知)X-Y problem
  • 输入行是否有以下模式"4 words",colorname
  • @sameerkn : 第一列有不同的字符串不仅有 4 个单词,颜色名称相同
  • @Urvashi:您想将字段存储在变量或数组中吗?解析完这些行你想做什么?
  • @Urvashi: 为什么这行You-Are-My-Sunshine-Happy-Birthday-Red,red Sred 后面多了一个字符S

标签: arrays string bash shell split


【解决方案1】:

你可以使用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 变量上使用 sed:

leftpart=$(sed -n -e 's/\(^.*\)\(\(red\|black\).*\)/\1/p' <<< $INPUT_STRING)

OP 更改输入格式后编辑: 我的回答仍然适用;只需将红色替换为红色即可。其余的同样适用。

【讨论】:

  • 谢谢。它有效。 leftpart=$(sed -n -e 's/(^.*)((red\|black).*)/\1/p'
【解决方案2】:

对于您的新输入

输入

$ 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

【讨论】:

  • 输出确实是用户要求的;但这并没有给他 bash 变量中字符串的每一部分......
【解决方案3】:

使用 awk 的 OneLiner(用于 IGNORECASE 的 gnu)

awk -F ',' 'BEGIN{IGNORECASE=1}{sub("-"$NF"$","",$1);print "str1="$1}' YourFile

自注释代码

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

【讨论】:

    【解决方案4】:

    根据您的 cmets,您需要第一个“虚线”字段的颜色,而不是第二个字段的值(逗号分隔)。

    如果第一个“破折号”字段中的颜色始终是最后一个字符串(破折号分隔),您可以简单地使用
    a="You-Are-My-Sunshine-Happy-Birthday-Red" ; awk -F- '{print $NF}' &lt;&lt;&lt;"$a"

    PS:你可以用 cut 或 awk 隔离整行的第一个字段: awk -f, '{print $1}' &lt;&lt;&lt;"$fileline"cut -d, -f1 &lt;&lt;&lt;"$fileline"

    你可以结合以上两个来实现你所需要的。

    【讨论】:

      【解决方案5】:

      保持简单:

      $< 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 用于扩展正则表达式。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 2019-01-13
        • 2013-06-04
        • 2011-06-12
        相关资源
        最近更新 更多