Find a tag with data and replace its data in another tag


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a tag with data and replace its data in another tag
# 1  
Old 08-27-2017
Find a tag with data and replace its data in another tag

Hi I have one file,

Code:
:16R::GENL
:20C::RELA//SET//ABC123456
:22F::XYZYESR
:20C::MITI//NETT/QWERTY12345
:16S::GENL

:16R::GENL
:20C::RELA//SET//XYZ23456
:22F::XYZYESR
:16S::GENL

The requirement is, if :20C::MITI// is present in any block, then replace the data of :20C::MITI// in :20C::RELA//

The expected output of the above example is :
Code:
:16R::GENL
:20C::RELA//NETT/QWERTY12345
:22F::XYZYESR
:20C::MITI//NETT/QWERTY12345
:16S::GENL

:16R::GENL
:20C::RELA//SET//XYZ23456
:22F::XYZYESR
:16S::GENL

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules).

Last edited by Don Cragun; 08-27-2017 at 09:41 AM..
# 2  
Old 08-27-2017
Hello Soumyadip Dutta,

Welcome to forums, I hope you will enjoy learning and sharing knowledge here. Coming to your question, if your actual Input_file is same as sample shown then following may help you in same.
Code:
awk '
/:16S::GENL/{
   flag=""
}
/:16R::GENL/{
   flag=1
}
flag && /:20C::RELA/{
   val=$0;
   getline;
   val1=$0;
   getline;
   if($0 ~ /:20C::MITI/){
      sub("SET//ABC123456","NETT/QWERTY12345",val)
};
   print val ORS val1 ORS $0;
   next
};
1
'   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-27-2017
Thanks RavinderSingh13!!

It is working.
But there is one requirement.
The values of :20C::MITI/ and :20C::RELA/ are not fixed and may vary from blocks to blocks.

Could you help me in using a variable to fetch the values and then substitute it.

Code:
sub("SET//ABC123456","NETT/QWERTY12345",val)

The above part needs to be changed, since they are not fixed.

Thanks again!!
Soumyadip
# 4  
Old 08-27-2017
Quote:
Originally Posted by Soumyadip Dutta
Thanks RavinderSingh13!!
It is working.
But there is one requirement.
The values of :20C::MITI/ and :20C::RELA/ are not fixed and may vary from blocks to blocks.
Could you help me in using a variable to fetch the values and then substitute it.
Code:
sub("SET//ABC123456","NETT/QWERTY12345",val)

The above part needs to be changed, since they are not fixed.
Thanks again!!
Soumyadip
Hello Soumyadip Dutta,

You could HIT a THANKS button here if you want to thank to a person for a useful post as post's left corner. Coming to your question now, if those values are NOT fixed then could you please do let me know what is the pattern to catch these lines then?

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 08-27-2017
Ravinder,

the pattern is as follows:
Code:
:20C::RELA//SET//ABC123456
:22F::XYZYESR
:20C::MITI//NETT/QWERTY12345

The red parts are fixed, and we have to identify using them.
The blue parts can change.

So whenever there is a pattern ":20C::MITI//" is occurring after the pattern ":20C::RELA//" within a block, the value of the pattern ":20C::MITI//" which is "NETT/QWERTY12345" has to replaced in the pattern ":20C::RELA//".

Output :
Code:
:20C::RELA//NETT/QWERTY12345
:22F::XYZYESR
:20C::MITI//NETT/QWERTY12345

Note that the start and the end of a block is determined by the below patterns:

:16R::GENL -- start
:16S::GENL -- end

Please let me know if you need more details.
# 6  
Old 08-27-2017
Try:

Code:
awk '
  {
    for(i=1; i<=NF; i++) {
      if($i~/:20C::RELA/) f=i
      if($i~/:20C::MITI/) {
        s=$i
        sub("[^/]*", x, s)
        sub("//.*", s, $f)
      }
    }
  }
  1
' RS= FS='\n' OFS='\n' ORS='\n\n' file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 08-27-2017
Hey Scrutinizer,

Its working.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with... (6 Replies)
Discussion started by: Nexeu
6 Replies

2. Shell Programming and Scripting

Extracting data between two tag pairs

In a huge log file (43MB, 43k lines) I am trying to extract data between two tag pairs on same line and export it to a file so I can pull it into Excel for a report. One Pair is <Text>data I need</Text> Other pair follows on same line and is <TimeStamp>more data I need</TimeStamp> I would need... (2 Replies)
Discussion started by: NanookArctic
2 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

Extracting Delimiter 'TAG' Data From log files

Hi I am trying to extract data from within a log file and output format to a new file for further manipulation can someone provide script to do this? For example I have a file as below and just want to extract all delimited variances of tag 32=* up to the delimiter "|" and output to a new file... (2 Replies)
Discussion started by: Buddyluv
2 Replies

5. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

6. Shell Programming and Scripting

Extract the data from tag below asssigned the variable

Sample data file. UID=12_C_S_12_PrecisionMktg^12_C_S_12_PrecisionMktg_LinkList NameField=LINK_NAME ExternalTrackingField=EXTERNAL_TRACKING CategoryField=LINK_CATEGORY URLField=LINK_URL UID=12_PrecisionMktg^FILTER_12_C_S_12_PrecisionMktg comma=,... (5 Replies)
Discussion started by: bmk
5 Replies

7. Shell Programming and Scripting

Substitute partial data only within a xml tag

Hello, I have huge xml files and I need to replace only part of the data within a particular xml tag. This doesnt seem to be as simple as it sounds. I have searched everywhere and couldnt find any solution. Ex: In the below case I would like "def" to be replaced by "xyz" only when found in... (8 Replies)
Discussion started by: roshanjain2
8 Replies

8. Programming

how to remove duplicate node data by searching tag element

hi everyone, I written one script that search all xml files and create one xml file, but I need to remove some duplicate nodes by testing one tag element. </Datainfo> <data> <test>22</test> <info>sensor value</info> <sensor> <sensor value="23"... (0 Replies)
Discussion started by: veerubiji
0 Replies

9. Shell Programming and Scripting

How To get the data from a tag in XML File

Hi I have a XML file in which data is loaded from a relational table and the column names are tags in the xml file which is shown below. ... (8 Replies)
Discussion started by: naughty21
8 Replies
Login or Register to Ask a Question