Sparrow
Sparrow

Reputation: 148

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

Consider the below file.

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

wish the Output to be like below for the above:

HEAD~XXXX
XXX~XXX~XXX~XXX~XXX~XXX~~WIN~SCRIPT~~~
XXX~XXX~XXX~XXX~XXX~XXX~~~~~~
XXX~XXX~XXX~XXX~XXX~XXX~~~~~~
TAIL~20

If the 9th field is SCRIPT, I want both 8th & 9th fields to be empty like the 10th & if the line contains words HEAD/TAIL those have to ignored from our above condition, i.e., NF!=13 - will need the header & footer as it is in the input. I have tried the below, but there should be a smarter way.

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

the above doesn't work

    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

I'm trying an UNIX shell script with the below requirements.

Consider a file like this..

XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~SCRIPT~~~
XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~OTHERS~~~~
XXX~XXX~XXX~XXX~XXX~XXX~~XXX~~OTHERS~~~
  1. Each file should have 12 fields(~ as delimiter), if not a ~ has to removed.
  2. If anything OTHER than SCRIPT string present in the 10th field, the field has to be removed.

I tried the below in /bin/bash, I know I'm not doing it so well. I'm feeding line to sed & awk commands.

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

Upvotes: 0

Views: 154

Answers (1)

glenn jackman
glenn jackman

Reputation: 247012

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

In bash, I would write:

(
  # 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
)

Your followup question:

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

If you have further questions, please create a new question instead of editing this one.

Upvotes: 1

Related Questions