problem with redirection in cat and sed

Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by SakeenaHarris on Fri Mar 12, 2010 12:13 pm

cat littlefile
Marry had a little lamb.little lamb.little lamb
Mary had a little lamb whose fleece was white as snow
Every where that marywent,mary went,mary went.Everywhere that mary went
[@oracleclient ~]$ cat littlefile | sed -e 's/mary/naaz/g'
Marry had a little lamb.little lamb.little lamb
Mary had a little lamb whose fleece was white as snow
Every where that naazwent,naaz went,naaz went.Everywhere that naaz went
[@oracleclient ~]$ cat littlefile | sed -e 's/mary/naaz/g' > littlefile
[@oracleclient ~]$ cat littlefile
[@oracleclient ~]$
[@oracleclient ~]$ cat littlefile | sed -e 's/mary/naaz/g' > littlej
[@oracleclient ~]$ cat littlej
[@oracleclient ~]$

SakeenaHarris

Posts : 7
Points : 13
Join date : 2010-03-08

View user profile

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by mightyganesh on Fri Mar 12, 2010 12:47 pm

in the $ cat littlefile | sed -e 's/mary/naaz/g' > littlefile command
first the littlefile is opened in write mode first ! so all the content gets cleared!
so the sed command takes an empty file as the input and so it produces a empty output!

so u get an empty file at-last! Razz
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 31

View user profile http://ganeshguna.blogspot.com

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by mightyganesh on Fri Mar 12, 2010 12:53 pm

try this command

$ ls -l >abc

i mean create a file (abc) which does not exist already!

then open the file abc and see! u will see that abc is present in there !

it means first the output file is created and then the ls command is executed!
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 31

View user profile http://ganeshguna.blogspot.com

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by itssrinath on Fri Mar 12, 2010 12:55 pm

mightyganesh wrote:in the $ cat littlefile | sed -e 's/mary/naaz/g' > littlefile command
first the littlefile is opened in write mode first ! so all the content gets cleared!
so the sed command takes an empty file as the input and so it produces a empty output!

so u get an empty file at-last! Razz

And since the littlefile is already empty, in the second attempt using littlej, the output is empty.

[335920@oracleclient Srinath]$ cat>littlefile
Sometext
Second Line.
[335920@oracleclient Srinath]$ cat littlefile | sed -e 's/Some/some/g' > little
[335920@oracleclient Srinath]$ cat little
sometext
Second Line.

itssrinath

Posts : 4
Points : 5
Join date : 2010-02-26

View user profile

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by Christopher on Fri Mar 12, 2010 1:01 pm

if u want to store the output of sed in same file, go for '<<'. this ll append the output of sed command to the input file.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 31

View user profile http://unixcpp.forumotion.com

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by DineshThangaraju on Fri Mar 12, 2010 1:07 pm


DineshThangaraju

Posts : 18
Points : 19
Join date : 2010-02-26
Age : 31
Location : India

View user profile

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by DineshThangaraju on Fri Mar 12, 2010 1:11 pm


DineshThangaraju

Posts : 18
Points : 19
Join date : 2010-02-26
Age : 31
Location : India

View user profile

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by Christopher on Fri Mar 12, 2010 1:13 pm

still if u want to do it, use temp file.

Code:
$ sed 's/PINK/BLUE/g' 1.txt > 1.temp ; cat 1.temp > 1.txt ; rm 1.temp

it will store the output of sed command in the same file.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 31

View user profile http://unixcpp.forumotion.com

Back to top Go down

problem with redirection in cat and sed Empty Re: problem with redirection in cat and sed

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum