Search for a multi-line strings in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a multi-line strings in a file
# 1  
Old 07-18-2011
Search for a multi-line strings in a file

Hello
I need to search for a mult-line strngs(with spaces in between and qoted) in a file1 and replace that text with Fixed string globally in file1. The strng to search for is in file2.

The file is big with some 20K records. so speed and effciency is required

file1: (where srch & rplc will happen) e;g lets say 5 lines in file are;

Thebare book_names='Unix1 Best' and then editor_ranks='Honsun' where live.
Science is another Line_no1='Number Whole' and look for ppl and data
Lets have person='Noro' and past Noro and Unix1
This is nothing to do
Next line is Honsun

file2Smiliestring to be srched)
book_names
editor_ranks
Line_no1
person

I need to replce all these strgs value with XXX. Also need to replace all occurances of these values globaly in file.

Expetd file:
Thebare book_names=XXX and then editor_ranks=XXX where live.
Science is another Line_no1=XXX and look for ppl and data
Lets have person=XXX and past XXX and XXX
This is nothing to do
Next line is XXX

Any ideas? Need urgnt help
# 2  
Old 07-18-2011
Try the following shell scrpit

Code:
 
sed -e "s:^:s/\\(:" -e "s:$:=\\)'[^']*'/\\1XXX/g:" file2 > file3
sed -f file3 file1 > Expected_file

# 3  
Old 07-18-2011
Try this,
Code:
#!/usr/bin/perl
undef $/;
open(FH1,"<","file1") or die "canot open file 1\n";
open(FH2,"<","file2")  or die "canot open file 2\n";
$data=<FH1>;
$/="\n";
while(<FH2>) {
        chop;
        $data=~s/$_='(.+?)'\s/$_=XXX /g;
        $data=~s/$1/XXX/g;
        }
print $data;
close(FH1);
close(FH2);

Invocation
Code:
 perl perlscript.pl

# 4  
Old 07-18-2011
Thanks Pravinn for the reply...
But the script is not running properly..

i invoked it with perl script.pl

but not working.i have checked the perl installed on my machine and its okay. pls help

---------- Post updated at 06:41 AM ---------- Previous update was at 06:38 AM ----------

Quote:
Originally Posted by 116@434
Try the following shell scrpit

Code:
 
sed -e "s:^:s/\\(:" -e "s:$:=\\)'[^']*'/\\1XXX/g:" file2 > file3
sed -f file3 file1 > Expected_file


Thanks for quick reply, I tried running this script but giving teh below error:

sed: -e expression #2, char 26: invalid reference \1 on `s' command's RHS

also,
File1 is the input file to be replaced
File2 has the list of strings to be searched.

pls let me know what is File 3 in the sed script u provided.

Thanks
# 5  
Old 07-18-2011
Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!", "Doubt" or anything that does not accurately reflect the nature of your problem. Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums
# 6  
Old 07-19-2011
Write following lines in a file say sed.script:

Code:
 
s:^:s/\\(:
s:$:=\\)'[^']*'/\\1XXX/g:

Then invoke the following in a shell script:

Code:
 
sed -f sed.script  file2 > file3
sed -f file3 file1 > Expected_file

file3 is an intermediate file that's being created in the first sed invoked.
# 7  
Old 07-19-2011
Hi Hiano,

Please post error messages which you are facing while executing script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

5. Shell Programming and Scripting

Replace a multi-line strings or numbers

Hi I have no experience in Unix so any help would be appreciated I have the flowing text 235543 123 45654 199 225 578 45654 199 225 I need to find this sequence from A file 45654 199 225 (22 Replies)
Discussion started by: khaled79
22 Replies

6. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

7. Shell Programming and Scripting

Global search and replace multi line file

Hello I need to search for a mult-line strngs(with spaces in between and qoted) in a file1 and replace that text with Fixed string globally in file1. The strng to search for is in file2. The file is big with some 20K records. so speed and effciency is required file1: (where srch & rplc... (0 Replies)
Discussion started by: Hiano
0 Replies

8. Shell Programming and Scripting

Multi-Line Search and Replace

There appears to be several threads that touch on what I'm trying to do, but nothing quite generic enough. What I need to do is search through many (poorly coded) HTML files and make changes. The catch is that my search string may be on one line or may be on several lines. For example there... (5 Replies)
Discussion started by: bisbell
5 Replies

9. Shell Programming and Scripting

Using Awk to Search Two Strings on One Line

If i wanted to search for two strings that are on lines in the log, how do I do it? The following code searches for just one string that is one one line. awk '/^/ {split($2,s,",");a=$1 FS s} /failure agaf@fafa/ {b=a} END{print b}' urfile What if I wanted to search for "failure agaf@fafa"... (3 Replies)
Discussion started by: SkySmart
3 Replies

10. Shell Programming and Scripting

Search and replace multi-line text in files

Hello I need to search for a mult-line text in a file exfile1 and replace that text with another text. The text to search for is in exfile2 and the replacement text is in exfile3. I work with kornshell under AIX and need to do this with a lot of files. (the file type is postscript and they need... (10 Replies)
Discussion started by: marz
10 Replies
Login or Register to Ask a Question