'awk' help for script to clean out wireless.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers 'awk' help for script to clean out wireless.
# 1  
Old 01-10-2015
'awk' help for script to clean out wireless.

I've been working on a script to clean out the wireless connections on my MAC. I know the terminal command to do this (that part is not rocket science Smilie )

So I thought I would have a go at automating this but I'm having trouble with the 'awk' command & the correct context. My code is :=


Code:
#!/bin/sh
# Removes ALL known wireless on that machine
for wireless in `networksetup -listpreferredwirelessnetworks en1 | awk '{ print $0}'`
do
echo Deleting "$wireless"
/usr/sbin/networksetup -removepreferredwirelessnetwork en1 "$wireless"
done
/usr/sbin/networksetup -addpreferredwirelessnetworkatindex en1 "This is a test" 0 WPA2 123456789
exit 0

But the awk command returns every word in the echo & does not pull the line out. I did read some where that %0 should return every line back, bit it does not seem to work for me. Wondering where I was going wrong?
# 2  
Old 01-10-2015
provide sample input, desired output
# 3  
Old 01-10-2015
This is what I get when I run the script. The awk command is not passing back "This Is A Test". Its passing back only single words, so my script its trying to delete the wireless "This", "Is", "A", "Test" that don't exist. (Ignore the "preferred networks on en1")


Code:
Lord:Desktop administrator$ sudo ./wireless\ clean\ up.sh 
Deleting Preferred
Network Preferred was not found in the preferred networks list
Deleting networks
Network networks was not found in the preferred networks list
Deleting on
Network on was not found in the preferred networks list
Deleting en1:
Network en1: was not found in the preferred networks list
Deleting This
Network This was not found in the preferred networks list
Deleting is
Network is was not found in the preferred networks list
Deleting a
Network a was not found in the preferred networks list
Deleting test
Network test was not found in the preferred networks list
Network This is a test is already in the preferred networks list
Lord:Desktop administrator$

I did try using this command :-

Code:
networksetup -listpreferredwirelessnetworks en1 > wireless.txt

in slightly modified code & using awk to read the file but it kept error out on the 1st line.

---------- Post updated at 07:24 PM ---------- Previous update was at 06:16 PM ----------

Or you can just delete
"/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist"

file... Gonna crack the script tho if it kills me Smilie
# 4  
Old 01-10-2015
The awk script isn't your problem and isn't helping in your script. The for is seeing words to process; not lines. Try this instead:
Code:
#!/bin/sh
# Removes ALL known wireless on that machine
network setup -listpreferredwirelessnetworks en1 | while read wireless
do      echo Deleting "$wireless"
        /usr/sbin/networksetup -removepreferredwirelessnetwork en1 "$wireless"
done
/usr/sbin/networksetup -addpreferredwirelessnetworkatindex en1 "This is a test" 0 WPA2 123456789
exit 0

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-10-2015
Thank you for that Don Smilie You are a star Smilie Worked like a charm.

The reason I used "awk" as I have a script that removes printers (code below) & that worked fine. So I was kinda of scratching my head so to speak.

Code:
#!/bin/sh
# Removes ALL printers on that machine
for printer in `lpstat -p | awk '{print $2}'`
do
echo Deleting $printer
lpadmin -x $printer
done
exit 0

There is an extra space in line 3.
# 6  
Old 01-10-2015
Quote:
Originally Posted by Lord Lien
Thank you for that Don Smilie You are a star Smilie Worked like a charm.

The reason I used "awk" as I have a script that removes printers (code below) & that worked fine. So I was kinda of scratching my head so to speak.

Code:
#!/bin/sh
# Removes ALL printers on that machine
for printer in `lpstat -p | awk '{print $2}'`
do
echo Deleting $printer
lpadmin -x $printer
done
exit 0

There is an extra space in line 3.
I'm not sure what "There is an extra space in line 3." is referring to.

Note that awk is a fairly expensive program to use this way. Since read is a shell built-in, it is usually faster and uses fewer system resources to do the above with something like the previous example:
Code:
#!/bin/sh
# Removes ALL printers on that machine
lpstat -p | while read junk printer junk
do	echo "Deleting $printer"
	lpadmin -x "$printer"
done
exit 0

