How to add blank line between lines from different ip groups - text

I have a file localhost.txt of the form (ip,info):
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18
192.168.1.20 date 6/2/2012
192.168.1.20 host name riko
192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84
I want output result as:
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18
192.168.1.20 date 6/2/2012
192.168.1.20 host name riko
192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84
i tried this
awk '{if(NR > 1 && $2 != prev_two){printf "\n";} prev_two=$2; print $0}' localhost.txt >> result.txt
and this
awk 'prev != $2 {print ""} {print; prev=$2}' localhost.txt >> result.txt
but dosent work
i have been read this
Add blank line between lines from different groups

This:
$ awk '{if(NR > 1 && $1 != prev_two){printf "\n";} prev_two=$1; print $0}' localhost.txt >> result.txt
$ cat result.txt
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18
192.168.1.20 date 6/2/2012
192.168.1.20 host name riko
192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84
should work. It's the first field you want to group by, not the second (hence the $1).

You can do this with awk
awk '$1!=a && NR>1 {$0=RS$0} {a=$1} 1' file
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18
192.168.1.20 date 6/2/2012
192.168.1.20 host name riko
192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84

This is not an one liner usnig awk, but it works.
#!/bin/bash
prev=""
while read -r line
do
cur=$(echo "$line" | awk '{print $1}')
if [[ "$prev" == "" ]]; then
prev="$cur"
elif [[ "$prev" != "$cur" ]]; then
echo ""
fi
echo "$line"
prev="$cur"
done

Related

Set an awk,sed output to a variable in Bash

UPDATE:
Now I have a simpler file to handle, similar to this:
127.0.0.1 25 127.0.0.1
127.0.0.1 25 127.0.0.1
127.0.0.1 32828 127.0.0.1
127.0.0.1 32830 127.0.0.1
127.0.0.1 32906 127.0.0.1
127.0.0.1 32908 127.0.0.1
127.0.0.1 32984 127.0.0.1
(first column ip_local, second column port_local and last column ip_foreign)
All I have to do is to send the awk output to a variable.
The code I managed to write is this, but I still have problems to process the text file...
#!/bin/sh
for i in `cat DEV0_IPsListSep.txt`;
do
ip_local=$(awk '{print $1}' $i);
port_local=$(awk '{print $2}' $i);
ip_foreign=$(awk '{print $3}' $i);
if [$port_local < 3200] || [$ip_foreign != 127.0.0.1] || [$ip_foreign !=
192.168.1.1]
then
echo $ip_foreign >> IPFinalList.txt;
fi
done
I have a bash script that processes a file with two columns. The first column is the local ip addresses, the other is the one of the foreign addresses.
Example:
127.0.0.1:25 127.0.0.1:33862
127.0.0.1:25 127.0.0.1:36498
127.0.0.1:25 127.0.0.1:37338
127.0.0.1:25 127.0.0.1:37410
127.0.0.1:25 127.0.0.1:38320
127.0.0.1:25 127.0.0.1:39428
127.0.0.1:25 127.0.0.1:39514
127.0.0.1:25 127.0.0.1:39768
127.0.0.1:25 127.0.0.1:39846
127.0.0.1:25 127.0.0.1:40376
I would like the script to assign the outputs of sed and awk commands to my custom variables, to separate IPs and ports.
I tried to create a script but still receive syntax errors...
#!/bin/sh
for i in `IPslist.txt`;
ipLocal=$(awk '{print $1}' | sed -e 's/:.*//' $i)
portLocal=$(awk '{print $1}' | sed -e 's/.*://' $i)
ipForeign=$(awk '{print $2}' | sed -e 's/:.*//' $i)
portForeign=$(awk '{print $2}' | sed -e 's/.*://' $i)
if [ $portLocal < 3200 ] || [ $ipForeign != 127.0.0.1 ] || [
$ipForeign != 192.168.1.1 ]
then
echo $ipForeign >> IPFinalList.txt
fi
With single awk process (optimized solution):
awk -F':|[[:space:]]+' '$2<3200 || $3!= 127.0.0.1 || $3!= 192.168.1.1' IPslist.txt > IPFinalList.txt

script for checking IP's hostname from file

I used for to file one contain the IP with hostname like the /ec/hosts
and the other contain the IP and counter try to print the ip if hostname not found and if is found print the hostname.
Script:
for i in `cat ip | awk '{print $2}'` ;do
var=`grep "$i" Server_ip` |
awk ' {if($var == "") print $i else print $1}';
done
File 1
localhost 127.0.0.1
test 10.0.0.1
test1 10.0.0.2
File 2
3 127.0.0.1
2 10.0.0.1
1 10.0.0.2
4 10.0.0.3
5 10.0.0.4
Desired output
localhost
test
test1
10.0.0.3
10.0.0.4
You can try this;
#!/bin/bash
for i in `cat ip | awk '{print $2}'` ;do
var=`awk -v ip=${i} '$2 == ip {print $1}' Server_ip`
if [ -z "$var" ]; then
echo $i
else
echo "$var"
fi
done
Eg;
user#host:/tmp/test$ cat Server_ip
localhost 127.0.0.1
test 10.0.0.1
test1 10.0.0.2
user#host:/tmp/test$ cat ip
3 127.0.0.1
2 10.0.0.1
1 10.0.0.2
4 10.0.0.3
5 10.0.0.4
user#host:/tmp/test$ ./test.sh
localhost
test
test1
10.0.0.3
10.0.0.4

