awk or other command to replace JobNumber


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or other command to replace JobNumber
# 1  
Old 01-20-2017
awk or other command to replace JobNumber

Hi,

I need help with replacing WorkJobNumber to empty string

Input data in file:

HTML Code:
<property class="java.lang.String" name="WorkJobNumber" value="000000106039"/>
Like this to be

HTML Code:
<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.
Code:
nawk -F"=" '{ if ($2~RobotJobNumber) print $0; else print "A"}' OFS="="

Appreciate help.
# 2  
Old 01-20-2017
See if this works:
Code:
awk '
  {
    for(i=1; i<=NF; i++) {
      if($i~/^name="WorkJobNumber"/)
        w=1
      if($i~/^value=/ && w) {
        sub(/".*"/,"\"\"",$i)
        w=0
      }
    }
  }
  1
'  file

# 3  
Old 01-20-2017
Thank you sir. This works.

Could you please explain the code for my understanding so, I can write similar code in future.
# 4  
Old 01-20-2017
Quote:
Originally Posted by pinnacle
.
.
.
Please note WorkJobNumber string can come in other records, I like to replace only records where the entire row is like the example above.
.
.
.
If you really need that, try
Code:
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.
# 5  
Old 01-20-2017
Hello pinnacle,

Could you please try following too and let me know if this helps you.
Code:
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.
Code:
<property class="java.lang.String" name="WorkJobNumber" value="/>"

Thanks,
R. Singh
# 6  
Old 01-21-2017
Lemme give a shot with explanin Scrutinizer code :

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 Smilie

Hope that helps
Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 7  
Old 01-21-2017
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.
Code:
awk '
  {
    for(i=1; i<=NF; i++) {
      if(w && $i~/^value=/) {
        sub(/".*"/,"\"\"",$i)
      }
      w=0
      if($i~/^name="WorkJobNumber"/)
        w=1
    }
  }
  1
' file


Last edited by MadeInGermany; 01-23-2017 at 04:08 AM.. Reason: last line added
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with awk or sed Command to Replace Text in Files

Hello Everyone, I have many files like so: file1.txt file2.txt file3.txt Within each file I have many lines of random text separated by commas like so: abcAAA,123,defAA,456777,ghiA,789 jklB,101,mnoBBB,11211,pqrB,13111 stuCC,415,vwxCCCC,161,yzaC,718 I am trying to use SED or AWK to... (4 Replies)
Discussion started by: D3U5X
4 Replies

2. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

3. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

4. Shell Programming and Scripting

Need command to replace empty using sed/awk

Hi, In a file we have the following data like as below abcdef="cfg-1-15" bmmdda-g-45-2 yhdiao"rtg-1-df-34" I need a sed/awk command to replace the above string with empty. Thx, (1 Reply)
Discussion started by: kirankumar
1 Replies

5. Shell Programming and Scripting

awk command to replace columns in 2 files

Hi All, I already have a code which replaces column 14 of NPBR.XTR.tmp with column 8 of NPBR3G.XTR.final awk -F'\|' 'FNR==NR{a= $2"^"$8;next;}a{split(a,b,"^");$8=b;$14=b;}1' OFS="|" ${SHTEMP}NPBR3G.XTR.final ${SHTEMP}NPBR.XTR.tmp > ${SHTEMP}NPBR.XTR.final I also need to replace column 15... (2 Replies)
Discussion started by: nua7
2 Replies

6. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

7. Shell Programming and Scripting

How to Replace the value of a column using awk command?

Hi cat test.txt H|123|341|567|asfg D|dfg|trtyy|errt D|ert|frty|wer Here I need to replace the third column value with 100 of the first record only and while printing I need to print the full file content also..I am expecting a result like this H|123|100|567|asfg D|dfg|trtyy|errt... (3 Replies)
Discussion started by: saj
3 Replies

8. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

9. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

10. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies
Login or Register to Ask a Question