Register
It is currently Thu Oct 23, 2014 8:25 am

sed/ awk to find lines in a file and re-write them @ the top


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Feb 26, 2014 4:28 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
So, I was curious if there was a way to search a file in sed or awk to find line(s) and re-write them at the beginning of the file.. For example
Code:
# Returned active IPs
192.168.0.1
192.168.0.15
192.168.0.16
122.168.0.250
192.168.0.251


Pretend only .1, .250, and .253 were servers and the rest were workstations. I would like to somehow search the file for server IPs and amend the saved file to add them to the tip of the file.

Is this possible?

Thanks,
Brin


Top
 Profile  
 PostPosted: Thu Feb 27, 2014 6:34 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
sed can hold "non-servers lines" and print them at the end of its process (i.e. after "servers lines" have been printed).

awk could use arrays to kind of sort server and non-servers lines in two arrays, at the end you make it print servers first.


Top
 Profile  
 PostPosted: Thu Feb 27, 2014 9:32 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Watael wrote:
sed can hold "non-servers lines" and print them at the end of its process (i.e. after "servers lines" have been printed).

awk could use arrays to kind of sort server and non-servers lines in two arrays, at the end you make it print servers first.


Here is the relevant code snips of how I am getting the active/ inactive IP's. I didn't post the stuff that makes the report look 'pretty'. I am running on cron
Code:
IFCONFIG=/usr/sbin/ifconfig
NET=`${IFCONFIG} -a | grep -w 'inet' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d . -f 1-3`
TMPDIR=/foo/TMP
RPT=/foo/net_report.txt

  ping -c 1 $IP > /dev/null
  if [ $? -eq 0 ]; then
        HOST_CHK=`nslookup $IP | awk '/name / { print $4 }' | cut -d . -f 1`
        if [ "x$HOST_CHK" != "x" ]; then
          echo "${IP}     ${HOST_CHK}" >> $TMPDIR/up.list
        else
          echo "${IP}       No hostname defined"  >> $TMPDIR/up.list
        fi

  else
        HOST_CHK=`nslookup $IP | awk '/name / { print $4 }' | cut -d . -f 1`
        if [ "x$HOST_CHK" != "x" ]; then
          echo "${IP}       ${HOST_CHK}" >> $TMPDIR/down_unav.list

        else
          echo "${IP}" >> $TMPDIR/down.list
        fi
fi

#Clearing out old lists
echo "" > $TMPDIR/up.list
echo "" > $TMPDIR/down.list
echo "" > $TMPDIR/down_unav.list

#Running the IP search
for IP in ${NET}.{1..254}
do
  net_ping $IP & disown
  sleep .1
done

#Clearing out old report
echo "" > $RPT
cat $TMPDIR/up.list >> $RPT
sleep 1
cat $TMPDIR/down.list >> $RPT
sleep 1
cat $TMPDIR/down_unav.list >> $RPT

exit 0


Is there anything in there that I could add/ amend that I could flag certain IP's to be held or whatnot? I'm still growing in the bash world, so any help is most appreciated.

Thanks!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP