How to print data between 2 lines in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to print data between 2 lines in a file
# 1  
Old 04-28-2006
How to print data between 2 lines in a file

i want to print the data between two line numbers in file to another file.
which command should i use

i tried sed command . i am storing the line numbers in two variables say L1,L2. but $L1 and $L2 are not working in sed command.

is there any other command to do this
reply soon
# 2  
Old 04-28-2006
What sed command did you try ?
# 3  
Old 04-29-2006
Bug hey thanks for ur reply yaar

hey i got the solution to the above problem.

i used the sed command sed -n " $L1,$L2 w outfile" inputfile where L1 and L2 are variables which contains line numbers.

previously i used the same command but with ' qoutes instead of " quotes.


Any have thanks for showing interest to solve my problem
# 4  
Old 10-20-2008
i am using sed -n "$lineno1 $lineno2 w OUT.FILE" $infile , but i got the error

sed: -e expression #1, char 3: Unknown command: `1'

please help
# 5  
Old 11-04-2008
Try with awk:

cat -n test.log | awk '{if((NR>122)&&(NR<128)) print}'

for a test to see if it is working (this should print the lines from number 123 to 127. Then you can remove 'cat' and the command should be:

awk '{if((NR>122)&&(NR<128)) print}' test.log

Hope this will work for you (works for me Smilie)

If you want to use line numbers dynamically as variables, then use nawk instead awk:

nawk -v l1=$L1 -v l2=$L2 '{if((NR>l1)&&(NR<l2)) print}' test.log

Last edited by igorc; 11-04-2008 at 08:20 PM..
# 6  
Old 12-18-2008
Very helpful thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to combine data from 2 file ? How to print ?

Dear friends, I am just trying to print data from 2 file,namely file_1.txt and file_1.dat (specific column of ROW 1) file_1.txt 12 13 14 15 99 AMC 69 36 89 12 13 14 15 99 AMC 69 84 -12 12 13 14 ... (6 Replies)
Discussion started by: nex_asp
6 Replies

2. Shell Programming and Scripting

Print specific lines of a repeated set of data

I have a file that needs 1st line, 2nd line, and 26th line printed from every chunk of data. Each chunk of data contains 26 lines (#line+%line+24 data lines = 26 lines of data repeated). Input file: # This is a data file used for blockA (chunk 1). % 10576 A 10 0 1 04 (data1) 03 (data2)... (2 Replies)
Discussion started by: morrbie
2 Replies

3. Shell Programming and Scripting

Compare a file with all others then print off data

my script so far nawk -F, 'NR==FNR{a++;next} a{b++} END{for(i in b){if(b-1){print i";\t\t"b}else{print "NEW:"i";\t\t1"} } }' OFS=, 20111228.csv *.csv | sort NE:221478,SHELF:13,SLOT:4; 2 NE:221726,SHELF:8,SLOT:1; 2 NE:222318,SHELF:14,SLOT:1; 9... (20 Replies)
Discussion started by: llcooljatt
20 Replies

4. 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

5. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

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... (25 Replies)
Discussion started by: jack00423
25 Replies

10. Shell Programming and Scripting

print out result from data file

i got a data file which contains all the pid,ppid,user,command,pcpu,start_time,status. I wanted to display out the pcpu which is greater than 0. i uses awk'{if($5 > 0){print}}' filename.txt but is printing out result which not i wanted. Is there any way which i can print out those pcpu which is... (8 Replies)
Discussion started by: thms_sum
8 Replies
Login or Register to Ask a Question