pattern replace inside text file using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pattern replace inside text file using sed
# 1  
Old 09-17-2009
Error pattern replace inside text file using sed

Hi,

I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file.

For Example:

If a pattern found like
Code:
<a href="http://www.mysite.com/mypage.jsp">

it should be retained.

But if a pattern found like
Code:
<a href="../mypage.jsp">

it should be changed to
Code:
<a href="../mypage.html">

So the requirement is, if the URL is not starting with "http://" or "https://", change the file extension to ".html"

Any Ideas?

Thanks,
Meharo
# 2  
Old 09-17-2009
try:

Code:
awk '{if($0 !~ /http/ && $0 !~ /https/){gsub(/jsp/,"html",$0);print}else print}' file

# 3  
Old 09-17-2009
Code:
sed 's/\(.*="\..*\).\(jsp\)/\1.html/' file

awk '! /http[:s]/{sub(/jsp"/,"html\"")}1' file

# 4  
Old 09-17-2009
thank you guys,

the pattern is not as simple as a single line or always the link starts with "../". It can be like
Code:
<a href="anypath/anypage.jsp">

or multiple links present in the same line like
Code:
<a href="anypath/anypage.jsp"><...><a href="http://www.mysite.com/anypath/anypage.jsp">

meharo
# 5  
Old 09-17-2009
Quote:
Originally Posted by meharo
thank you guys,

the pattern is not as simple as a single line or always the link starts with "../". It can be like
Code:
<a href="anypath/anypage.jsp">

This code should solve this problem.
Code:
sed 's/\(.*=".*\).\(jsp\)/\1.html/' file

awk '! /http[:s]/{sub(/jsp"/,"html\"")}1' file


Quote:
Originally Posted by meharo
or multiple links present in the same line like
Code:
<a href="anypath/anypage.jsp"><...><a href="http://www.mysite.com/anypath/anypage.jsp">

I didn't see this requirement on your original post Smilie , maybe I should give you the chance to solve the problems by yourself.

Success.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to replace inside file

How can I change the comma sign (,) to plus sign (+) with the sed command or any regex? I mean to change only the comma that beteen the quotation marks. From this file: A,B,C A,"B,C",D "A,B",C,D A,B,"C,D" To this file: A,B,C A,"B+C",D "A+B",C,D A,B,"C+D" (6 Replies)
Discussion started by: elior
6 Replies

2. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

3. Shell Programming and Scripting

sed help, Find a pattern, replace it with same text minus leading 0

HI Folks, I'm looking for a solution for this issue. I want to find the Pattern 0/ and replace it with /. I'm just removing the leading zero. I can find the Pattern but it always puts literal value as a replacement. What am I missing?? sed -e s/0\//\//g File1 > File2 edit by... (3 Replies)
Discussion started by: SirHenry1
3 Replies

4. Shell Programming and Scripting

Sed command to replace with pattern except for text and closing parentheses

Can someone help me with a sed command: There will be multiple occurences in a file that look like this: MyFunction(12c34r5) and I need to replace that with just the 12c34r5 for every occurrence. The text between the parentheses will be different on each occurrence, so I can't search for that.... (4 Replies)
Discussion started by: missb
4 Replies

5. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

6. Shell Programming and Scripting

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

7. AIX

Pattern to replace ^M and ^Y in a 4.2 AIX text file

I have files on my AIX 4.2 client system where I need to do the following replacements below but have no clue how ? They are control characters (linefeed, chariage return, ...). First, replace "^M^Y^M" with ^char_for_end_of_line Then replace "^M" with " " Trim all left spaces In VI, my... (7 Replies)
Discussion started by: Browser_ice
7 Replies

8. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

Sed , Replace a "variable text" inside of a statement

Please Help... I am trying to manipulte the following line Before : <user:Account_Password>002786</user:Account_Password> the password is the "variable", i need to delete / omit the password in the file, (it occurs several thousand times) so the tag line looks like After:... (4 Replies)
Discussion started by: jackn7
4 Replies

10. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies
Login or Register to Ask a Question