LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   number of lines to the new file (https://www.linuxquestions.org/questions/linux-newbie-8/number-of-lines-to-the-new-file-695121/)

coppuca 01-05-2009 10:43 AM

number of lines to the new file
 
Hi,
I have a huge file and I need to put only few lines of it in a new file.
For example:
Code:

awk '/keyword/ {print NR}' myfile
shows line #567, and I need to print lines 567-577 to a new file.

Please help me.

matthewg42 01-05-2009 10:56 AM

If you want to extract the lines which match some keyword (or regular expression pattern), you can just use grep. awk can do all this too, but it's more complicated.

e.g.
Code:

$ ls
my_original_file
$ grep "bananas" original_file > new_file

grep's output is all lines which match the pattern "bananas", and this output is re-directed into a new file called, "new_file". No need to worry about the line numbers at all. You can do it of course, but it seems more complicated than you need.

pixellany 01-05-2009 10:58 AM

sed -n '567,577p' oldfile > newfile

angel115 01-05-2009 10:59 AM

Hi coppuca,

Well you can use grep instead of awk
Code:

grep -A10 'YourPattern' Original_File > Destination_File
The previous code will display the 10 next line matching YourPattern

-A = After the matching line
-B = Before the matching line

I Hope that will help.

Best regards,
Angel.

coppuca 01-05-2009 10:59 AM

no. i want to extract the line that match the keyword + 10 lines below it.

matthewg42 01-05-2009 11:02 AM

What Angel said.

pixellany 01-05-2009 10:59 PM

Sorry my other answer did not fit the question exactly.

There is also:

sed -n '/pattern/,+10p' filename

makyo 01-06-2009 10:17 AM

Hi.

If you have a really long file, you may want to consider some optimization. I don't have any really large files, but here are results on working with a file that is around 1 GB. I assume that for a match, you want only the first hit. I have adjusted the requirement form 10 to 2 to save posting space:
Code:

#!/bin/bash -

# @(#) s1      Demonstrate obtaining a segment, piece, part of a file.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed grep cgrep
set -o nounset
echo

FILE=${1-/tmp/test-one-gb}

echo "  Lines in data file $FILE:"
time wc -l $FILE

echo
echo " Results, sed:"
time sed -n '434,435 p' $FILE

echo
echo " Results, sed with quit:"
time sed -n -e '434,435 p' -e '436 q' $FILE

echo
echo " Results, grep, max-count:"
time grep --max-count=1 -A 1 -n "nightmare" $FILE

echo
echo " Results, cgrep, -N matches:"
echo " http://www.bell-labs.com/project/wwexptools/cgrep/"
time cgrep -N 1 +1 -n -D "nightmare" $FILE

exit 0


Code:

$ ./s1

(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
GNU sed version 4.1.2
grep (GNU grep) 2.5.1
cgrep - (local: ~/executable/cgrep Sep 28 2007 )

  Lines in data file /tmp/test-one-gb:
14754910 /tmp/test-one-gb

real    0m19.423s
user    0m1.049s
sys    0m1.645s

 Results, sed:
nightmare to a dead sartainty.  Landlord, I whispered, that aint the
harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m5.926s
user    0m5.020s
sys    0m0.807s

 Results, sed with quit:
nightmare to a dead sartainty.  Landlord, I whispered, that aint the
harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m0.001s
user    0m0.002s
sys    0m0.000s

 Results, grep, max-count:
434:nightmare to a dead sartainty.  Landlord, I whispered, that aint the
435-harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m0.001s
user    0m0.000s
sys    0m0.001s

 Results, cgrep, -N matches:
 http://www.bell-labs.com/project/wwexptools/cgrep/
434:nightmare to a dead sartainty.  Landlord, I whispered, that aint the
435:harpooneer, is it?  Oh, no, said he, looking a sort of diabolically funny,

real    0m0.002s
user    0m0.001s
sys    0m0.002s

Note that without the "quit", sed will go through the entire file, whereas it's much faster with the "quit". If you want to do matching, then the GNU grep has a feature to stop at "n" hits. If you don't have GNU grep, then one can obtain cgrep, which has similar features (and much more), from the site noted ... cheers, makyo


All times are GMT -5. The time now is 12:31 AM.