It is currently Mon, 27 Jun 2022 11:25:47 GMT



 
Author Message
 Append string to each line if it matches a pattern
Hello,

I've got a messy DB dump that I'm trying to clean up.
All lines in the dump should end with ');   (that's: quotation
parenthesis semi-column)
But some lines don't end with that, and I need to fix that and append
'); to them.

I need to fix only lines that contain a certain string (e.g. foo).

I think I basically need a combination of these two "sed recipes", but
don't know how to combine them:

 # substitute "foo" with "bar" ONLY for lines which contain "baz"
 sed '/baz/s/foo/bar/g'

 # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
 sed '/baz/!s/foo/bar/g'

Any help would be appreciated.  Sed, awk...
Thanks!



 Wed, 29 Oct 2008 04:11:44 GMT   
 Append string to each line if it matches a pattern

   sed "/foo/s/.*/&');/"

Janis



 Wed, 29 Oct 2008 06:01:23 GMT   
 Append string to each line if it matches a pattern

This command will add '); to the end of each line containing 'foo'
regardless of whether it needs to be fixed.  An additional check
must be performed in order to determine if the line contains ');
already.

An example:

    awk "
        /foo/ && ! /\'\)\;/ {
            printf(\"%s');\n\", \$0)
        }
        { print }
    " dbfile.txt

--
Kenan Kalajdzic



 Wed, 29 Oct 2008 06:46:31 GMT   
 Append string to each line if it matches a pattern

The OP wrote (see above) that the presence of "foo" is the condition
for lines not having the terminating pattern, so no additional check
would be necessary according to the question as posted.

If he meant that he would want to fix only those subset of lines that
contain a certain string _and_ are not already terminated by '); then
you're right.

That program is incorrect.

Also to get rid of all that error-prone quoting and escaping better
put the awk program

     /foo/ && !/');/ { printf ("%s');\n", $0) ; next } 1

in a file, say, fixit.awk and call it

     awk -f fixit.awk dbfile.txt >fixedfile.txt

Janis



 Wed, 29 Oct 2008 07:28:33 GMT   
 Append string to each line if it matches a pattern
Thanks Janis and Kenan for such a quick answer.  This looks like it
works!


 Wed, 29 Oct 2008 08:07:40 GMT   
 Append string to each line if it matches a pattern

sed -e '/baz/{s/foo/bar/g;}'
sed -e '/baz/!{s/foo/bar/g;}'



 Wed, 29 Oct 2008 12:11:31 GMT   
 Append string to each line if it matches a pattern

AFAICS, the only thing that you may refer to as 'incorrect' is the
undefined interpretation of the three characters in the second
ERE, namely /\'\)\;/.  This will cause some versions of awk to fail
matching the string ');.  Your proposal, however, will not work
with all awks either (the 'original' awk available from
http://cm.bell-labs.com/cm/cs/who/bwk/awk.tar.gz will refuse to
execute it).  Turning the three characters into three bracket
expressions /['][)][;]/ would make the program(s) more portable.

--
Kenan Kalajdzic



 Fri, 31 Oct 2008 03:05:55 GMT   
 Append string to each line if it matches a pattern

I was mainly referring to the  { print }  statement that produces
a result that does not match the requirement. You need at least a
  next  statement to prevent double printing.

- Show quoted text -



 Fri, 31 Oct 2008 07:22:16 GMT   
 Append string to each line if it matches a pattern

The OP wrote that he needed to put "');" on the end of some lines that
do not already end with that, and only those that contain "foo".  His
proposal to combine an inclusive RE and an exclusive RE condition make
it pretty clear that that he meant to include strings that contain "foo"
and exclude those that end with "');" from his substitution.

[... pretty much like the correction below except without the "next" and
all on the command line with escaped characters ...]

That program is also incorrect, although it probably works for many
cases.  The pattern "/');/" should be anchored at the end of the line to
meet the stated needs of the OP.  What if there is a "');" in the middle
of the line somewhere?  I also had to escape the parenthesis in the RE
in the version of awk I had handy (a BSD version that does not respond
to "awk --version"; I'm sure gawk does not have this problem).  This
worked for me as the content of fixit.awk:

/foo/ && ! /'\);$/ { printf ("%s');\n", $0) ; next } 1

Of course, I see that what was provided already got the poster what he
needed.  This is just for completeness in case other posters bother to
search before posting a similar question in the future.



 Fri, 31 Oct 2008 12:20:47 GMT   
 
   [ 9 post ] 

Similar Threads

1. Matching Line After Pattern (Pattern Occurs Multiple Times)

2. Pattern matching and extracting the data which matches the pattern

3. Using sed to append text if a pattern is matched

4. string pattern matching

5. ksh conditional (string match pattern)

6. vi replace second matching pattern in every line - Help Please sorry for double posting

7. matching pattern over multiple lines

8. removing lines that match a pattern from a file

9. print all lines once a pattern matches

10. matching lines of pattern in a file


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software