awk or other command to replace JobNumber

Hi,

I need help with replacing WorkJobNumber to empty string

Input data in file:

<property class="java.lang.String" name="WorkJobNumber" value="000000106039"/>

Like this to be

<property class="java.lang.String" name="WorkJobNumber" value=""/>

Please note WorkJobNumber string can come in other records, I like to replace only records where the entire row is like the example above.

I like to put the Value of WorkJobNumber from numerical to empty string.

I tried this but does not work.

nawk -F"=" '{ if ($2~RobotJobNumber) print $0; else print "A"}' OFS="="

Appreciate help.

See if this works:

awk '
  {
    for(i=1; i<=NF; i++) {
      if($i~/^name="WorkJobNumber"/)
        w=1
      if($i~/^value=/ && w) {
        sub(/".*"/,"\"\"",$i)
        w=0
      }
    }
  }
  1
'  file

Thank you sir. This works.

Could you please explain the code for my understanding so, I can write similar code in future.

If you really need that, try

awk '
match ($0, /<property class="java.lang.String" name="WorkJobNumber" value="[0-9]*"\/>/) {sub (substr ($0, RSTART+63, RLENGTH-66), _)
                                                                                        }
1
'  file

It checks for EXACTLY that line and replaces value ONLY if matched.

Hello pinnacle,

Could you please try following too and let me know if this helps you.

awk '{sub(/<property class=\"java.lang.String\" name=\"WorkJobNumber\" value=\"[0-9]+\"\/>/,"<property class=\"java.lang.String\" name=\"WorkJobNumber\" value=\"/>\"");print}'  Input_file

Output will be as follows.

<property class="java.lang.String" name="WorkJobNumber" value="/>"

Thanks,
R. Singh

Lemme give a shot with explanin Scrutinizer code :

  {
    for(i=1; i<=NF; i++) { # for all fields in the line separated by field separator (in this case default FS space)
      if($i~/^name="WorkJobNumber"/) # if field matches desired string 
        w=1 # create a variable w with value 1
      if($i~/^value=/ && w) { # if field in the line matches '^value=' and w is defined (regular expression '^name="WorkJobNumber"' has been matched in the same line)
        sub(/".*"/,"\"\"",$i) # substitute, in this case truncate, ..value.. with nothing.
        w=0 # reset w for next line of input
      }
    }
  }
  1 # print everything

NF determines the number of fields per line.
Using that information, we can use i in a for loop with $ to evaluate each field string wise with our conditions. (NF is 4, so i is from 1 to 4, $1 being first field etc.)
awk considers record separator(RS) a line (if not stated otherwise).
So we introduce a w variable to process our conditions per line.

Everything is printed (again per line, if matched print changed if not print as is) as file is processed line by line.

If i missed something feel free to correct me :slight_smile:

Hope that helps
Regards
Peasant.

1 Like

Perhaps this can be improved by only checking the word after the "WorkJobNumber".
That means w is reset unconditionally, its query must happen before, and its setting must happen after.

awk '
  {
    for(i=1; i<=NF; i++) {
      if(w && $i~/^value=/) {
        sub(/".*"/,"\"\"",$i)
      }
      w=0
      if($i~/^name="WorkJobNumber"/)
        w=1
    }
  }
  1
' file