Updating value based on search pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Updating value based on search pattern
# 1  
Old 06-07-2013
Updating value based on search pattern

I have a file with following data

Code:
<Field FieldName="CHCFA21_01_01" FieldType="Text">
        <Output CapturedValue="">
          <DataSource Name="" Value="" />
        </Output>
      </Field>
      <Field FieldName="CHCFA21_01_02" FieldType="Date">
        <Output CapturedValue="">
          <DataSource Name="" Value="" />
        </Output>
      </Field>
      <Field FieldName="CHCFA21_01_03" FieldType="Text">
        <Output CapturedValue="">
          <DataSource Name="" Value="" />
        </Output>
      </Field>
      <Field FieldName="CHCFA21_01_04" FieldType="Rate">
        <Output CapturedValue="">
          <DataSource Name="" Value="" />
        </Output>
</Field>

I need the output like this

Code:
<Field FieldName="CHCFA21_01_01" FieldType="Text">
        <Output CapturedValue="CHCFA21_01_011">
          <DataSource Name="" Value="" />
        </Output>
      </Field>
      <Field FieldName="CHCFA21_01_02" FieldType="Date">
        <Output CapturedValue="060713">
          <DataSource Name="" Value="" />
        </Output>
      </Field>
      <Field FieldName="CHCFA21_01_03" FieldType="Text">
        <Output CapturedValue="CHCFA21_01_031">
          <DataSource Name="" Value="" />
        </Output>
      </Field>
      <Field FieldName="CHCFA21_01_04" FieldType="Rate">
        <Output CapturedValue="125.20">
          <DataSource Name="" Value="" />
        </Output>
</Field>

Note :
If fieldtype is text use the value in fieldname tag, append with 1 and that value need to passed in Output CapturedValue tag
If fieldtype is date pass current date in MMDDYY format in Output CapturedValue tag
If fieldtype is rate pass any amount in Output CapturedValue tag in following format(for eg 125.20)

Code:
My OS Info : SunOS viper 5.9 Generic_117171-07 sun4u sparc SUNW,Sun-Fire-480R
My shell is csh

# 2  
Old 06-07-2013
Here is one way of doing it:
Code:
nawk '
        /FieldType="Text"/ {
                V = $0
                gsub ( /.*FieldName="|".*/, X, V )
                V = V "1"
        }
        /FieldType="Date"/ {
                "date +%m%d%y" | getline V
        }
        /FieldType="Rate"/ {
                V = "125.20"
        }
        /Output CapturedValue/ {
                sub (/=""/, "=\"" V "\"" )
        }
        1
' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

Search for duplicates and delete but remain the first one based on a specific pattern

Hi all, I have been trying to delete duplicates based on a certain pattern but failed to make it works. There are more than 1 pattern which are duplicated but i just want to remove 1 pattern only and remain the rest. I cannot use awk '!x++' inputfile.txt or sed '/pattern/d' or use uniq and sort... (7 Replies)
Discussion started by: redse171
7 Replies

4. UNIX for Dummies Questions & Answers

Dynamically accept search pattern and display lines based on it

I have a output file which contains n number of document.Each document has n number of segments and identified using below points The starting segment is ISA and Ending segment is IEA Each document has unique number and it will be passed in REF*D9 segment Each line in sample file is called... (3 Replies)
Discussion started by: nsuresh316
3 Replies

5. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

6. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

Search and replace - pattern-based

Hey folks! I am new to shell-scripting, but I have a problem that I would like to solve using a script. I create very large html forms, used for randomized trials. In these forms, each question is supplied with a variable that looks something like this: PROJECT_formNN Where NN is the question... (1 Reply)
Discussion started by: Roevhat
1 Replies

9. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question