关于shell:如果条件不满足则删除分隔符并在条件上替换字符串

remove delimiter if condition not satisfied and substitute a string on condition

考虑下面的文件。

1
2
3
4
5
HEAD~XXXX
XXX~XXX~XXX~XXX~XXX~XXX~~WIN~SCRIPT~~~
XXX~XXX~XXX~XXX~XXX~XXX~~WIN~TPSCRI~~~
XXX~XXX~XXX~XXX~XXX~XXX~~WIN~RSCPIT~~~
TAIL~20

希望上面的输出如下:

1
2
3
4
5
HEAD~XXXX
XXX~XXX~XXX~XXX~XXX~XXX~~WIN~SCRIPT~~~
XXX~XXX~XXX~XXX~XXX~XXX~~~~~~
XXX~XXX~XXX~XXX~XXX~XXX~~~~~~
TAIL~20

如果第9个字段是脚本,我希望第8和第9个字段都为空,就像第10个字段一样,如果行中包含单词head/tail,则必须从上述条件中忽略这些单词,即nf!=13-将需要页眉和页脚,因为它在输入中。我试过下面的方法,但应该有一个更聪明的方法。

1
awk -F'~' -v OFS='~' '($9 !="Working line takeover with change of CP" {$9 =""}) && ($9 !="Working line takeover with change of CP" {$8 =""}) {NF=13; print}' file

以上都不行

1
2
3
4
5
6
7
8
9
10
11
    head -1 file > head
    tail -1 file > tail
    sed -i '/HDR/d' file
    sed -i '/TLR/d' file
    sed -i '/^\s*$/d' file
    awk -F'~' -v OFS='~' '$9 !="Working line takeover with change of CP" {$9,$8 =""} {NF=13; print}' file  >>  file.tmp    //syntax error

    cat file.tmp >> head
    cat tail >> head
    echo"">> head
    mv head file1

我正在尝试一个具有以下要求的Unix shell脚本。

考虑这样一个文件……

1
2
3
XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~SCRIPT~~~
XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~OTHERS~~~~
XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~OTHERS~~~
  • 每个文件都应该有12个字段(~作为分隔符),如果不是必须删除~
  • 如果第10个字段中存在脚本字符串以外的任何内容,则必须删除该字段。
  • 我试过下面的in/bin/bash,我知道我做得不太好。我正在向SED&awk命令输入行。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    while read readline
        echo"entered while"
        do
            fieldcount=`echo $readline | awk -F '~'  '{print NF}'`
            echo"Field count printed"
            if [ $fieldcount -eq 13 ] && [ $fieldcount -ne 12 ]
            then
            echo"entering IF & before deletion"
                #remove delimiter at the end of line
                #echo"$readline~">> $S_DIR/$1.tmp
                #sed -i '/^\s*$/d' $readline
                sed -i s'/.$//' $readline
                echo"after deletion"
                if [ awk '/SCRIPT/' $readline -ne"SCRIPT"]
          then
                #sed -i 's/SCRIPT//' $readline
                replace_what="OTHERS"
                #awk -F '~' -v OFS=~ '{$'$replace_what'=''; print }'
                sed -i 's/[^,]*//' $replace_what
                echo"$readline">> $S_DIR/$1.tmp
                fi
            else
                echo"$readline">> $S_DIR/$1.tmp
            fi      
            done < $S_DIR/$1

    1
    awk -F'~' -v OFS='~' '$10 !="SCRIPT" {$10 =""} {NF=12; print}' file
    1
    2
    3
    XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~SCRIPT~~
    XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~~~
    XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~~~

    在bash中,我会写:

    1
    2
    3
    4
    5
    6
    7
    8
    (
      # execute in a subshell, so the IFS setting is localized
      IFS='~'
      while read -ra fields; do
        [[ ${fields[9]} !="SCRIPT" ]] && fields[9]=''
        echo"${fields[*]:0:12}"
      done < file
    )

    您的后续问题:

    1
    2
    3
    4
    5
    awk -F'~' -v OFS='~' '
        $1 =="HEAD" || $1 =="TAIL" {print; next}
        $9 !="SCRIPT" {$8 = $9 =""}
        {NF=13; print}
    ' file

    如果您还有其他问题,请创建一个新问题,而不是编辑此问题。