I'm working on a bash script for partially automating router connection in command line. This is part where I have a problem:
#!/bin/bash
STRING2="ESSID"
MYPWD=$(pwd)
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?
Edit
Without the last piped grep, text file contains some text like this after execution:
1:___________ESSID:"somessid"
2:___________ESSID:"somessid"
3:___________ESSID:"somessid"
4:___________ESSID:"somessid"
ps: <__> stands for white space
#!/bin/bash
#declaring a string variable
STRING="MERHABA DUNYA"
STRING2="ESSID"
COUNTER="1"
MYPWD=$(pwd)
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"
-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