PDA

View Full Version : [SOLVED] Simple bash script keeps erasing my files randomly



u-slayer
November 10th, 2008, 10:27 AM
I wanted to write a simple script that would read all the files given to it and replace double spaces with tabs. It works, sometimes but it also seems to randomly erase my files:(. Pls help.:confused:



for f in "$@"; do
echo -e "$f "
cat "$f" | sed 's/\x20\x20/\t/g' > "$f"
done

geirha
November 10th, 2008, 10:47 AM
Problem is that " > file " truncates the file. And that may happen before the cat command gets the chance to read the file. sed has an option (-i) to do the change "in place".


sed -i 's/ /\t/g' "$@"

u-slayer
November 11th, 2008, 01:46 AM
Thanks geirha, that worked.:)