Search and replace text in a file based on a specific line 
Author Message
 Search and replace text in a file based on a specific line

I want to create a sh script to do the following:
1) find in the file the specified search string
2) replace from the specified text (allowing for the "?" wirdcard) and
replacing it with the sullied text.  Note that only the "?" will be assumed
a wildcard and the assumtion is also made that no "?" as a regular
character, ie the "?" is always a wildcard in this parameter.

The command would have the following sytax:
replinfile filename search replacefrom replaceto

For example if I started with the following file called "testfile":
-------------------------------------
This is test #21****line 5
Another test #ab****line 6
Second last test #xyz****line 7
Last test #&&****line 8
-------------------------------------
After running the following commands:
replinfile testfile "line 6" "test #??" "test #www"
replinfile testfile "line 8" "test #???" test #xx"
replinfile testfile "line 5" "test #??" "test #yy"
replinfile testfile "line 7" "test #???" "test #zz"
The "testfile" file should look like this:
-------------------------------------
This is test #yy****line 5
Another test #www****line 6
Second last test #zz****line 7
Last test #xx***line 8
-------------------------------------

I tried looking at sed to do this but was not able to find a way to take
advantage of wildcard caracters.  What would be the most effecient and less
complicated (if there is such an animal which complies to both)?  Would it
be awk, sed, ed, perl, etc?



Mon, 10 Nov 2003 23:08:12 GMT  
 Search and replace text in a file based on a specific line

Quote:

> I want to create a sh script to do the following:
> 1) find in the file the specified search string
> 2) replace from the specified text (allowing for the "?" wirdcard) and
> replacing it with the sullied text.  Note that only the "?" will be assumed
> a wildcard and the assumtion is also made that no "?" as a regular
> character, ie the "?" is always a wildcard in this parameter.

> The command would have the following sytax:
> replinfile filename search replacefrom replaceto

> For example if I started with the following file called "testfile":
> -------------------------------------
> This is test #21****line 5
> Another test #ab****line 6
> Second last test #xyz****line 7
> Last test #&&****line 8
> -------------------------------------
> After running the following commands:
> replinfile testfile "line 6" "test #??" "test #www"
> replinfile testfile "line 8" "test #???" test #xx"
> replinfile testfile "line 5" "test #??" "test #yy"
> replinfile testfile "line 7" "test #???" "test #zz"
> The "testfile" file should look like this:
> -------------------------------------
> This is test #yy****line 5
> Another test #www****line 6
> Second last test #zz****line 7
> Last test #xx***line 8
> -------------------------------------

> I tried looking at sed to do this but was not able to find a way to take
> advantage of wildcard caracters.  What would be the most effecient and less
> complicated (if there is such an animal which complies to both)?  Would it
> be awk, sed, ed, perl, etc?

Hello,

there is no awk/perl needed, sed can do that.

grep "line 6" testfile | sed 's/test\ \#../test\ \#www/g'
Another test #www****line 6

I'm sure you can figure out, how to put some variables in a script
around this. There is no animal, it's called regular expressions.

Good luck

Michael Heiming



Tue, 11 Nov 2003 01:10:11 GMT  
 Search and replace text in a file based on a specific line
I will try that out.

PS: The "animal" I was refering to was the "less complicated".


Quote:

> > I want to create a sh script to do the following:
> > 1) find in the file the specified search string
> > 2) replace from the specified text (allowing for the "?" wirdcard) and
> > replacing it with the sullied text.  Note that only the "?" will be
assumed
> > a wildcard and the assumtion is also made that no "?" as a regular
> > character, ie the "?" is always a wildcard in this parameter.

> > The command would have the following sytax:
> > replinfile filename search replacefrom replaceto

> > For example if I started with the following file called "testfile":
> > -------------------------------------
> > This is test #21****line 5
> > Another test #ab****line 6
> > Second last test #xyz****line 7
> > Last test #&&****line 8
> > -------------------------------------
> > After running the following commands:
> > replinfile testfile "line 6" "test #??" "test #www"
> > replinfile testfile "line 8" "test #???" test #xx"
> > replinfile testfile "line 5" "test #??" "test #yy"
> > replinfile testfile "line 7" "test #???" "test #zz"
> > The "testfile" file should look like this:
> > -------------------------------------
> > This is test #yy****line 5
> > Another test #www****line 6
> > Second last test #zz****line 7
> > Last test #xx***line 8
> > -------------------------------------

> > I tried looking at sed to do this but was not able to find a way to take
> > advantage of wildcard caracters.  What would be the most effecient and
less
> > complicated (if there is such an animal which complies to both)?  Would
it
> > be awk, sed, ed, perl, etc?

