I'm working on a bash script for partially automating router connection in command line. This is part where I have a problem:

sudo iwlist wlan1 scan | grep $STRING2 | grep -n $STRING2 > $STRING2.txt
sed -r -e 's/\s+//g' -e 's/:ESSID//' $MYPWD"/"$STRING2".txt

The last line opens a text file, then deletes white spaces and also :ESSID's from the text file which is $MYPWD/$STRING2.txt. What I want to do is also write the output of sed to that file by piping. A previous approach was piping grep : > $MYPWD"/"$STRING2".txt" at the end such as :

sed -r -e 's/\s+//g' -e 's/:ESSID//' $MYPWD"/"$STRING2".txt | grep : > $MYPWD"/"$STRING2".txt"

But it ended up with an empty $MYPWD/$STRING2.txt. I'm not sure why this is happening. Any ideas?


Without the last piped grep, text file contains some text like this after execution:





ps: <__> stands for white space

#declaring a string variable
echo -e "Later you can check the recent networks around you from : " $MYPWD"/"$STRING2".txt\n"
#or MYPWD=${pwd}
#-e flag enables \n escape
echo -e $STRING2".txt is being updated\n. . .\n"
sudo iwlist wlan1 scan | grep $STRING2 | grep -n $STRING2 > $STRING2.txt # | sed -i# #s/ //g $STRING2.txt > $STRING2.txt
sed -r -e 's/\s+//g' -e 's/:ESSID//' $MYPWD"/"$STRING2".txt"
NUMLINE=$(sudo cat $MYPWD/$STRING2.txt | wc -l)
echo -e "\n. . ."
echo -e "\nThere are "$NUMLINE " visible networks\n"
  • Could you fix the quotes please? Show us the exact command in your script. What you show won't run so presumably, that's not what you're actually running. Please show us exactly what you are using.
    – terdon
    Jul 28, 2015 at 16:21
  • 1
    What platform are you on? If you have GNU sed, you can use the -i flag to modify a file in place. I think the version of OSX supports that as well, but you have to provide an extension for the backup file Jul 28, 2015 at 16:21
  • @terdon I've edited with the exact command Jul 28, 2015 at 16:27
  • @EricRenouf GNU sed, you're right. Thanks for that it solved the problem. Jul 28, 2015 at 16:32

1 Answer 1


When you have a command such as this

sed -e ... "datafile" >"datafile"

You will end up with a zero length result. The reason is that the shell sets up stdin and stdout before it executes the command. So stdout is sent to the file datafile, creating or truncating it in the process, and only then is the sed -e ... "datafile" run.

A common solution is something like this

sed -e ... "datafile" >"datafile.tmp" && mv -f "datafile.tmp" "datafile"

Some commands (including versions of sed) have the edit in place option:

sed -i.bak -e ... "datafile"
sed --in-place=.bak -e ... "datafile"
  • ...and just found that this has been also suggested in a comment. Why not make that an answer and save others unnecessary time and effort duplicating something that's already been said but lost in comments?
    – roaima
    Jul 28, 2015 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.