Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[bash] Suchen/Ersetzen via Dictionary File?
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) Diskussionsforum
View previous topic :: View next topic  
Author Message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Fri Nov 22, 2013 5:58 pm    Post subject: [bash] Suchen/Ersetzen via Dictionary File? Reply with quote

Hallo Zusammen,

ich habe folgendes Problem:

Ich habe eine Datei, die so aussieht:

Code:
foo bar
123 987
alt neu
asdf ASDF


Nun sollen via bash in der Datei blabla.txt alle foo, durch bar, alle 123, durch 987, usw. ersetzt werden.

Leider habe fehlt mir im Moment eine Idee, wie ich das am besten realisieren könnte?

Evtl. hat ja Jemand eine Idee? :)
Back to top
View user's profile Send private message
Finswimmer
Bodhisattva
Bodhisattva


Joined: 02 Sep 2004
Posts: 5467
Location: Langen (Hessen), Germany

PostPosted: Fri Nov 22, 2013 6:43 pm    Post subject: Reply with quote

Schleife über die Dictionary Datei.
Pro Zeile trennst nach Leerzeichen: 1. Teil ist $search, 2. Teil $replace

Dann per sed s'#$search#$replace#'g die gesamte blabla Datei ersetzen.
_________________
Bitte auf Rechtschreibung, korrekte Formatierung und Höflichkeit achten!
Danke
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Fri Nov 22, 2013 7:45 pm    Post subject: Reply with quote

Ich habe es jetzt mal "quick'n'dirty" so gelöst:

Code:
#!/bin/bash


INFILE="dictionary.txt"
TARGET="my_file.txt"

cat $INFILE |while read i ; do
  SEARCH=${i%%\ *}
  i=${i#*\ }
  REPLACE=${i%%\ *}
  sed -i $TARGET -e "s/$SEARCH/$REPLACE/"
done


Geht sicherlich auch eleganter, aber so funktioniert es erst mal. ;)
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Nov 24, 2013 2:10 pm    Post subject: Reply with quote

Deutlich eleganter:
Code:
INFILE=dictionary.txt
TARGET=my_file.txt
while read SEARCH REPLACE
do sed -i -e "s'$SEARCH'$REPLACE'g" -- "$TARGET"
done <"$INFILE"

In beiden Fällen musst Du darauf achten, dass die letzte Zeile von dictionary.txt mit einem "newline" abgeschlossen wird, sonst schlägt das "while" zu früh fehl...
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sun Nov 24, 2013 2:17 pm    Post subject: Reply with quote

Man könnte ja zur Sicherheit noch ein,

Code:
echo /n >> $INFILE


einbauen. ;)
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Nov 24, 2013 2:22 pm    Post subject: Reply with quote

3PO wrote:
Man könnte ja zur Sicherheit noch ein,

Code:
echo /n >> $INFILE


einbauen. ;)

Und das Source-File modifizieren? Sehr schlechter Stil. Was ich meinte wäre ein besserer Test im Stile von
Code:
while {
  read SOURCE DEST
  [ -n "${DEST}" ]
}
do ...
done <...
Back to top
View user's profile Send private message
papahuhn
l33t
l33t


Joined: 06 Sep 2004
Posts: 626

PostPosted: Sun Dec 22, 2013 9:26 am    Post subject: Reply with quote

Das Problem an den Lösungen hier ist, dass bereits ersetzte Strings in einem weiteren Schleifendurchlauf nochmal ersetzt werden könnten. Beispiel:

Code:

foo bar
bar bier


soll angewendet werden auf den Text "Hello foobar". Was will man da als Resultat bekommen? "Hello barbier" oder "Hello bierbier"? In den Fällen, in denen ich solche Ersetzungen machen musste, war immer das Resultat "Hello barbier" gefordert. Das Vorgehen dazu ist dann, dass man nach "foo|bar" matcht und dann im Regex-Replace-Part einen Hash-Lookup macht. Wie das mit bash/sed ginge, weiss ich nicht, für Perl macht man das exemplarisch so:

Code:

#!/usr/bin/perl -w
use strict;                                                                   

my $str = 'Hello foobar';

my %hash = map {split /\s+/} <DATA>;
my $rx = join '|', keys %hash;

$str =~ s/($rx)/$hash{$1}/eg;

print $str;

__DATA__
foo bar
bar bier

_________________
Death by snoo-snoo!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum 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