> Hello,

> there is no awk/perl needed, sed can do that.

> grep "line 6" testfile | sed 's/test\ \#../test\ \#www/g'
> Another test #www****line 6

> I'm sure you can figure out, how to put some variables in a script
> around this. There is no animal, it's called regular expressions.

> Good luck

> Michael Heiming



Tue, 11 Nov 2003 01:29:52 GMT  
 Search and replace text in a file based on a specific line
Upon looking at it carefully, I noticed that the file does not get modified.
The original file must containt the original lines but with the changes we
specified.  This is where I had difficulty to start with.


Quote:

> > I want to create a sh script to do the following:
> > 1) find in the file the specified search string
> > 2) replace from the specified text (allowing for the "?" wirdcard) and
> > replacing it with the sullied text.  Note that only the "?" will be
assumed
> > a wildcard and the assumtion is also made that no "?" as a regular
> > character, ie the "?" is always a wildcard in this parameter.

> > The command would have the following sytax:
> > replinfile filename search replacefrom replaceto

> > For example if I started with the following file called "testfile":
> > -------------------------------------
> > This is test #21****line 5
> > Another test #ab****line 6
> > Second last test #xyz****line 7
> > Last test #&&****line 8
> > -------------------------------------
> > After running the following commands:
> > replinfile testfile "line 6" "test #??" "test #www"
> > replinfile testfile "line 8" "test #???" test #xx"
> > replinfile testfile "line 5" "test #??" "test #yy"
> > replinfile testfile "line 7" "test #???" "test #zz"
> > The "testfile" file should look like this:
> > -------------------------------------
> > This is test #yy****line 5
> > Another test #www****line 6
> > Second last test #zz****line 7
> > Last test #xx***line 8
> > -------------------------------------

> > I tried looking at sed to do this but was not able to find a way to take
> > advantage of wildcard caracters.  What would be the most effecient and
less
> > complicated (if there is such an animal which complies to both)?  Would
it
> > be awk, sed, ed, perl, etc?

> Hello,

> there is no awk/perl needed, sed can do that.

> grep "line 6" testfile | sed 's/test\ \#../test\ \#www/g'
> Another test #www****line 6

> I'm sure you can figure out, how to put some variables in a script
> around this. There is no animal, it's called regular expressions.

> Good luck

> Michael Heiming



Tue, 11 Nov 2003 01:44:28 GMT  
 Search and replace text in a file based on a specific line

Quote:

> Upon looking at it carefully, I noticed that the file does not get modified.
> The original file must containt the original lines but with the changes we
> specified.  This is where I had difficulty to start with.

You didn't not mentioned this in your original post. Now I would use
awk:

awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
\#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

This is all on one line.

Michael Heiming

[SNIP]



Tue, 11 Nov 2003 02:31:56 GMT  
 Search and replace text in a file based on a specific line

  MH> You didn't not mentioned this in your original post. Now I would use
  MH> awk:

  MH> awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
  MH> \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

gack!! that is more unreadable than some of the worst perl i have ever
seen.

let perl do most of the work for you.

perl -pe '/line 6/ && s/test #../test #www/'

or to make it handle command line args:

perl -pe '/$ARGV[0]/ && s/$ARGV[1]/$ARGV[2]/' 'line 6' 'test #..' 'test #www'

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info:     http://www.sysarch.com/perl/OOP_class.html



Tue, 11 Nov 2003 03:00:19 GMT  
 Search and replace text in a file based on a specific line

Quote:


>   MH> You didn't not mentioned this in your original post. Now I would use
>   MH> awk:

>   MH> awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
>   MH> \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

> gack!! that is more unreadable than some of the worst perl i have ever
> seen.

Sure it looks strange, but it does a bit more then the line perl you
suggested.

Regards

Michael Heiming

Quote:
> let perl do most of the work for you.

> perl -pe '/line 6/ && s/test #../test #www/'

> or to make it handle command line args:

> perl -pe '/$ARGV[0]/ && s/$ARGV[1]/$ARGV[2]/' 'line 6' 'test #..' 'test #www'

> uri

> --




Tue, 11 Nov 2003 03:40:17 GMT  
 Search and replace text in a file based on a specific line


  >>

  >>
  MH> You didn't not mentioned this in your original post. Now I would use
  MH> awk:
  >>
  MH> awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
  MH> \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2
  >>
  >> gack!! that is more unreadable than some of the worst perl i have ever
  >> seen.

  MH> Sure it looks strange, but it does a bit more then the line perl you
  MH> suggested.

