Need to print certain lines from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to print certain lines from a file
# 1  
Old 07-16-2008
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
# 2  
Old 07-16-2008
Code:
awk '/^;$/ { while (getline && $0 !~ /^;$/) { print } ; exit }' inputfile > outputfile

# 3  
Old 07-17-2008
thanks for quick reply...

Thanks a lot..
# 4  
Old 07-17-2008
Regarding that script

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
# 5  
Old 07-17-2008
One more query on printing lines

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
# 6  
Old 07-17-2008
Here it is again with comments

Code:
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 ";".
# 7  
Old 07-17-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print lines in a file

Dear All, a.txt A 1 Z A 1 ZZ B 2 Y B 2 AA how can i use awk one line to achieve the result: A Z|ZZ B Y|AA Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

2. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

3. Shell Programming and Scripting

Print matching lines in a file

Hello everyone, I have a little script below: die "Usage infile outfile reGex" if @ARGV != 3; ($regex) = @ARGV; open(F,$ARGV) or die "Can't open"; open(FOUT,"+>$ARGV") or die "Can't open"; while (<F>) { print FOUT if /$regex/.../$regex/; } No matter what I give $regex on the... (2 Replies)
Discussion started by: new bie
2 Replies

4. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

5. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

6. Windows & DOS: Issues & Discussions

Print lines 20-30 from a file

Hi I want to print lines 20-30 from a file. In UNIX , this command will work sed -n '20,30p' file However what is the equivalent command in DOS ? Pls help me ! (2 Replies)
Discussion started by: dashing201
2 Replies

7. Shell Programming and Scripting

How to print file without few exactly matching lines?

Hi I have a very long file with 4 columns of numbers for example 1875 1876 12725 12723 13785 13786 4232 4230 13184 13185 ... (2 Replies)
Discussion started by: ananyob
2 Replies

8. Shell Programming and Scripting

how to print the certain lines in a file to different files

Hi All, File that I have: <ct> <name>group <value>1 <value>2 <value>3 </ct>-->file The output that I needed is <ct> <name>group <value>1 -->file1 <ct> <name>group <value>2 -->file2 (6 Replies)
Discussion started by: natalie23
6 Replies

9. UNIX for Dummies Questions & Answers

How to get/print the lines from a specified file ? (LINUX)

It my first post here . I just want to get the content of the file as values for printinting along with line number in LINUX Here is what I tried . $ cat test1.txt ABC DSF GHI JKL MNO PQR STU VWX YZO $ cat test.sh #!/bin/ksh (4 Replies)
Discussion started by: rajavu
4 Replies

10. Shell Programming and Scripting

Print only certain lines from a text file

Hi all, I have a text file and I want to clean up the file by only print those lines start with the date. Is there anyway I can do that?  Thanks CT (1 Reply)
Discussion started by: CamTu
1 Replies
Login or Register to Ask a Question