[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
gEDA-user: Add author=... to sym files
Attached is a simple script that adds text to a sym file.
It
. puts the text 200 units above the device=... line.
. ignores files which are symbolic links
. ignores old text with the same thing before the "="
. does this "in place", saving a backup file in file.old
$ ./add_text.pl "author=Karl Hammar" 78xx.sym
$ diff 78xx.sym 78xx.sym.old
3,4d2
< T 300 2000 5 10 0 0 0 0 1
< author=Karl Hammar
$ grep -B3 device= 78xx.sym
T 300 2000 5 10 0 0 0 0 1
author=Karl Hammar
T 300 1800 5 10 0 0 0 0 1
device=78/79xx
$
I can also do
$ ./add_text.pl "author=Karl Hammar" *.sym
It worked for me :)
Regards,
/Karl Hammar
-----------------------------------------------------------------------
Aspö Data
Lilla Aspö 148
S-742 94 Östhammar
Sweden
+46 173 140 57
#!/usr/bin/perl -w
use strict;
my $text;
my $var;
sub Usage() {
print "Usage:\n add_text.pl attr=text files...\n";
exit(1);
}
sub run($) {
my $file = shift;
lstat($file);
return 0 unless -f _;
return 0 unless open(FH, "$file");
my @line = <FH>;
close(FH);
if (grep(m/$text/, @line )) { # already there
return 0;
}
my @new;
my $pos = undef;
my $flag = 1;
for (@line) {
if ($flag) {
if ($pos) {
if (m/^device=/) { # put new text 200 above the device text
$pos =~ m/^T[ \t](\d+)[ \t]+(\d+)(.*)$/;
my $y = $2 + 200;
push @new, "T $1 $y$3\n";
push @new, "$text\n";
$flag = 0;
}
if (m/^$var=/) {
# ignore old value
} else {
push @new, $pos;
push @new, $_;
}
$pos = undef;
} else {
if (m/^T[ \t]+/) {
$pos = $_;
} else {
push @new, $_;
}
}
} else {
push @new, $_;
}
}
if ($flag) {
# no "device=..", put it anywhere
push @new, "T 1000 1000 5 10 0 0 0 0 1\n";
push @new, "$text\n";
}
return 0 unless open(FH, ">$file.new");
print FH @new;
close(FH);
`mv $file $file.old`;
`mv $file.new $file`;
}
if (@ARGV < 1) {
Usage();
}
$text = shift;
$var = undef;
if ($text =~ m/^(.+)=/) {
$var = $1;
} else {
Usage();
}
for (@ARGV) {
run($_);
}
_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user