Note that I use the name junk for variables used by read for fields that I won't be using later as a convention to make it clear that I don't care what is in those fields. Some people use a variable named ignore, x, or _ for this. The name isn't important; just use something that makes it clear to you (when you look at this code again a year from now) what is being done.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-11-2015
Quote:
Originally Posted by Don Cragun
I'm not sure what "There is an extra space in line 3." is referring to.
On your 1st script it should read on line 3.

Code:
networksetup -listpreferredwirelessnetworks en1 | while read wireless


On the printers script using awk, I must have got lucky. As when I do a

Code:
lpstat -p

I get the following results

Code:
printer Fake_Printer_1 is idle.  enabled since Sat 10 Jan 23:32:19 2015
printer Fake_Printer_2 is idle.  enabled since Sun 11 Jan 06:56:25 2015

& the awk command I was using, only grabs the part I needed after the word printer.

Code:
for printer in `lpstat -p | awk '{print $2}'`

I did read online that awk with the $0 would grab the whole line. I've never really bothered with scripting as I've always had Casper to help with MAC management, now I have some time on my hands I want to have a play with with whats under the bonnet. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Beginners Questions & Answers

UNIX Script to clean files

Hello All, I need a script that would delete files which are more than "X" number of days old, also if there can be a log file of the deleted files for reference. I am from windows background hence finding it difficult. Any help is much appreciated Regards Wert (4 Replies)
Discussion started by: wert468
4 Replies

3. Shell Programming and Scripting

Clean up UNIX mail box using script

Hi, I would like to clean up by unix mail mail box thru some script command.. I do know how to delete from mail box ... e.g. $ mail ? d* ? quit But I need to clean up thru some command which I can use in my script before sending any email.. Thanks in advance! (1 Reply)
Discussion started by: pareshkp
1 Replies

4. Shell Programming and Scripting

Need a shell script to clean data

Hi, Appreciated if anyone can throw some hint I have a file format like this: old(1): PRCNCP 1 old(2): PRSKU ... (6 Replies)
Discussion started by: netbanker
6 Replies

5. Shell Programming and Scripting

Script to FTP,clean up and email

Hi i need a unix script to do the following tasks. My folder structure is /home/MSTR/test and will have the following folder within it Cache Lookup Source Target 1. On the Source & Target folder i have to take a copy of files older than 5 days and move(FTP) it into local machines C:\Backup... (1 Reply)
Discussion started by: Codesearcher
1 Replies

6. Shell Programming and Scripting

noob question - is awk the tool to clean dirty text files?

Hi, nevermind. I think I've found the answer. It appears I was looking for index, match, sub, and gsub. I want to write a shell script that will clean the html out of a bunch of files and format the data for import into excel. Awk seems like a powerful tool, but it seems oriented to... (1 Reply)
Discussion started by: yogert909
1 Replies

7. Shell Programming and Scripting

How to clean this script?

Hello guys, this script partially works but it's still pretty ugly and, moreover, if the month is jan/feb/mar... it doesn't work at all. Could anyone say me how to correct, cut and clean a little bit? #!/usr/bin/ksh egrep -v -e "^\s*#" /file/permission | awk '{ print $1 }' | sort | uniq... (3 Replies)
Discussion started by: gogol_bordello
3 Replies

8. OS X (Apple)

Startup script to clean out trash can

I need to know how I would be able to clean out the trash can of a single "dumb" user every time the MAC is turned on. Back ground. OS 10.3x G3 Mac Two users configured... 1) Root or Admin (superuser) 2) student (Simple no access to anything but shared folder for files etc.) The problem... (4 Replies)
Discussion started by: Andrek
4 Replies

9. Shell Programming and Scripting

clean up script

I have a script which would monitor a given directory and delete any files which are older than 10 days. I was going to set the 10 crob jobs to perform this operation for 10 different directories (some are actually sub-directories), but my boss doesn't like that idea, so I need to do that in one... (1 Reply)
Discussion started by: mpang_
1 Replies

10. Shell Programming and Scripting

writing script to clean up a directory

I have to do a directory clean up on several machines. The task is as follows: go to a particular directory (cd /xxx) 1. create a directory ' SCRIPTCLEANUP ' ( i KNOW IT) loop through 2. List the directory 3. if directory and start with 'DQA' leave it, 4. if directory or file move it to... (0 Replies)
Discussion started by: ajaya
0 Replies
Login or Register to Ask a Question