Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash script
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German)
View previous topic :: View next topic  
Author Message
commanderhammiltion
n00b
n00b


Joined: 03 Mar 2007
Posts: 5

PostPosted: Wed Feb 06, 2008 2:59 pm    Post subject: bash script Reply with quote

hallo leute, ich hoffe ich bin nicht ganz off topic.

folgendes problem:

In einem Verzeichniss liegen etwa, 30.000 Datein. Alle diese Datein sind textdatein, in einigen dieser Textdatein steht eine bestimmte Zeichenkette.
Ich möchte nun ein Script schreiben was alle Datein in diesem Verzeichniss durchsucht und wenn eine entsprechende datei gefunden wurde
diese in ein anderes verzeichniss kopiert.


leider stehe ich gerade total auf dem schlauch und wäre um eine schnelle hilfe sehr dankbar
_________________
--i still learing gentoo
Back to top
View user's profile Send private message
firefly
Watchman
Watchman


Joined: 31 Oct 2002
Posts: 5329

PostPosted: Wed Feb 06, 2008 3:05 pm    Post subject: Reply with quote

ich mach geb erstmal ein paar hinweise :)
- grep bzw egrep
- schleife (z.b. for)
_________________
Ein Ring, sie zu knechten, sie alle zu finden,
Ins Dunkel zu treiben und ewig zu binden
Im Lande Mordor, wo die Schatten drohn.
Back to top
View user's profile Send private message
schachti
Advocate
Advocate


Joined: 28 Jul 2003
Posts: 3765
Location: Gifhorn, Germany

PostPosted: Wed Feb 06, 2008 3:07 pm    Post subject: Reply with quote

Code:
grep -r -l SUCHBEGRIFF QUELLVERZEICHNIS | xargs -i cp {} ZIELVERZEICHNIS


sollte das tun.
_________________
Never argue with an idiot. He brings you down to his level, then beats you with experience.

How-To: Daten verschlüsselt auf DVD speichern.
Back to top
View user's profile Send private message
Knieper
l33t
l33t


Joined: 10 Nov 2005
Posts: 846

PostPosted: Wed Feb 06, 2008 3:11 pm    Post subject: Reply with quote

Oder:
Code:

#!/bin/sh

for f in `grep "bla" * | cut -f1 -d:`
do
        cp $f pfad
done

wenn die Parameterzahl etwas zu hoch ist.
_________________
Je dümmer desto Gnome/KDE.
Back to top
View user's profile Send private message
firefly
Watchman
Watchman


Joined: 31 Oct 2002
Posts: 5329

PostPosted: Wed Feb 06, 2008 3:13 pm    Post subject: Reply with quote

Knieper wrote:
Oder:
Code:

#!/bin/sh

for f in `grep "bla" * | cut -f1 -d:`
do
        cp $f pfad
done

wenn die Parameterzahl etwas zu hoch ist.

moep das geht auch einfacher nur mit grep ;)

for i in $(grep -l test ./*); do echo $i; done
_________________
Ein Ring, sie zu knechten, sie alle zu finden,
Ins Dunkel zu treiben und ewig zu binden
Im Lande Mordor, wo die Schatten drohn.
Back to top
View user's profile Send private message
schachti
Advocate
Advocate


Joined: 28 Jul 2003
Posts: 3765
Location: Gifhorn, Germany

PostPosted: Wed Feb 06, 2008 3:14 pm    Post subject: Reply with quote

Das geht aber schief, wenn Dateinamen mit Leerzeichen vorkommen, oder?
_________________
Never argue with an idiot. He brings you down to his level, then beats you with experience.

How-To: Daten verschlüsselt auf DVD speichern.
Back to top
View user's profile Send private message
firefly
Watchman
Watchman


Joined: 31 Oct 2002
Posts: 5329

PostPosted: Wed Feb 06, 2008 3:31 pm    Post subject: Reply with quote

schachti wrote:
Das geht aber schief, wenn Dateinamen mit Leerzeichen vorkommen, oder?

hmm stimmt.

dann machen wir es halt so:

Code:
OLDIFS=$IFS     # sicherheitshalber den alten inhalt von IFS speichern
IFS=$"\n"                                #<------ IFS auf \n (neue zeile umstellen), da grep eine liste liefert deren einzelne Elemente mit dem neue zeilen zeichen getrennt sind
for i in $(grep -l hallo ./*)
do
    echo $i
done
IFS=$OLDIFS           # alten IFS wert wieder zurückschreiben


dann klappt es auch mit Dateien die Leerzeichen im namen haben.
IFS (Internal Field Separator) wird z.b. von der for-schleife verwendet um einen stream, hier liste von dateien, in tokens zu zerlegen
_________________
Ein Ring, sie zu knechten, sie alle zu finden,
Ins Dunkel zu treiben und ewig zu binden
Im Lande Mordor, wo die Schatten drohn.
Back to top
View user's profile Send private message
commanderhammiltion
n00b
n00b


Joined: 03 Mar 2007
Posts: 5

PostPosted: Wed Feb 06, 2008 3:43 pm    Post subject: Reply with quote

hallo, wenn ich folgendes eingebe:

grep -r -l MSH|^~ . | xargs -i cp {} ./hl7


der suchstring ist MSH|^~


wenn ich das eingebe kommt

-bash: ^~: command not found
_________________
--i still learing gentoo
Back to top
View user's profile Send private message
schachti
Advocate
Advocate


Joined: 28 Jul 2003
Posts: 3765
Location: Gifhorn, Germany

PostPosted: Wed Feb 06, 2008 3:48 pm    Post subject: Reply with quote

Klappt es, wenn Du den Suchstring in Anführungszeichen einschließt? Du mußt dabei noch beachten, dass ^ im Rahmen von regulären Ausdrücken eine besondere Bedeutung hat.
_________________
Never argue with an idiot. He brings you down to his level, then beats you with experience.

How-To: Daten verschlüsselt auf DVD speichern.
Back to top
View user's profile Send private message
Knieper
l33t
l33t


Joined: 10 Nov 2005
Posts: 846

PostPosted: Wed Feb 06, 2008 4:16 pm    Post subject: Reply with quote

firefly wrote:
moep das geht auch einfacher nur mit grep ;)

Stimmt, aber bei -l haette ich nachsehen muessen, bei cut nicht. ;)

schachti wrote:
Das geht aber schief, wenn Dateinamen mit Leerzeichen vorkommen, oder?

Ja, aber wer macht das schon. Man kann ja auch (das langsamere) find nehmen und hat die Probleme dann nicht mehr.

Es kann sich aber noch ein Problem ergeben:
commanderhammiltion wrote:
grep -r -l MSH|^~ . | xargs -i cp {} ./hl7

Wenn er rekursiv grep()t und in ein Unterverzeichnis kopiert, geht grep irgendwann dort hinein und durchsucht die schon kopierten Dateien. Waere ua. auch mit find zu loesen (oder einer genaueren Angabe als '.').
_________________
Je dümmer desto Gnome/KDE.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum