Results 1 to 4 of 4
im having this problem. theoretically it makes sense but when I go to test it doesn't work
the code is:
echo "Search for what you delete"
read DEL
grep -v ...
- 04-04-2009 #1Just Joined!
- Join Date
- Apr 2009
- Posts
- 2
bash syntax help
im having this problem. theoretically it makes sense but when I go to test it doesn't work
the code is:
echo "Search for what you delete"
read DEL
grep -v $DEL ~/Desktop/text.txt > ~/Desktop/text.txt
The problem is after ~/Desktop/text.txt (>). I want it to erase the existing contents and add the new one.
I know after the user enter what he/she wants to delete, grep -v grabs all the everything that doesnt match, that works fine. but when i redirect the output back to the text, I use ( > or >! ) and it erases everything from the text file and leaves it blank. When I use >> it redirects the output fine but just adds to the text file.
I want to know if I am doing a syntax error or that i Need to add something else to the code
any help will be greatly appreciated.
Thanks.
- 04-05-2009 #2
Linux doesn't let you overwrite a file that you are currently reading. Grep ouputs its lines as it finds them, and bash tries to write them to the file, but it is the same file grep is still reading from.
You either have to have some sort of temporary file:
Or, you can use the program sponge (a personal favorite):Code:grep -v $DEL ~/Desktop/text.txt > ~/Desktop/temp.txt && mv ~/Desktop/temp.txt ~/Desktop/text.txt
Sponge 'soaks up' stdin and when it is done, writes to stdout.Code:grep -v $DEL ~/Desktop/text.txt | sponge ~/Desktop/text.txt
I don't know what distro you are on, but on Ubuntu, the package that contains sponge is called "moreutils".
- 04-05-2009 #3Just Joined!
- Join Date
- Apr 2009
- Posts
- 2
awsome i didnt know bash worked that way. this problem had been bugging my for a bit but i didnt think to use a dummy. first option worked! but its the first time ive heard of the program sponge looks cool. its an actual command?
im using ubuntu, Im gonna go test the program sponge.
Thanks alot for the help I really appreciate it.
- 04-05-2009 #4
Yeah, sponge is a very useful program (I use it a lot with sed). Since you are using Ubuntu, you can install sponge with
This package includes a whole bunch of very useful programs that are not included by default on a Unix system.Code:sudo apt-get install moreutils
- sponge: soak up standard input and write to a file
- ifdata: get network interface info without parsing ifconfig output
- ifne: run a program if the standard input is not empty
- vidir: edit a directory in your text editor
- vipe: insert a text editor into a pipe
- ts: timestamp standard input
- combine: combine the lines in two files using boolean operations
- pee: tee standard input to pipes
- zrun: automatically uncompress arguments to command
- mispipe: pipe two commands, returning the exit status of the first
- isutf8: check if a file or standard input is utf-8
- lckdo: execute a program with a lock held


Reply With Quote