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 are many files with outdated links. So, I want to search on:
and make some change. If it were all on 1 line then I could just do a sed substitution and be done with it.
The problem is that in the (poorly coded) HTML files the pattern might be found like this:
Where the line breaks are completely random.
I need a way to search on a specific pattern of text that may occur over 1 or more lines and then make my substitution.
I'm not married to any one tool- I've given up on sed and searched for potential python or perl solutions. Nothing I've found is generic enough to handle all the different ways these files are written.
Please provide a bigger sample of input file and expected output
This can for example be used to put it in one line and insert some # after every </tr> mark then translate the # into a \n
instead of # you should use a character that does not already exist in the file you want to parse. (µ#% ...)
Thank you.
I was able to make a few changes and turn it into a script.
One of the problems I was dealing with was broken links pointing to files in a non-existent directory named refrnc. So, I wanted to find all hyperlinks to files in that directory and just remove the tag but leave the text.
So, HTML that looked like:
Or being that it could occur over multiple lines, it could have been:
OR
And I wanted to change it to:
The following script makes these changes across multiple lines and case-insensitively.
There are still a few things I don't understand.
1) What is $&?
2) What is the undef $/ doing?
3) Is it really necessary to set $bak=$_ ? Couldn't I have used $_ in the while loop and skipped the step of setting $bak ?
Is there anything in there that just looks like really bad PERL? Or, anything that could be done better?
From perlvar, undefining the input record separator causes <> to read an entire file at a time. And from perlvar, $& is the string matched by the regex <a\s+href.*?/refrnc/.*?>.*?</a>.
I also wonder about all the working being done -- can you provide references that you based this on? But since you just want to remove the offending "<a>" and "</a>":
This User Gave Thanks to m.d.ludwig For This Post:
I just took what k_manimuthu posted and hacked around a bit.
Really, my goal is to learn enough that I can apply this to different cases as customer requests come up. I've been able to handle most everything with BASH/SED/AWK.... I don't know PERL, but perhaps it's time to take my O'Reilly PERL programming book off the bookshelf and finally learn this.
BTW, is there a way to do this in Python? If I'm going to commit to learning a new scripting language I think I'd prefer to learn Python.
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)
I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file.
myVar=`cat myfile`
sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file
myfile:
cat
dog
boy
girl
mouse
house
test.file:
football
hockey
Replace_Flag
baseball
... (4 Replies)
hi I am trying to use SED to replace the line matching a pattern using the command
sed 'pattern c\
new line
' <file1 >file 2
I got two questions
1. how do I insert a blank space at the beginning of new line?
2. how do I use this command to execute multiple command using the -e... (5 Replies)
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... (7 Replies)
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)
Dear All,
i want to search particular string and want to replance next line value.
following is the test file.
search string is
tmp,???
,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50
tmp,123 --- if match tmp,??? then... (3 Replies)
Hello
I am looking to have a script that performs some tasks for find and replace and inserts a line as well. I have done some programming 10 years ago, so it is causing me a little grief.
File consists of 2500 records. I will show you a sample consisting of two records below and what needs... (3 Replies)
Hi All,
I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text.
An example of 4 lines in my file is:
1. MatchText_randomNumberOfText moreData ReplaceMe moreData
2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
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)