View previous topic :: View next topic |
Author |
Message |
qp n00b
Joined: 03 Jul 2007 Posts: 6
|
Posted: Tue Jul 03, 2007 6:58 pm Post subject: sed-Wissen aus der Praxis für Noobs und Geeks |
|
|
*edit* div. Threads zusammengeführt und Titel verallgemeinert --think4urs11
data.txt :
1 kirk
2 worf
3 spock
4 data
5 odo
6 kira
7 enterprise
8 chekov
9 riker
10 sulu
------ EOF----------
SED -n -e '/o/, /a/p' data.txt
Eigentlich sollte es von der 1.Zeile mit 'o' bis 1.Zeile mit 'a' ausgeben.
Also :
2 worf
3 spock
4 data
Aber die Ausgabe ist :
2 worf
3 spock
4 data
5 odo
6 kira
8 chekov
9 riker
10 sulu
Wieso?
Danke im voraus!!! |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Tue Jul 03, 2007 7:02 pm Post subject: |
|
|
deutsch nur in den deutschen Foren, daher 'moved from Portage & Programming to Diskussionsforum' _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
Fauli l33t
Joined: 24 Apr 2004 Posts: 760 Location: Moers, Germany
|
Posted: Tue Jul 03, 2007 7:28 pm Post subject: Re: SED -n -e '/o/,/a/p' data.txt |
|
|
qp wrote: | SED -n -e '/o/, /a/p' data.txt |
Wenn sed das a gefunden hat, fängt es wieder von Neuem an, nach dem o zu suchen. _________________ Do your part to beautify the web! Turn off link underlining! |
|
Back to top |
|
|
qp n00b
Joined: 03 Jul 2007 Posts: 6
|
Posted: Tue Jul 03, 2007 9:05 pm Post subject: |
|
|
Ok, ich hab es schon geahnt... danke für die Antwort...
Und wie kann ich das unterdrücken, nämlich, dass SED genau
das erste Auftreten dieses Bereiches ausgibt? Ich hab probiert mit 'q', aber
klappt nicht so richtig... |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Tue Jul 03, 2007 10:42 pm Post subject: |
|
|
Code: | sed '/o/!d;:loop;N;/a/!b loop;q' data.txt |
_________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
qp n00b
Joined: 03 Jul 2007 Posts: 6
|
Posted: Wed Jul 04, 2007 10:10 pm Post subject: sed -n "/^[^oe]*$/p" d2 |
|
|
sed -n "/^[^oe]*$/p" data.txt
was macht diese Zeile genau??
/^[^oe]*/ bedeutet : es sollen keine e oder o (in beliebiger Menge) am Anfang der Zeile
stehen...
/^[^oe]*$/ heisst es nun, dass alles sich auf die letzte Zeile beziehen muss?
irgendwie verstehe ich es nicht..
Hat jemand eine Idee?
Danke im voraus! |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Wed Jul 04, 2007 10:25 pm Post subject: |
|
|
im Zweifelsfall hilft einfach ausprobieren
Dein Kommando gibt nur noch Zeilen aus in denen weder ein o noch ein e vorkommen. _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
qp n00b
Joined: 03 Jul 2007 Posts: 6
|
Posted: Wed Jul 04, 2007 10:39 pm Post subject: |
|
|
ja klar, das kann man durch ausprobieren finden...
aber ich will die logik verstehen, die dahinter steht...
irgendwie ist es unlogisch - wozu sind erstes ^ und $ am Ende?
das ganze verwirrt sehr stark.. |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Wed Jul 04, 2007 10:59 pm Post subject: |
|
|
dir ist aber schon bewußt das hier das Gentooforum ist und nicht das fuer anonyme sed-geeks (wie mich)?
...mein ja nur da deine Posts im Forum der letzten beiden Tage bisher außer sed eigentlich nur sed und gelegentlich sed zum Thema hatten...
lies dich mal durch ein paar howtos, z.B.
http://sed.sourceforge.net/ oder http://www-128.ibm.com/developerworks/linux/library/l-sed1.html _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
qp n00b
Joined: 03 Jul 2007 Posts: 6
|
Posted: Wed Jul 04, 2007 11:08 pm Post subject: |
|
|
@Think4UrS11
Danke für den Hinweis!
Ich bin extra auf Gentoo-Forum gekommen, weil ich aus meiner Erfahrung eindeutig
sagen kann : Unter Gentoo-People gibt's am meisten Hacker. Also wenn nicht
Gentoo-Forum, dann wer? :O |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Wed Jul 04, 2007 11:10 pm Post subject: |
|
|
qp wrote: | Also wenn nicht Gentoo-Forum, dann wer? :O |
Ok, DAS Argument lasse ich gelten _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
Ampheus l33t
Joined: 19 Oct 2005 Posts: 814 Location: Rheine, NRW, Germany
|
Posted: Thu Jul 05, 2007 11:44 am Post subject: |
|
|
Danke für den Link.
Will mich endlich mal richtig mit sed befassen
Achja, das Lob von qp nehme ich natürlich auch gerne an |
|
Back to top |
|
|
nikaya Veteran
Joined: 13 May 2006 Posts: 1471 Location: Germany
|
Posted: Thu Jul 05, 2007 12:12 pm Post subject: |
|
|
Das Sed by example ist übrigens von drobbins. _________________ Notes on Dhamma
How to waste your time: look for an explanation of consciousness, ask to know what feeling is. (Nanavira Thera) |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Thu Jul 05, 2007 6:42 pm Post subject: |
|
|
Ampheus wrote: | Will mich endlich mal richtig mit sed befassen |
Sehr löblich, sed ist was sehr feines; kommt selten vor das es mit sed nun wirklich nicht mehr geht.
Abschreckende Beispiele gefällig? 1, 2
Ampheus wrote: | Danke für den Link. |
Eine leicht variierte Fassung der IBM-Artikel zu sed gibt es auch auf der Gentoo-HP bei den Dokumentationen; Abschnitt DeveloperWorks-Artikel, neben feinen Sachen zu awk, ssh key-Mgmt. und haste-nich-gesehen...
noch ein anderes howto: http://www.linux-fuer-alle.de/doc_show.php?docid=181&catid=8 _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
Ampheus l33t
Joined: 19 Oct 2005 Posts: 814 Location: Rheine, NRW, Germany
|
Posted: Thu Jul 05, 2007 7:39 pm Post subject: |
|
|
Meine Güte, wie lange arbeitest du schon mit sed?
Das Programm ist ja dermaßen mächtig...
Leider wirkt das auf mich immer sehr kryptisch
Naja da muss ich durch.... |
|
Back to top |
|
|
Fauli l33t
Joined: 24 Apr 2004 Posts: 760 Location: Moers, Germany
|
Posted: Thu Jul 05, 2007 8:58 pm Post subject: |
|
|
Ich weiß nicht, ob diese Skript-Sammlung schon in einer der oben verlinkten Seiten erwähnt wird, aber dort gibt's auch jede Menge Skripts von "basic" bis "extreme", inkl. Sokoban-Spiel. _________________ Do your part to beautify the web! Turn off link underlining! |
|
Back to top |
|
|
sirro Veteran
Joined: 20 Jul 2003 Posts: 1472 Location: aachen.nrw.de.eu
|
Posted: Sat Jul 07, 2007 8:53 am Post subject: |
|
|
Hier ist ja mindestens ein sed-experte anwesend, darum stelle ich auch mal eine Frage:
Code: | #echo 'yyy zzz yyy' | sed -re 's#.*(zzz)?.*#xxx \1 xxx#;'
xxx xxx |
Wie bekomme ich es hin, dass er den optionalen Teil der RE mit ersetzt?
Also ein $BLACKMAGIC, so dass folgendes gilt:
Code: | #echo 'yyy zzz yyy' | sed -re $BLACKMAGIC
xxx zzz xxx |
|
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Sat Jul 07, 2007 9:04 am Post subject: |
|
|
meinst du das? Code: | $ echo "yyy zzz yyy" | sed 's/yyy/xxx/g'
xxx zzz xxx |
_________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
sirro Veteran
Joined: 20 Jul 2003 Posts: 1472 Location: aachen.nrw.de.eu
|
Posted: Sat Jul 07, 2007 9:13 am Post subject: |
|
|
Ne. Das Beispiel ist natuerlich vereinfacht.
Beispiel: Ich will aus einem User-Agent mehrere Angaben rausziehen, die aber variieren koennen.
Code: | SonyEricssonK750i/R1L Browser/SEMC-Browser/4Profile/MIDP-2.0 Configuration/CLDC-1.1
SonyEricssonM600i/R100 Mozilla/4.0 (compatible; MSIE 6.0; Symbian OS; 513) Opera 8.65 [en] |
"SonyEricsson" ist bei allen gleich, darauf folgt das Modell. Soweit so einfach.
Allerdings hat nicht jedes Modell auch Symbian und Opera. Also will ich eine RE, die mir den Agent durch den Modell-Namen ersetzt und falls vorhanden auch noch die Angaben, dass es ein Symbian ist oder einen Opera-Browser benutzt.
Mit mehreren Ausdruecken (fuer jeden moeglichen Fall eine) ist das natuerlich kein Problem, aber das ist ja die unelegante Methode.
Ergebnis soll also etwas aehnliches sein wie:
Code: | M600i (Symbian OS; Opera 8.65)
SonyEricssonK750i () |
|
|
Back to top |
|
|
Ampheus l33t
Joined: 19 Oct 2005 Posts: 814 Location: Rheine, NRW, Germany
|
Posted: Sat Jul 07, 2007 9:32 am Post subject: |
|
|
hehe dann möchte ich auch nochmal
Code: | sed -e 's/<? /<?php /' -e 's/<?[^php]/<?php /' test.txt | less |
test.txt:
Code: | <?foo
<? foo
<?php foo |
Leider funktioniert es nicht ganz, da bei der ersten Zeile zwar der String richtig ersetzt wird, aber der darauffolgende um 1 Zeichen gekürzt wird.
Also so siehts dann aus:
Code: | <?php oo
<?php foo
<?php foo |
|
|
Back to top |
|
|
sirro Veteran
Joined: 20 Jul 2003 Posts: 1472 Location: aachen.nrw.de.eu
|
Posted: Sat Jul 07, 2007 9:40 am Post subject: |
|
|
Ampheus wrote: | s/<?[^php]/<?php /' |
Das funktioniert so nicht.
Das fette sagt, dass auf "<?" EIN zeichen folgt, dass weder 'p' noch 'h' ist. Da trifft auf deine erste Zeile zu, da 'f' weder 'p' noch 'h' ist also wird es mit <?php ersetzt.
Was du wahrscheinlich willst ist, dass der ganze ausdruck !="php" gematcht wird. Bisher habe ich da noch keine andere Loesung gesehen als die Zeichen aufwaendig einzeln in der Reihenfolge zu matchen und dann spaeter mit \1 wieder einsetzen: [^p][^h][^p]. Bei 3 Buchstaben ist das zwar nicht mehr uebersichtlich aber der overhead haelt sich in grenzen.
Evtl. geht es aber auch noch anders
Code: | sed -e 's/<? /<?php /' -e 's/<?\([^p][^h][^p]\)/<?php \1/' test.txt |
|
|
Back to top |
|
|
Ampheus l33t
Joined: 19 Oct 2005 Posts: 814 Location: Rheine, NRW, Germany
|
Posted: Sat Jul 07, 2007 10:34 am Post subject: |
|
|
Ah danke, die \1 hat mir geholfen |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Sat Jul 07, 2007 6:23 pm Post subject: |
|
|
@sirro:
'n quickhack; weder elegant noch schön aber funktioniert
Code: | sed 'G;s/ /\n/g' agent.log | sed '/Symbian/{N;s/\n/ /;b};/Opera/{N;s/\n/ /;b}' | sed '/Sony\|Symbian\|Opera/b;d' | sed 's/^SonyEricsson\(.*\)$/\n\1/' | sed '/^$/b;:x;$!N;s/\n\(.\)/ \1/;t x' |
_________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself
Last edited by think4urs11 on Sun Jul 08, 2007 2:23 pm; edited 2 times in total |
|
Back to top |
|
|
sirro Veteran
Joined: 20 Jul 2003 Posts: 1472 Location: aachen.nrw.de.eu
|
Posted: Sun Jul 08, 2007 2:19 pm Post subject: |
|
|
Immerhin
Aber da das in einer einzigen langen sed-Datei ist komme ich mit einem grep zwischendurch auch nicht weiter.
Hab jetzt einfach zwei Zeilen draus gemacht und nur nach Opera abgefragt, das muss reichen. |
|
Back to top |
|
|
think4urs11 Bodhisattva
Joined: 25 Jun 2003 Posts: 6659 Location: above the cloud
|
Posted: Sun Jul 08, 2007 2:23 pm Post subject: |
|
|
geht auch ganz ohne grep - hab es oben korrigiert _________________ Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself |
|
Back to top |
|
|
|