Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    2

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

  2. #2
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    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:
    Code:
    grep -v $DEL ~/Desktop/text.txt > ~/Desktop/temp.txt && mv ~/Desktop/temp.txt ~/Desktop/text.txt
    Or, you can use the program sponge (a personal favorite):
    Code:
    grep -v $DEL ~/Desktop/text.txt | sponge ~/Desktop/text.txt
    Sponge 'soaks up' stdin and when it is done, writes to stdout.
    I don't know what distro you are on, but on Ubuntu, the package that contains sponge is called "moreutils".

  3. #3
    Just 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.

  4. #4
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Yeah, sponge is a very useful program (I use it a lot with sed). Since you are using Ubuntu, you can install sponge with
    Code:
    sudo apt-get install moreutils
    This package includes a whole bunch of very useful programs that are not included by default on a Unix system.
    - 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...