extracting lines from a file with similar first name

consider i have two files

cat onlyviews1.sql
CREATE VIEW V11
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V22
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V33
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
cat onlyviews2.sql
CREATE VIEW V119
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V11
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V22
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
firstword="CREATE VIEW V11"

i am performing mainly 3 actions as given below..

1) sed -n "/$firstword/,/;/p" onlyviews1.sql | awk '!x[$1]++' >viewextract1.sql
or
sed -n "/$firstword/,/;/p" onlyviews2.sql | awk '!x[$1]++' > viewextract2.sql
and
 2) viewpresent=`cat onlyviews2.sql | grep -i "$firstword" | wc | awk -F' ' '{print $1}'`
and
3) echo "`sed "/$firstword/,/;/d" onlyviews2.sql`" > onlyviews2.sql

the command (1) extracts individual view from onlyviews file to viewextract file respectively...
the command (2) checks whether a particular view is present in onlyviews2.sql or not.. that is viewpresent will be >1 if present or 0 if not
the command (3) deleted that particular view from onlyviews2.sql

now my problem is say variable $firstword has the contents as shown previously that is CREATE VIEW V11 so if i use commands it will consider V119 too in onlyviews2.sql... since "CREATE VIEW V11" is also present in ""CREATE VIEW V11"9".. so how to overcome this situation...?.... :wall::wall::wall:

/$firstword\n/

?

what does that \n do?..

First tell if it is working in your case :smiley:

i tried just now its not working... viewextract1.sql
viewextract2.sql are empty after i used \n after the variable... i couldnt proceed to check (2) and (3) since command (1) failed....

What does it output:

grep "CREATE VIEW V11" onlyviews1.sql | cat -e

when i tried the above command on onlyviews1.sql i got

 grep "CREATE VIEW V11" onlyviews1.sql | cat -e
        CREATE VIEW V11$

we need to try on onlyviews2.sql for which i got the output

 grep "CREATE VIEW V11" onlyviews2.sql | cat -e
        CREATE VIEW V11$
CREATE VIEW V119$

Try:

/$firstword\$/
1 Like

it worked :-).. thanks a lot.... but what does \$ do...?

It is indicating end of line in the regex match.

1 Like

oh okay got it.. thanks :slight_smile: