![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
help using awk
Hi I have an output of the nmap command scan for port 22 as below
Interesting ports on 172.29.143.221: PORT STATE SERVICE 22/tcp open ssh Interesting ports on 172.29.143.240: PORT STATE SERVICE 22/tcp closed ssh Interesting ports on 172.29.143.243: PORT STATE SERVICE 22/tcp open ssh I would like to print out the ip address with STATE "open" only. Thank. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Please try this,
Code:
sed -nf /tmp/OpenIp.sed /tmp/YourInputFile Code:
/Interesting/{
N
N
/open/{
P
}
}
Nagarajan G Last edited by ennstate; 01-17-2008 at 03:52 AM. Reason: Incorrect filename |
|
#3
|
||||
|
||||
|
Code:
awk '/Interesting/{r[NR+2]=$4}/open/{print r[NR]}' file | tr -d :
|
|
#4
|
||||
|
||||
|
Or:
Code:
awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" filename
|
|
#5
|
|||
|
|||
|
Thank you to radoulov. It works well just one single command.
Is it possible if i can take this output and insert this into an array? If so, would you please advise the code ? |
|
#6
|
||||
|
||||
|
Yes,
but the implementation depends on the shell you're using. Some examples: [bash] Code:
bash 3.2.25(1)$ cat file
Interesting ports on 172.29.143.221:
PORT STATE SERVICE
22/tcp open ssh
Interesting ports on 172.29.143.240:
PORT STATE SERVICE
22/tcp closed ssh
Interesting ports on 172.29.143.243:
PORT STATE SERVICE
22/tcp open ssh
bash 3.2.25(1)$ a=($(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file))
bash 3.2.25(1)$ echo ${a[0]}
172.29.143.221
bash 3.2.25(1)$ echo ${a[1]}
172.29.143.243
Code:
$ # ksh88 doesn't support this syntax
$ a=($(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file))
$ print ${a[0]}
172.29.143.221
$ # or
$ set -A a $(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file)
$ print ${a[1]}
172.29.143.243
Code:
% a=($(awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file))
% print $a[1]
172.29.143.221
% print $a[2]
172.29.143.243
[sh] Code:
$ set -- `awk '/^Interesting/{ip=$4}/open/{print ip}' FS="[ :]" file`
$ echo $1
172.29.143.221
$ echo $2
172.29.143.243
|
||||
| Google The UNIX and Linux Forums |