Need to print certain lines from a file

Hi ALL,

I want to print lines from file using certain conditions
for exmple:
# The following commands will create a new control file and use it
# to open the database.
# The contents of online logs will be lost and all backups will
# be invalidated. Use this only if online logs are damaged.

;
ALTER DATABASE OPEN;
SIZE 2000M REUSE AUTOEXTEND OFF;
SIZE 2000M REUSE AUTOEXTEND OFF;
;
ALTER DATABASE OPEN RESETLOGS;
SIZE 2000M REUSE AUTOEXTEND OFF;
SIZE 2000M REUSE AUTOEXTEND OFF;

here i want to print lines between first occurance of ";" and second occurance of it.

Thanks
Jack

awk '/^;$/ { while (getline && $0 !~ /^;$/) { print } ; exit }' inputfile > outputfile

Thanks a lot..

Hi,

I am new to scripting..
awk '/^;$/ { while (getline && $0 !~ /^;$/) { print } ; exit }' 6 > 7

could you please explain me what it does and how does it check.

Thanks
Jack

HII,

I want to print lines from file based on certain line
exapmple

RECOVER DATABASE
ALTER DATABASE OPEN;
ALTER TABLESPACE TEMP ADD TEMPFILE ;
ALTER TABLESPACE TEMP ADD TEMPFILE ;
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "PATCH01" RESETLOGS NOARCHIVELOG
-- SET STANDBY TO MAXIMIZE PERFORMANCE
MAXLOGFILES 16
MAXLOGMEMBERS 5
MAXDATAFILES 512
MAXINSTANCES 1
MAXLOGHISTORY 7260
LOGFILE;

here i want to print from "ALTER DATABASE OPEN" till before startup nomount

Please help on this..

Thanks and Regards
Jack

Here it is again with comments

awk '
    # for every line containing just a ";"
    /^;$/ {
        # read subsequent lines until we reach another one containing
        # just a ";"
        while (getline && $0 !~ /^;$/) {
            # print them
            print
        }
        # exit from the script (i.e. do not process any more lines)
        exit
    }
' inputfile > outputfile

To satisfy your new requirement you only need to modify the script slightly to search for different strings instead of ";".

Hi,

Instead of strings specifying...i want to print from certain line to line in file..
exapmple

RECOVER DATABASE
ALTER DATABASE OPEN;
ALTER TABLESPACE TEMP ADD TEMPFILE ;
ALTER TABLESPACE TEMP ADD TEMPFILE ;
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "PATCH01" RESETLOGS NOARCHIVELOG
-- SET STANDBY TO MAXIMIZE PERFORMANCE
MAXLOGFILES 16
MAXLOGMEMBERS 5
MAXDATAFILES 512
MAXINSTANCES 1
MAXLOGHISTORY 7260
LOGFILE;

here i want to print from "ALTER DATABASE OPEN" till "MAXINSTANCES 1" line

Please help on this ..
Thanks
Jack

You can just use:

awk '/ALTER DATABASE OPEN/,/MAXINSTANCES 1/' inputfile > outputfile

Thanks alot..It solved my problem...
:slight_smile:

HI ,

I have one more problem ..

i want to replace few different entries in file
example
CREATE CONTROLFILE REUSE DATABASE "SID" RESETLOGS NOARCHIVELOG
-- SET STANDBY TO MAXIMIZE PERFORMANCE
MAXLOGFILES 16
MAXLOGMEMBERS 5
MAXDATAFILES 512
MAXINSTANCES 1
MAXLOGHISTORY 7260
LOGFILE
GROUP 1 (
'/mount/sid/patch01data/log01a.dbf',
'/mount/sid/patch01data/log01b.dbf'

here i want to change with SID=NEWSID,sid=newsid,REUSE=set and mount=newmount

Please help me how to replace it.

Thanks 
Jack

Use sed to do search-and-replaces on the input data.

HI
Thanks for quick replay
sed "'s/$esid/$sid/g','s/$emount/$mount/g','s/REUSE/SET/g','s/$ESID/$SID/g'" temp>temp1
sed: 's/patch01/1/g','s/dev04/2/g','s/REUSE/SET/g','s/PATCH01/3/g' is not a recognized function.
I m getting the above error

Please help me suggest me..

Thanks
jack

You're almost there... no need for the single quotes, and you can separate commands using ";", e.g.

sed "s/$esid/$sid/g;s/$emount/$mount/g;s/REUSE/SET/g;s/$ESID/$SID/g" temp>temp1

Thanks alot Annihilannic...
one more thing..

How to pass dynamic values to
sed "s/$esid/$sid/g;s/$emount/$mount/g;s/REUSE/SET/g;s/$ESID/$SID/g
to this $esid,sid ..
echo "enter esid"
read esid
echo "enter emount"
read emount
echo "enter ESID"
read ESID
echo "enter sid"
read sid
echo "enter mount"
read mount
echo "enter SID"
read SID
sed 's/$esid/$sid/g;s/$emount/$mount/g;s/REUSE/SET/g;s/$ESID/$SID/g' temp>temp1

Here the values are not taking ..Please help

Thanks
jack

It's because you changed the double quotes " to single quotes '.

Single quotes prevent variables from being evaluated.

So many thanks Anni..I have learnt somany things from u...
It worked for me..

Thanks
Jack

HI,

I need to add two file

example
++++++++++
cat dum
1
22
333
4444

another file
cat dummy
hhh
xnnnn
xmmmm

i need to have both in one file.

1
22
333
4444
hhh
xnnnn
xmmmm

Please help on this..
Thanks
Jack

You can use cat here to appaent dummy file to dum:

cat dummy  >> dum

Or, of course, cat dum dummy > newfile. This is what cat was designed for, concatenating files.

perl -ne 'if(m/^;/){$f++;next;} if($f==1){print;}' filename