Subject: Re: A shell script for search and replace
Subject: Re: filenames
Subject: Re: search and replace in unix
Subject: Re: any filenames converter ?
Subject: Re: upper to lower case DIRS - how to change recursively
Subject: Re: Date puzzle
Subject: Re: Getting more lines out of grep?
From: mcclellantj@harrier (Tad McClellan)
Newsgroups: comp.unix.questions
Subject: Re: A shell script for search and replace
Date: 4 Mar 1996 17:29:11 GMT
Lines: 17
Reply-To: mcclellantj@lfwc.lockheed.com
Uri Simchoni (simchoni@netvision.net.il) wrote:
: I want to write a shell script (or any other automatic means) for
: replacing all occurences of "index.htm" with "index.html" in all
: *.htm files in a directory.
: Any ideas on how to accomplish this using sed or any other standard unix
: tool?
perl -p -i.bak -e 's/\.htm\b/.html/' filename
--
Tad McClellan, Logistics Specialist (IETMs and SGML guy)
email: mcclellantj@lfwc.lockheed.com
Interesting trivia: If you took all the sand in North Africa and spread
it out... it would cover the Sahara desert.
--
From: "Kuntal M. Daftary" <daftary@cisco.com>
Newsgroups: comp.unix.questions
Subject: Re: filenames
Date: Fri, 26 Mar 1999 11:42:13 -0800
Lines: 22
Reply-To: "Kuntal M. Daftary" <daftary@cisco.com>
Mime-Version: 1.0
To: Alex MacAskill <026759m-@-acadiau.ca>
In-Reply-To: <36FBB61E.F5FFC8FB@-acadiau.ca>
On Fri, 26 Mar 1999, Alex MacAskill wrote:
> anyone know how I could take a directory of files and renameF} them all to
> say like pic1.jpg pic2.jpg etc etc
>
> So it would start by naming the first one pic1.jpg the second file would be
> pic2 and so on.
>
> Any help on how i could do this would be greatly appreciated. Thx in
> advance. PS I am running on a SunOS 5.6
since you havent mentioned what are the existing filenames i am assuming that
they are some random names like "apple.jpg" "girl.jpg" etc
if you have perl this is what you can do:
perl -e 'for(@ARGV) {$o=$_;$_="pic".++$i.".jpg";rename($o,$_) unless -e}' *.jpg
Kuntal Daftary
--
From: Steven Hand <smh22@cl.cam.ac.uk>
Newsgroups: alt.unix.wizards
Subject: Re: search and replace in unix
Date: 30 Mar 1999 09:47:11 +0100
Lines: 21
Paul Clifford <pcliff@mcs.com> writes:
> I have about 150 ascii text files located in one directory on a unix
> system. I want to search for all occurrences of a string of text (an old
> e-mail address) in every file and replace it with a new string (a new
> e-mail address).
>
> Is there a command I could run from a the unix shell, or from within VI,
> to change this text in all files in which it appears?
Using perl is probably the easiest (if you have it installed); e.g.
$ perl -pi.orig -e 's/old addrress/new address/' *
will replace the old address with the new in each file, making a
backup file called <fname>.orig in each case. If you don't want
backups, omit the ".orig" from the above command line.
HTH,
S.
--
Newsgroups: comp.sys.hp.hpux,comp.unix.misc,comp.unix.shell,comp.unix.questions
Subject: Re: any filenames converter ?
From: merlyn@stonehenge.com (Randal L. Schwartz)
Lines: 28
Date: 22 Apr 1999 13:41:57 -0700
>>>>> "Kuntal" == Kuntal M Daftary <daftary@cisco.com> writes:
Kuntal> On 22 Apr 1999, Kit-pui Wong wrote:
>> Is there any Un*x utility/shell script that can simply
>> convert in a fly all filenames under a directory to
>> pure-lower (or pure-upper) case ?
Kuntal> to pure uppercase:
Kuntal> perl5 -e 'for(@ARGV[1..$#ARGV]){rename($_,uc($_))}' <filelist>
Missed one. :)
Kuntal> to pure lowercase:
Kuntal> perl5 -e 'for(@ARGV[1..$#ARGV]){rename($_,lc($_))}' <filelist>
and again. $ARGV[0] is the first name in the list.
Also, you've just clobbered FOO.C if both foo.c and FOO.C exist.
Do this instead:
perl -e 'for (@ARGV) { my $new = uc($_); rename $_, $new unless -e $new }' ...
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me
--
From: ken@halcyon.com (Ken Pizzini)
Newsgroups: comp.unix.misc,comp.unix.questions
Subject: Re: upper to lower case DIRS - how to change recursively
Date: 22 Apr 1999 21:14:58 GMT
Lines: 12
Reply-To: ken@halcyon.com
User-Agent: slrn/0.9.5.3 (UNIX)
On Thu, 22 Apr 1999 09:15:33 -0400,
Tom Kralidis <tom.kralidis@ccrsDotnrcandOtgc.ca> wrote:
>I have numerous directories oriented as follows: W123, W111, W090,
>etc., about 40 of them. Within each of these dirs are more dirs eg.
>N45, N33, N66, etc.
>
>Anyone know of a quick fix to make them ALL lowercase eg. n45, n66,
>w123, etc.??
perl -MFile::Find -e 'finddepth sub {rename $_, lc($_)}, "."'
--Ken Pizzini
--
From: duke_of_chaos@my-dejanews.com
Newsgroups: comp.unix.questions
Subject: Re: Date puzzle
Date: Fri, 19 Mar 1999 22:15:00 GMT
Lines: 34
> > Using standard unix commands, how can you find yesterdays date?
>
> It is not easy to do this using standard Unix commands. I would
> recommend you use one of the various date utilIties for Perl or Python.
If you want to go the perl way, tehn here is a small perl script to do this..
#---------------------------------------------------------------------------
# Returns Yesterday's date in yyyy/mm/dd format
#---------------------------------------------------------------------------
sub Get_Yesterdays_Date {
local(@yester_day, $date);
local($day, $month, $year);
@yester_day = localtime(time - 60*60*24);
$day = $yester_day[3]; $day = "0".$day if($day < 10);
$month = $yester_day[4]+1; $month = "0".$month if($month < 10);
$year = 1900 + $yester_day[5];
$date = $year . "/" . $month . "/" . $day;
print STDOUT "\n\nYesterday's Date: $date \n" if($Verbose == 1);
return $date;
}
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
--
From: "Kuntal M. Daftary" <daftary@cisco.com>
Newsgroups: comp.unix.questions
Subject: Re: Getting more lines out of grep?
Date: Wed, 21 Apr 1999 11:32:24 -0700
Lines: 138
Mime-Version: 1.0
In-Reply-To: <slrn7hcbho.nl.ken@pulsar.halcyon.com>
On 15 Apr 1999, Ken Pizzini wrote:
> On Thu, 15 Apr 1999 16:08:45 GMT, <terry8053@my-dejanews.com> wrote:
> > Does anyone know of a way I can grep a pattern out of a file and get the
> > respones to include the line with the pattern and the line above and below
> > it? It not have to be simple I can create an alais for it or is there
> > another command I could use?
>
> You can't do it with a run-of-the-mill grep. With GNU grep you can use "-C1"
> (1 line of above-and-below context) option. Otherwise you'll need to cobble
> something together with sed/awk/perl/other.
here is a perl script that i use.
Kuntal Daftary
----- 8< -----
#!/usr/local/bin/perl5
#==============================================================================
# Searches for a pattern in a file like grep but also prints lines above
# and below the line which matched the pattern. Also highlights the line
# which matched the pattern and the pattern itself. Offers a few options.
#------------------------------------------------------------------------------
# Kuntal Daftary Sun Mar 29 13:01:15 PST 1998
#==============================================================================
#==============================================================================
# source the required libraries
#------------------------------------------------------------------------------
use Term::ANSIColor;
#==============================================================================
# Find the basename on the command name and initialize valiables
#------------------------------------------------------------------------------
($self = $0) =~ s:.*/::;
#==============================================================================
# Basic options parsing
#------------------------------------------------------------------------------
die "$self: Missing file name\n" unless @ARGV;
while (@ARGV) {
$word = shift;
if ($word =~s/^-//) {
if ($word =~ /^he?l?p?$/) {
print "\nUsage: $self [options] <pattern> <list of files>\n";
print " $self -help\n\n";
print "where options include:\n\n";
print "-above <n> show n lines above the match [default: 3]\n";
print "-below <n> show n lines below the match [default: 3]\n";
print "-i case insensitive search [default: off] \n";
print "-hilite <line,patt> hilights pattern and line where it occurs\n";
print " possible values for <line> and <patt> are\n";
print " bold,reverse,underline,blink\n";
print "
exit 0;
} elsif ($word =~ /^ab?o?v?e?$/) {
$above = shift;
die "$self: -$word: Requires numeric argument\n" if $above =~ /\D/;
die "$self: -$word: Requires number >= 0\n" if ($above < 0);
} elsif ($word =~ /^be?l?o?w?$/) {
$below = shift;
die "$self: -$word: Requires numeric argument\n" if $below =~ /\D/;
die "$self: -$word: Requires number >= 0\n" if ($below < 0);
} elsif ($word =~ /^i$/) {
$caseInsensitive = "i";
} elsif ($word =~ /^hil?i?t?e?$/) {
$hilite = shift;
@hilite = split(",", $hilite);
$hilite = 1;
} else {
die "$self: -$word: unknown switch\n";
}
} else {
push (@files, $word);
}
}
$pattern = shift @files;
die "$self: Missing pattern\n" unless $pattern;
die "$self: Missing filename\n" unless @files;
if ($hilite) {
@hlline = ($hilite[0]);
@hlpatt = ($hilite[1]);
} else {
@hlline = (undef);
@hlpatt = (undef);
}
#==============================================================================
# Parameter defaulting
#------------------------------------------------------------------------------
$above = 3 unless defined $above;
$below = 3 unless defined $below;
$hilite = 0 unless defined $hilite;
$caseInsensitive = "" unless defined $caseInsensitive;
#==============================================================================
# Begin searching
#------------------------------------------------------------------------------
foreach $file (@files) {
open (FILE, "$file") || warn "$self: $file: $!\n";
while (<FILE>) {
s/\n$//;
&print_n_shift;
($below > 0 ? $below[$below-1] : $curr) = $_;
}
close (FILE);
for (0 .. $below) {
&print_n_shift;
undef $below[$below-$_-1] if ($below > 0);
}
}
print "#"x79, "\n" if ($matches);
sub print_n_shift {
if (defined $curr && $curr =~ m!$pattern!) {
++$matches;
print "="x79, "\n", " FILE: $file\n" if (! $DONE{$file}++);
print "-"x79, "\n", join("\n", @above), "\n";
while ($curr =~ m!$pattern!) {
print colored ($`, @hlline);
print colored ($&, @hlpatt);
$curr = $';
}
print colored ($', @hlline);
print colored ("", "reset");
print "\n";
print join("\n", @below), "\n";
}
for (0 .. $above-2) { $above[$_] = $above[$_+1]; }
$above[$above-1] = $curr if ($above > 0);
$curr = $below[0] if ($below > 0);
for (0 .. $below-2) { $below[$_] = $below[$_+1]; }
}
--
|
Hosted by: |
|
©copyright 1995-2002 sysinfo.com