LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2016, 02:40 AM   #1
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
Replacing text with content from another file in an order


I am not that good and scripting and got stuck with a requirement while writing a script, I have been trying to replace contents of same text in a file that comes multiple times with the contents of another file which comes in each line. Below is an example
file1
Code:
REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(FAIL).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(FAIL).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(FAIL).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(FAIL).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END
file2
Code:
SUCCESS
DEFAULT
WRONG
CORRECT
I want to replace "FAIL" from the file1 with entries from file 2 so that first "FAIL" gets replaced with first entry from file 2 which is "SUCCESS" and this continues. The end result should be like following,
Code:
REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(SUCCESS).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(DEFAULT).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(WRONG).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(CORRECT).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
END
I tried using a for loop in below format, but it doesn't work. How do increment to replace first occurrence with the first entry from file2 and second occurrence with second entry from file2, and so on?
Could you please help?

for a in `cat file2`; do sed 's/FAIL/$a/1' file1

Last edited by Sayan Acharjee; 06-14-2016 at 11:28 PM.
 
Old 06-14-2016, 04:33 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Hi!

I used a tiny script for this, where 'fail.sh' is the name of the script, and source.txt is your infile:
Code:
$ ./fail.sh source.txt
REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(SUCCESS,).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(DEFAULT,).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(WRONG,).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(CORRECT).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
What I used in the script:
- a while loop to read the file
- an array to hold the replacements (if you need to read these from a file, then do so)
- sed to do the actual replacing
- a counter
- echo to print the information to STDOUT

Being a Red Hat Certified Engineer, you will figure this out easily.

Best regards,
HMW

Last edited by HMW; 06-14-2016 at 04:39 AM.
 
Old 06-14-2016, 11:54 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I have 3 shell scripts, one is pure bash, second uses sed and third uses awk ... all seems fairly straight forward.

Your original solution is quite close ... may I suggest looking at the curly braces and /q options on the following page: http://www.grymoire.com/Unix/Sed.html
 
Old 06-17-2016, 12:09 AM   #4
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
As grail pointed out, better to tell the solution even if it wasn't needed later as it was marked as solved.
Code:
declare -a arr;

#to store the texts to be replaced in the file in the array
arr=($(cat file1 |grep "(*)"|grep -v Comp|awk -F"(" {'print $4'}|awk -F")" {'print $1'}));



count=0;
while read -r i
do
sed -i "s/${arr[$count]}/$i/g" file1
(( count ++ ))
done < file2

Last edited by Sayan Acharjee; 06-17-2016 at 05:40 AM.
 
Old 06-17-2016, 05:23 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Interesting Your initial example gave the impression that we knew the word FAIL and hence only had to replace it, but your solution points to not knowing what might be in the required spot ... which
is better as I initially was wondering how you would ever change FAIL for SUCCESS

Here is a bit of a tidy up for you:
Code:
arr=($(awk -F"[()]+" '/REGEX/{getline;print $4}' file1))

count=0

while read -r new
do
  sed -i "s/${arr[count++]}/$new/g" file1
done<file2
I would point out that I have tidied the code but it will not produce the results you were wanting .. this will update the original file (I presumed that "test" was a typo as there is no file with that name)
so that all items are changed to the first entry from second file and then no other changes get made as all the original names have now been changed.

Here is output after above has been run:
Code:
$ cat file1
REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(SUCCESS).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(SUCCESS).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(SUCCESS).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END

REGEX Pattern1 FOLLOWS BaseEvent
^(?i)(.*(SUCCESS).*)$
ComponentID PRINTF("%s:Pattern1", log_name)
IdentifierKey $1
msg $1
ComponentArg $2
Severity 5
END
So from the example, all FAIL words were changed to SUCCESS, but when loop performs further iterations the word FAIL is nowhere to be found so no future changes are made
 
1 members found this post helpful.
Old 06-17-2016, 05:50 AM   #6
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
There would not be multiple occurrence for the same string, so this works perfectly fine. Yes, that was a typo with the file name
 
Old 06-17-2016, 02:26 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe in the future you would like to present examples that actually reflect what you have and what you need then as it appears nothing about the original question was actually correct.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing text in a file graziano1968 Linux - General 4 07-26-2010 06:23 AM
replacing text in file kushalkoolwal Programming 17 10-25-2005 05:25 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 09:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 03:22 PM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration