Unix Shell - cat with pipe to grep ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > June 2005 > cat with pipe to grep ?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author cat with pipe to grep ?
Marek Stepanek

2005-06-17, 5:53 pm




hello all,


first I am working with tcsh on Macintosh ...

Is it possible to sent the content of all html-files to all files with the
same name one level up, and replacing same time the "../../" with "../" ?

Hope this question was clear ! I am imagining (?good English?) something
like the following :

% cat file01.htm > ../folder/file01.htm | grep !../../!../!

I absolutely don't know grep in shell, so I wrote this a bit perlish, just
to illustrate, what I mean.


greetings from Munich



marek

--
________________________________________
______________________________
___PODIUM_INTERNATIONAL_// _the_embassy_for_talented_young_musician
s___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
________________________________________
______________________________



datacide

2005-06-17, 5:53 pm

Hello,

what you are looking for is 'sed' and not 'grep'
The syntax would be

cat filename | sed 's/searchterm/replacewith/g' >>
.../directoryup/filename

otherwise man sed ;)

Ed Morton

2005-06-17, 5:53 pm



datacide wrote:
> Hello,
>
> what you are looking for is 'sed' and not 'grep'
> The syntax would be
>
> cat filename | sed 's/searchterm/replacewith/g' >>
> ../directoryup/filename


UUOC:

sed 's/searchterm/replacewith/g' filename >> ../directoryup/filename


> otherwise man sed ;)
>

Ed Morton

2005-06-17, 5:53 pm



Marek Stepanek wrote:

>
>
> hello all,
>
>
> first I am working with tcsh on Macintosh ...
>
> Is it possible to sent the content of all html-files to all files with the
> same name one level up, and replacing same time the "../../" with "../" ?
>
> Hope this question was clear ! I am imagining (?good English?) something
> like the following :
>
> % cat file01.htm > ../folder/file01.htm | grep !../../!../!


sed 's:../../:../:g' file01.htm > ../file01.htm

Regards,

Ed.

> I absolutely don't know grep in shell, so I wrote this a bit perlish, just
> to illustrate, what I mean.
>
>
> greetings from Munich
>
>
>
> marek
>

Marek Stepanek

2005-06-17, 5:53 pm

thanx !

your answers are really quick! I will experiment with your suggestions and
"sed".


greetings


marek


--
________________________________________
______________________________
___PODIUM_INTERNATIONAL_// _the_embassy_for_talented_young_musician
s___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
________________________________________
______________________________



Kenny McCormack

2005-06-19, 5:51 pm

In article <d8utq3$31r@netnews.proxy.lucent.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>
>sed 's:../../:../:g' file01.htm > ../file01.htm


ITYM:

gawk 'gsub(/\.\.\/\.\.\//,"../")+1' file01.htm > ../file01.htm

(Or something like that)

Ed Morton

2005-06-19, 5:51 pm



Kenny McCormack wrote:
> In article <d8utq3$31r@netnews.proxy.lucent.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
> ...
>
>
>
> ITYM:
>
> gawk 'gsub(/\.\.\/\.\.\//,"../")+1' file01.htm > ../file01.htm
>
> (Or something like that)


Why?
Marek Stepanek

2005-06-20, 7:53 am



thank you all for the great help. I played around with the sed tool, I tried
first with % man sed - which was a total enigma for me, but then I
discovered this

http://www.grymoire.com/Unix/Sed.html

I started to understand sed and your suggestions.

but I made some errors (line break from email client):

[markslap:www.taxinik.de/nik-limousines/fotoalbum] mareklap% sed
's:../../:../:g' pictures01.htm > ../../fotoalbum/pictures01.htm

gave me an empty "../../fotoalbum/pictures01.htm" Why ? I corrected my
attempt to

[markslap:www.taxinik.de/nik-limousines/fotoalbum] mareklap% sed
's:../../:../:g' < pictures01.htm > ../../fotoalbum/pictures01.htm

which gave me some funny results, until I realized, that the "." needs to be
escaped like follows :

[markslap:www.taxinik.de/nik-limousines/fotoalbum] mareklap% sed
's:\.\./\.\./:../:g' < pictures01.htm > ../../fotoalbum/pictures01.htm

Am I on the right road now ? :-)

Thank you all once again for your friendly help



marek



--
________________________________________
______________________________
___PODIUM_INTERNATIONAL_// _the_embassy_for_talented_young_musician
s___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
________________________________________
______________________________




Marek Stepanek

2005-06-20, 6:05 pm



Perhaps a patient soul could be so nice and answer me one supplemental
question :


is it possible to do this is "batch mode" too ? :


mareklap% sed 's:\.\./\.\./:../:g' < pictures01_01.htm >
.../../fotoalbum/pictures01_01.htm

mareklap% sed 's:\.\./\.\./:../:g' < pictures01_02.htm >
.../../fotoalbum/pictures01_02.htm


.... etc until :

mareklap% sed 's:\.\./\.\./:../:g' < pictures01_12.htm >
.../../fotoalbum/pictures01_12.htm


I love "sed" :-) really great, if only it would support a more complex grep
would be even better ...


greetings from Munich



marek




--
________________________________________
______________________________
___PODIUM_INTERNATIONAL_// _the_embassy_for_talented_young_musician
s___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
________________________________________
______________________________



Ed Morton

2005-06-20, 6:05 pm



Marek Stepanek wrote:
>
> Perhaps a patient soul could be so nice and answer me one supplemental
> question :
>
>
> is it possible to do this is "batch mode" too ? :
>
>
> mareklap% sed 's:\.\./\.\./:../:g' < pictures01_01.htm >
> ../../fotoalbum/pictures01_01.htm

<snip>
> mareklap% sed 's:\.\./\.\./:../:g' < pictures01_12.htm >
> ../../fotoalbum/pictures01_12.htm


for f in pictures*.html
do
sed 's:\.\./\.\./:../:g' < "$f" > "../../fotoalbum/$f"
done

>
> I love "sed" :-) really great, if only it would support a more complex grep
> would be even better ...


What kind of complex search do you have in mind? Note that the
equivalent to the sed statement would just be the following in awk:

awk 'gsub("\.\./\.\./","../")1' file

and you can easily specify complex conditions in awk if sed doesn't
satisfy your needs

Ed.
Marek Stepanek

2005-06-20, 6:05 pm

On 20.06.2005 16:04, in article d96ieg$2f9@netnews.proxy.lucent.com, "Ed
Morton" <morton@lsupcaemnt.com> wrote:

>
>
> Marek Stepanek wrote:
> <snip>
>
> for f in pictures*.html
> do
> sed 's:\.\./\.\./:../:g' < "$f" > "../../fotoalbum/$f"
> done
>
>
> What kind of complex search do you have in mind? Note that the
> equivalent to the sed statement would just be the following in awk:
>
> awk 'gsub("\.\./\.\./","../")1' file
>
> and you can easily specify complex conditions in awk if sed doesn't
> satisfy your needs
>
> Ed.




Thanx, Ed,

Wow ! Really powerful ! I am even angry, because I spoiled so many time
before discovering the power of Shell !
Very helpful your answers ! And so quick ! I will have a look into
% man awk
and probably make a search on the web, because the man pages of awk will be
certainly as cryptic as the of sed, isn't it ? :-)

Does the grep engine of awk is PCRE-based ? I would like something similar,
what I do in PERL or with the Macintosh Text-Editor BBEdit.


greetings from Munich



marek


--
________________________________________
______________________________
___PODIUM_INTERNATIONAL_// _the_embassy_for_talented_young_musician
s___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
________________________________________
______________________________






Marek Stepanek

2005-06-20, 6:05 pm

On 20.06.2005 16:04, in article d96ieg$2f9@netnews.proxy.lucent.com, "Ed
Morton" <morton@lsupcaemnt.com> wrote:

> for f in pictures*.html
> do
> sed 's:\.\./\.\./:../:g' < "$f" > "../../fotoalbum/$f"
> done
>



oh, Ed and all,

sorry for one more beginners question:

how do I have to put your suggestion directly into my (tcsh) shell window ?
because of the line feeds, which the shell is taking for a "ok" or "do it!"
Escaping the line feed \
is not helping!

Or is it possible to do such kind of scripts with an external text document
only ?


marek (blusching - really sorry to bother this group, with such childish
questions ... )



--
________________________________________
______________________________
___PODIUM_INTERNATIONAL_// _the_embassy_for_talented_young_musician
s___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
________________________________________
______________________________



Chris F.A. Johnson

2005-06-20, 6:05 pm

On 2005-06-20, Marek Stepanek wrote:
> On 20.06.2005 16:04, in article d96ieg$2f9@netnews.proxy.lucent.com, "Ed
> Morton" <morton@lsupcaemnt.com> wrote:
>
>
> oh, Ed and all,
>
> sorry for one more beginners question:
>
> how do I have to put your suggestion directly into my (tcsh) shell window ?
> because of the line feeds, which the shell is taking for a "ok" or "do it!"
> Escaping the line feed \
> is not helping!
>
> Or is it possible to do such kind of scripts with an external text document
> only ?


If you change your shell to a Bourne-type shell, then you can enter
it at the command line. I recommend bash, which is as good at the
prompt as tcsh; others will suggest zsh or ksh.

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Stephane Chazelas

2005-06-21, 2:50 am

On Mon, 20 Jun 2005 09:04:46 -0500, Ed Morton wrote:
[...]
> awk 'gsub("\.\./\.\./","../")1' file

[...]

$ echo aa/aa/aa | awk 'gsub("\.\./\.\./","../")1'
.../aa

You need:

awk '{gsub("\\.\\./\\.\\./", "../"); print}' file

(escape "." for the regexp, and backslash for the double
quotes).

or
awk '{gsub(/\.\.\/\.\.\//, "../"); print}' file

No need to be over-cryptic to have the concatenation of the
result of gsub and "1" as a condition for the default action.

--
Stephane
Kenny McCormack

2005-06-21, 5:52 pm

In article <slrndbffkl.fbd.stephane@duey.spider.com>,
Stephane Chazelas <stephane@artesyncp.com> wrote:
>On Mon, 20 Jun 2005 09:04:46 -0500, Ed Morton wrote:
>[...]
>[...]
>
>$ echo aa/aa/aa | awk 'gsub("\.\./\.\./","../")1'
>../aa
>
>You need:
>
> awk '{gsub("\\.\\./\\.\\./", "../"); print}' file
>
>(escape "." for the regexp, and backslash for the double
>quotes).


Right - that's why when I gave the original AWK solution, I used a slashed
RE, not a quoted (aka, dynamic) one.

> awk '{gsub(/\.\.\/\.\.\//, "../"); print}' file
>
>No need to be over-cryptic to have the concatenation of the
>result of gsub and "1" as a condition for the default action.


There is if you are playing golf.

(I.e., your statement is equivalent to: There is no need to try to minimize
the number of strokes it takes to get the ball in the cup - just take your
time, you'll get it in there eventually)

Chris F.A. Johnson

2005-06-22, 2:50 am

On 2005-06-21, Kenny McCormack wrote:
> In article <slrndbffkl.fbd.stephane@duey.spider.com>,
> Stephane Chazelas <stephane@artesyncp.com> wrote:
>
> Right - that's why when I gave the original AWK solution, I used a slashed
> RE, not a quoted (aka, dynamic) one.
>
>
> There is if you are playing golf.
>
> (I.e., your statement is equivalent to: There is no need to try to minimize
> the number of strokes it takes to get the ball in the cup - just take your
> time, you'll get it in there eventually)


A good golfer knows that trying for a hole in one is not always the
best strategy.


--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com