Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script voor 2e en 3e lijn van tekstbestanden?

3 views
Skip to first unread message

Mattias Campe

unread,
Apr 28, 2012, 4:52:41 AM4/28/12
to
Hallo bcol

In submappen zitten her en der tekstbestanden, met altijd foo als
bestandsnaam. Als ik van deze bestanden steeds de laatste lijn zou nodig
hebben, om deze te bewaren in een tekstbestand 'bar', dan zou ik volgend
commando kunnen uitvoeren:

find . -type f -name "foo" -exec tail -n1 -q {} \; >bar

Het probleem is dat ik niet de laatste lijn nodig heb, maar de tweede en
derde lijn. Ik dacht dat dit makkelijk zou zijn door het 'pipen', maar
het volgende lukt niet:

find . -type f -name "foo" -exec `head -n3 -q | tail -n2 -q` {} \; >bar

Iemand enig idee hoe ik dit zou kunnen oplossen?

Mvg
Mattias Campe
Message has been deleted

Lieven Marchand

unread,
Apr 28, 2012, 10:10:58 AM4/28/12
to
sed is daar meer voor geschikt.

sed -n -e'2,3p'

print de 2e tem de 3e lijn van zijn input

Mattias Campe

unread,
Apr 28, 2012, 2:51:13 PM4/28/12
to
Op 28-04-12 16:10, Lieven Marchand schreef:
Perfect! Dat is het! Bedankt!

Mattias

Mattias Campe

unread,
Apr 28, 2012, 2:52:08 PM4/28/12
to
Op 28-04-12 11:59, houghi schreef:
> Mattias Campe wrote:
>> find . -type f -name "foo" -exec `head -n3 -q | tail -n2 -q` {} \;>bar
>>
>> Iemand enig idee hoe ik dit zou kunnen oplossen?
>
> Loopje? (Untested)
> #!/bin/bash
> test -f bar&& rm bar #Remove file bar if it exists
> for FILE in `find . -type f -name "foo"`
> do
> head -n 3 $FILE |tail -n 2>> bar
> # Dubbele>> zodat er aangevuuld wordt en
> # niet overschreven
> done
>

Houghi, daar de oplossing van Lieven er zo handig uit zag, heb ik die
uitgetest en goed bevonden. Toch heel erg bedankt voor de moeite!

Mvg
Mattias

marc

unread,
Apr 28, 2012, 3:33:59 PM4/28/12
to
Mattias Campe wrote:

> de oplossing van Lieven er zo handig uit zag


head en tail ?

Zal wel enkele lijnen kunnen selecteren?


-n, --lines=[-]K
print the first K lines instead of the first 10; with the
leading `-', print all but the last K
lines of each file




--
--
What's on Shortwave guide: choose an hour, go!
http://shortwave.tk
700+ Radio Stations on SW http://swstations.tk
300+ languages on SW http://radiolanguages.tk

0 new messages