Operating System - HP-UX
1827283 Members
3261 Online
109717 Solutions
New Discussion

Interesting Scripting Question - Stdout and File Recreation

 
SOLVED
Go to solution
Ian Dennison_1
Honored Contributor

Interesting Scripting Question - Stdout and File Recreation

Pop quiz,...

I know we can recreate a Variable and re-assign the value directly,

eg. export PATH=$PATH":/tmp"

BUT! Can I do the same in one command for a log file?

eg. grep -v "Alpha" /tmp/file1 >/tmp/file1

Or am I resigned to 2 commands?
eg. grep -v "Alpha" /tmp/file1 >/tmp/file2
mv /tmp/file2 /tmp/file1

Share and Enjoy! Ian

Building a dumber user
7 REPLIES 7
Justo Exposito
Esteemed Contributor
Solution

Re: Interesting Scripting Question - Stdout and File Recreation

Hi Ian,

If you do:
grep -v "Alpha" /tmp/file1 |tee /tmp/file1

Works Fine!!

Regards,

Justo.
Help is a Beatiful word
Thierry Poels_1
Honored Contributor

Re: Interesting Scripting Question - Stdout and File Recreation

Hi,

sorry, but you will indeed need an intermediate file if you use redirection.

If you really do not want an extra file, you could use and ed script:

ed yourfile << [EOT]
,g/searchstring/d
w
q
[EOT]


good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Thierry Poels_1
Honored Contributor

Re: Interesting Scripting Question - Stdout and File Recreation

We have a winner !! (read as 10-pointer ;-)
The tee trick indeed does the trick perfectly.

Just add "> /dev/null" to get rid of the output on screen (don't know how big the file is).
grep -v "Alpha" /tmp/file1 |tee /tmp/file1 > /dev/null

regards,
Thierry
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Ian Dennison_1
Honored Contributor

Re: Interesting Scripting Question - Stdout and File Recreation

Magic answers both! Thanks gentlemen, much appreciated!

Ian
Building a dumber user
Justo Exposito
Esteemed Contributor

Re: Interesting Scripting Question - Stdout and File Recreation

Hi Again,

You are welcome Mister.

Best Regards,

Justo.
Help is a Beatiful word
John Palmer
Honored Contributor

Re: Interesting Scripting Question - Stdout and File Recreation

Ian,

Be aware that the tee trick only works for small amounts of data.

tee is useful because you specify the output filename as an argument, if you had to use > then the shell truncates it before any processing begins. However tee is actually reading and writing the same file and at some point the write process will effectively truncate the file and you will lose data.

Some tests that I have done indicate that you will get away with writing between 18 and 24Kb but personally I wouldn't trust it unless you're sure that the input file is very small.

Regards,
John
Ian Dennison_1
Honored Contributor

Re: Interesting Scripting Question - Stdout and File Recreation

John,

Thanks for the warning, will definitely check sizings before using this methodology.

Ian
Building a dumber user