i was following the OP request. you seem to keep the original line
around as well. the problem was not very well specified. if you give me
tighter specs, i could easily change the code to handle it.

i used to be an awk guru, but that was many generations ago. awk has
almost no need to live anymore.

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info:     http://www.sysarch.com/perl/OOP_class.html



Tue, 11 Nov 2003 04:43:34 GMT  
 Search and replace text in a file based on a specific line
This is what I meant by "replace"... which would actually do a search and
replace in the file.

Quote:

> > Upon looking at it carefully, I noticed that the file does not get
modified.
> > The original file must containt the original lines but with the changes
we
> > specified.  This is where I had difficulty to start with.

> You didn't not mentioned this in your original post. Now I would use
> awk:

> awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
> \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

> This is all on one line.

> Michael Heiming

> [SNIP]



Tue, 11 Nov 2003 04:42:31 GMT  
 Search and replace text in a file based on a specific line
I am sorry if I didn't express myself very well, but here goes again...

I want to create a sh script which will search a text file (parameter 1) for
a specific string (parameter 2).  This is used to uniquely identify exactly
which line I need modify.  The next step is to replace FROM text that is
supplied (parameter 3) (it can contain one or more "?" (or none) as a
wildcard) TO text that is also supplied (parameter 4).  So the format would
be:
replinfile textfile searchtext replacefrom replaceto

where textfile = text file to search and replace
          searchtext = the initial text to search for (used to uniquely
identify the line because the replace is done ONLY on one line)
          replacefrom = the text which identifies what text must be replaced
(the "?" can be used as a wildcard)
          replaceto = what the text must be replace with.

So if I had a text file called testfile which had a line "Oh no.  Here we go
now." (among other lines) and used my sh script with the following:
replinfile testfile "Oh no." "now." "again!"
I would have the line replaced in the file testfile to "Oh no.  Here we go
again!"

I hope I have provided enough information and made myself clear. ;)

PS I have tried the awk solution and, as mentionned it repeats th line in
the file.  I have also tried the perl solution and I get the errors "Can't
open line 6: No such file or directory", "Can't open test #..: No such file
or directory", and "Can't open test #www: No such file or directory".
Shouldn't you need to specify the name of the file somewhere?  I don't even
remotely know perl (only know of it) but I forsee the need to delve in it in
the future.


Quote:



>   MH> You didn't not mentioned this in your original post. Now I would use
>   MH> awk:

>   MH> awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
>   MH> \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

>   >> gack!! that is more unreadable than some of the worst perl i have
ever
>   >> seen.

>   MH> Sure it looks strange, but it does a bit more then the line perl you
>   MH> suggested.

> i was following the OP request. you seem to keep the original line
> around as well. the problem was not very well specified. if you give me
> tighter specs, i could easily change the code to handle it.

> i used to be an awk guru, but that was many generations ago. awk has
> almost no need to live anymore.

> uri

> --


http://www.sysarch.com
Quote:
> SYStems ARCHitecture and Stem Development ------

http://www.stemsystems.com
Quote:
> Learn Advanced Object Oriented Perl from Damian Conway - Boston, July
10-11
> Class and Registration info:

http://www.sysarch.com/perl/OOP_class.html


Tue, 11 Nov 2003 05:35:54 GMT  
 Search and replace text in a file based on a specific line
[SNIP]
Quote:

> PS I have tried the awk solution and, as mentionned it repeats th line in
> the file.


"The original file must containt the original lines but with the changes
we
specified."                                            ^^^^^^^^^^^^^^^^^^^^        

That's exactly what the line awk does, I posted...

Michael Heiming

[SNIP]

Quote:


> >   MH> You didn't not mentioned this in your original post. Now I would use
> >   MH> awk:

> >   MH> awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
> >   MH> \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

[SNIP]


Tue, 11 Nov 2003 06:00:37 GMT  
 Search and replace text in a file based on a specific line


Quote:

> [SNIP]

> > PS I have tried the awk solution and, as mentionned it repeats th line
in
> > the file.

> "The original file must containt the original lines but with the changes
> we
> specified."     ^^^^^^^^^^^^^^^^^^^^

> That's exactly what the line awk does, I posted...

I will explain with an example.  Here is the ORIGINAL file (called
testfile):
-------------------------------------
This is test #21****line 5
Another test #ab****line 6
Second last test #xyz****line 7
Last test #&&****line 8
-------------------------------------

I run the sh script: replinfile testfile "line 8" "???? test" "Final test"
would result the testfile file be modified to:
-------------------------------------
This is test #21****line 5
Another test #ab****line 6
Second last test #xyz****line 7
Final test #&&****line 8
-------------------------------------

[SNIP]



Tue, 11 Nov 2003 06:18:00 GMT  
 Search and replace text in a file based on a specific line

Quote:

> I will explain with an example.  Here is the ORIGINAL file (called
> testfile):
> -------------------------------------
> This is test #21****line 5
> Another test #ab****line 6
> Second last test #xyz****line 7
> Last test #&&****line 8
> -------------------------------------

> I run the sh script: replinfile testfile "line 8" "???? test" "Final
> test" would result the testfile file be modified to:
> -------------------------------------
> This is test #21****line 5 Another test #ab****line 6 Second last
> test #xyz****line 7 Final test #&&****line 8
> -------------------------------------

Here's a pretty lazy way:

#!/usr/bin/perl -pi.bak
BEGIN {

  $pattern =~ tr/?/./;

Quote:
}

/$line/ and s/$pattern/$replace/g;
__END__

--
Ren Maddox



Tue, 11 Nov 2003 06:40:12 GMT  
 Search and replace text in a file based on a specific line

  DR> I am sorry if I didn't express myself very well, but here goes
  DR> again...  I want to create a sh script which will search a text
  DR> file (parameter 1) for a specific string (parameter 2).  This is
  DR> used to uniquely identify exactly which line I need modify.  The
  DR> next step is to replace FROM text that is supplied (parameter 3)
  DR> (it can contain one or more "?" (or none) as a wildcard) TO text
  DR> that is also supplied (parameter 4).  So the format would be:
  DR> replinfile textfile searchtext replacefrom replaceto

if you want shell, then why post in c.l.perl.misc?

<c.l.perl.misc removed from followups>

  DR> I hope I have provided enough information and made myself clear. ;)

  DR> PS I have tried the awk solution and, as mentionned it repeats th line in
  DR> the file.  I have also tried the perl solution and I get the errors "Can't
  DR> open line 6: No such file or directory", "Can't open test #..: No such file
  DR> or directory", and "Can't open test #www: No such file or directory".
  DR> Shouldn't you need to specify the name of the file somewhere?  I don't even
  DR> remotely know perl (only know of it) but I forsee the need to delve in it in
  DR> the future.


work. but since you don't even care to understand what code you are
getting or how to fix it, i won't help any more.

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info:     http://www.sysarch.com/perl/OOP_class.html



Tue, 11 Nov 2003 07:31:28 GMT  
 Search and replace text in a file based on a specific line

*please, answer to the bottom of messages you answer and only cite
what's needed. also, when you xpost, don't forget to put a fu2.*

Quote:



> > > Upon looking at it carefully, I noticed that the file does not get
> modified.
> > > The original file must containt the original lines but with the changes
> we
> > > specified.  This is where I had difficulty to start with.

> > You didn't not mentioned this in your original post. Now I would use
> > awk:

> > awk '{if ($0 ~ /line\ 6/) ((o=$0) &&  sub ("test\ \#..","test\
> > \#www",$0) && ($0=$0" "o))} {print $0 }' testfile > testfile2

> > This is all on one line.

humm! I count two ugly lines :)

awk -v p="$1" -v s="$2" -v r="$3" '
BEGIN { s=sub("?",".") }
$0 ~ p { sub(s,r); print }
' testfile1 > testfile2

sed "/$1/s/`echo $2|sed 's/\?/./g'`/$3/" testfile1 > testfile2

ed - file << EOF
g/$1/s/`echo $2|sed 's/\?/./g'`/$3/
w
q
EOF

Quote:
> This is what I meant by "replace"... which would actually do a
> search and replace in the file.

this wasn't clearly expressed, so, you've got what you asked for.

add "mv testfile2 testfile1" to the end of the above sed/awk scripts :P

Cyrille.
--


Supprimer "%no-spam" et ".invalid" pour me repondre. | about who it chooses
Remove "%no-spam" and ".invalid" to answer me back.  | to be friends with.



Tue, 11 Nov 2003 12:19:12 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Search and replace on a large text file

2. Replacing a line in a Text File

3. Read specific line from text file

4. Replacing text at specific locations

5. parsing a long text file for specific text

6. Looking for best text search/replace method

7. Getting specific text from a line...

8. Search file and print line + next line?

9. Replacing text within a line

10. Replacing blank lines with text.

11. How to replace multiple-line text

12. 2-file awk search-and-replace

 

 
Powered by phpBB® Forum Software