Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

problem with redirection in cat and sed

5 posters

Go down

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

Post by SakeenaHarris 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

Back to top Go down

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

Post by mightyganesh 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 : 35

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 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 : 35

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 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

Back to top Go down

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

Post by Christopher 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 : 35

https://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 Fri Mar 12, 2010 1:07 pm


DineshThangaraju

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

Back to top Go down

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

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


DineshThangaraju

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

Back to top Go down

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

Post by Christopher 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 : 35

https://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

- Similar topics

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