print from - to line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print from - to line of a file
# 1  
Old 02-20-2012
print from - to line of a file

Need to print file contents from line number A to line-number B

eg:
Code:
 
# cat -n a
     1  aaaaaaaaaaaaa
     2  bbbbbbbbbbbbb
     3  ccccccccccccc
     4  ddddddddddddd
     5  eeeeeeeeeeeee
     6  ffffffffffffff
     7  gggggggggggg

## Need to print contents from linenum 3-6 only it may be big file of 20MB 
 
## I know to tail from line number 
# tail -n+3  a   --> But ther's no option for to line number ..

--Shirish
# 2  
Old 02-20-2012
Code:
awk '(NR>=3 && NR<=6){print NR "  " $0}' f1

---------- Post updated at 01:23 PM ---------- Previous update was at 01:22 PM ----------

here f1 is the name of file
# 3  
Old 02-20-2012
The awk in post #2 will read the whole file.

This combination of "head" and "tail" would be much quicker on a large file.
Code:
cat -n a | head -6 | tail -4

This User Gave Thanks to methyl For This Post:
# 4  
Old 02-20-2012
Thanks Methyl !!

That's the nice tips... Smilie ... last 1/2 hour i was trying to achieve this by tail ..only ..

--Shirish
# 5  
Old 02-20-2012
Code:
sed -n 3,6p infile

Large files:
Code:
sed -n '3,6p;6q' infile

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-20-2012
@Scrutinizer that is an excellent post.

I knew about:
Quote:
sed -n 3,6p infile
But it goes on to read the whole file.

But this piece of brilliant code does not feature in any of the many "sed one-liners" lists which I have read and it does not read the whole file. Therefore it is quick.
Quote:
sed -n '3,6p;6q' infile
This User Gave Thanks to methyl For This Post:
# 7  
Old 02-20-2012
GNU attempted to automagically optimize this very case, but apparently never managed to shake out all the bugs.

From http://git.savannah.gnu.org/cgit/sed.git/tree/ChangeLog:
Quote:
Tue Apr 14 17:34:54 PDT 1998 Ken Pizzini <ken@gnu.org>

* execute.c (execute_program, process_files, count_branches,
shrink_program): Added a first attempt at program optimization.
We now can quit early if we are running with the "-n"
and all of the commands are known to be valid only for
lines less than the current line. Thus the "sed" in
"foo | sed -n 1,2p" will print read three lines, printint
the first two, and then quit, regardless of how much longer
"foo" might run or output. This optimization does not buy
much in most cases (it sometimes even costs a little),
but when it does help it can help big. The code is
all conditionally compiled based on the
EXPERIMENTAL_DASH_N_OPTIMIZATION symbol being #defined,
so it can be easily omitted if it causes problems.

... snip ....

Sat May 23 16:07:09 HST 1998 Ken Pizzini <ken@gnu.org>

* sed/execute.c: #undef'd EXPERIMENTAL_DASH_N_OPTIMIZATION
because its code is buggy.
Many years later, in http://git.savannah.gnu.org/cgit/sed.../execute.c#n19
Code:
#undef EXPERIMENTAL_DASH_N_OPTIMIZATION	/*don't use -- is very buggy*/


Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. Shell Programming and Scripting

Grep in file and print in the line

hi # cat test.txt Test Date: 20131008 1515 -------------------------------------------------------------------------------------------------------------- Saxx = Proc_m0_s13 : 1640 Saxx = Proc_m0_s15 : 1791 Saxx = Proc_m0_s17 ... (2 Replies)
Discussion started by: justbow
2 Replies

4. Shell Programming and Scripting

Match a line in File 1 with Column in File 2 and print whole line in file 2 when matched

Hi Experts, I am very new to scripting and have a prb since few days and it is urgent to solve so much appreciated if u help me. i have 2 files file1.txt 9647810043118 9647810043126 9647810043155 9647810043161 9647810043166 9647810043185 9647810043200 9647810043203 9647810043250... (22 Replies)
Discussion started by: mustafa.abdulsa
22 Replies

5. Shell Programming and Scripting

read file line by line print column wise

I have a .csv file which is seperated with (;) inputfile --------- ZZZZ;AAAA;BBB;CCCC;DDD;EEE; YYYY;BBBB;CCC;DDDD;EEE;FFF; ... ... reading file line by line till end of file. while reading each line output format should be . i need to print only specific columns let say 5th... (2 Replies)
Discussion started by: rocking77
2 Replies

6. Shell Programming and Scripting

print a line when NOT in a file

I have a list of words, one word per line, LIST1.TXT that I want to check for in another file, PARAGRAPHS.TXT. If the line from LIST1.TXT is NOT in PARAGRAPHS.TXT I want to print it to screen. The keyword here is NOT. If the word from LIST1.TXT is NOT in the file PARAGRAPHS.TXT, then I want to... (7 Replies)
Discussion started by: ajp7701
7 Replies

7. Shell Programming and Scripting

Print first and last line of a file

I need to print ist and last line of a file My file having abc def dfg jkl bnm outout will be abc bnm ---------- Post updated at 11:24 PM ---------- Previous update was at 11:16 PM ---------- pls help me out. Can any one do this. (5 Replies)
Discussion started by: pritish.sas
5 Replies

8. Shell Programming and Scripting

print line whatever line i want in a file... there any way

Hi friends I need to get what ever line number i want Supplier.profile ----------------- SFTP/INVTRAN SFTP/INVSNAP GATEWAY/INVTRAN GATEWAY/INVSNAP ---------------- exec 0<$Supplier.profile while read LINE do echo $LINE (1 Reply)
Discussion started by: kittusri9
1 Replies

9. Shell Programming and Scripting

Print file line by line

I have a file where each line is a stream of text as follows, table1, select * from table1 table2, select * from table2 How do i loop through the file line by line? I have tried doing the following for line in `cat file.txt` do echo $line done and ... cat file.txt|while read line... (2 Replies)
Discussion started by: handak9
2 Replies

10. UNIX for Dummies Questions & Answers

Print one line of Existing File

Hi all, I need to know what command/s I can use to print lines from a file. I cannot do pattern matching, so grepping for a string is not possible. I'd like to say "print line 3" etc. Any ideas? Ta. (2 Replies)
Discussion started by: danhodges99
2 Replies
Login or Register to Ask a Question