Shell Script: Get eth name from Subnet mask

My machine has multiple ethernet device such as eth0, eth1, etc. One of those IP will have an IP in the range 192.168.x.x. How can I fetch the device name using shell script? (Preferably using ip commands rather than ifconfig)
Like
eth0 192.168.2.3
or
eth3 192.168.5.6
you can try this;
ip -o -4 a | awk '$2 ~ "eth" { gsub(/\/.*/, "", $4); print $2" "$4}'
to all interfaces;
ip -o -4 a | awk ' { gsub(/\/.*/, "", $4); print $2" "$4}'
eg:
user#host:/tmp/$ ip -o -4 a | awk ' { gsub(/\/.*/, "", $4); print $2" "$4}'
lo 127.0.0.1
eth0 x.x.x.x
docker0 x.x.x.x
user#host:/tmp/$ ip -o -4 a | awk '$2 ~ "eth" { gsub(/\/.*/, "", $4); print $2" "$4}'
eth0 x.x.x.x
man ip said :
-o, -oneline
output each record on a single line, replacing line feeds with the '\' character. This is convenient when you want to
count records with wc(1) or to grep(1) the output.
-4 a : Only show TCP/IP IPv4

How to execute more than one command on one line?

I am new to Linux, and I am having an issue of not being able to fully execute these lines of code. I have researched for hours and have also spent plenty of time manipulating these myself and still cannot create something that looks how it should be. Thank you all ahead of time.
How it is supposed to look:
$. network.sh
Today is: Day, Month, Day (numerical), year
The hostname for localhost.localdomain is: 192.168.xxx.xxx
Here is how it currently looks.
. network.sh
Today is: Sunday, October 11, 2015
The hostname for localhost.localdomain
is: [kris#localhost Desktop]$
is is on the next line. This completely disregards my last line of command and does not display it on the same line as "the hostname for localhost.localdomain".
Current file...
#!/bin/bash
# Script to print the current date
echo -n "Today is: "
d=$(date +%A," "%B" "%d," "%Y)
echo "$d"
# Show IP in color
echo -n "The hostname for " ; hostname ; echo -n "is:" ; echo -n ip addr list eth1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1
Just interpolate the sub processes outputs:
echo "The hostname for $(hostname) is: $(ip addr list eth1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1)"
The nested " are fine (the $(subshell) parsing takes precedence)
You can use:
echo "The hostname for $(hostname) is: $(ip addr list eth1 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1)"
Or, I suggest:
ip=$(ip addr list eth1 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1)
echo "The hostname for $(hostname) is: $ip"

bash IP whois lookup script

I have a decent admin script working for checking what IPs are logging on too a web app but I need to make it more fancy with a whois at the start and then I thought a geoip.
At the moment I've hashed out the whois part of it - my problem is that because there are multiple IPs - the whois doesn't know what to do with them
Any ideas on this would be great? and also ideas on geoips would be lovely!
Cheers
#!/bin/bash
#Setting date and time (y and z aren't being used at the moment)
x="$(date +'%d/%b/%Y')"
y="$(date +'%T')"
z="$(date +'%T' | awk 'BEGIN { FS =":"} ; {print $1}')"
#Human readable for email title
emaildate=$(date +"%d%b%Y--Hour--%H")
#Setting date and time for grep and filename
beta="$(date +'%d/%b/%Y:%H')"
sigma="$(date +'%d-%b-%Y-%H')"
#Current SSL Access logs
log='/var/log/apache2/ssl_access.log'
#Set saved log location
newlogs=/home/user/Scripts/logs
grep user#user.com $log | grep $beta | awk 'BEGIN { FS = " " } ; { print $1 }' | sort -u >> $newlogs/adminusage"$sigma".txt
#Preform whois
#whoip=`grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $newlogs/adminusage"$sigma".txt | sort | uniq >> $testing`
#echo $whoip
#testing="/home/user/Scripts/testing.txt"
#IPlookup="/home/user/Scripts/iptest.txt"
#Preform Usage for the current hour
if
grep -v 1.1.1.1 $newlogs/adminusage"$sigma".txt
then
#whois $testing >> $IPlookup
mail -s "Admin Usage for $emaildate" email.com < $newlogs/adminusage"$sigma".txt
else
echo
fi
Just use a loop and invoke whois once per iteration
Presuming that your grep returns a newline-delimited list of IP addresses, you could do something like this:
grep ... | sort | uniq | while IFS= read -r ip ; do
whois "$ip" >> whatever
done
If you have multiple IPs, simply loop over them and run whois on each:
for address is $whoip ; do
whois $address
done

Resources