Linux 从特定行到特定行搜索输出和cat,然后再次搜索

Linux 从特定行到特定行搜索输出和cat,然后再次搜索,linux,bash,sed,awk,grep,Linux,Bash,Sed,Awk,Grep,我写了这样的东西: OUT=$( nmap -p "$port" --script=http-headers.nse "$ip" 例如,输出为: |http-headers: | Server: Apache | Vary: Accept-Encoding | Content-Type: text/html; charset=utf-8 | Date: Thu, 06 Feb 2014 07:31:33 GMT | Age: 25 | Connection: clos

我写了这样的东西:

OUT=$( nmap -p "$port" --script=http-headers.nse "$ip"
例如,输出为:

|http-headers: 
|   Server: Apache
|   Vary: Accept-Encoding
|   Content-Type: text/html; charset=utf-8
|   Date: Thu, 06 Feb 2014 07:31:33 GMT
|   Age: 25
|   Connection: close
但我的输出长度是可变的。 因此,我希望在输出中的行之间进行搜索(最好使用
sed
awk
) 并检查一个条件。例如,如果它看到从第3行到第8行的
Apache
,则echo
right

编辑:

我的剧本:

#!/bin/bash

echo "Reading data - headers - both"

if [ $# -ne 3 ]; then
    echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
    exit 1
fi

if [ $3 == h ]; then
    while read -r -u3 port; do
    while read -r -u4 ip; do


        echo -n "$ip $port: "
        OUT=$( nmap -p "$port" --script=http-headers.nse "$ip" |
        tail -n 13 | 
        awk -F: '{print $2; exit}')   in other lines
        [[ $OUT == *Apache* ]] && echo right || echo wrong 
    done 4< "$2"
    done 3< "$1"

elif [ $3 == d ]; then
    echo data
elif [ $3 == b ]; then 
    echo both
fi
您可以使用此awk:

awk 'NR>=3 && NR<=8 && /Apache/{print "right"}' 
awk'NR>=3&&NR这是修复方法

#!/bin/bash

echo "Reading data - headers - both"

if [ $# -ne 3 ]; then
    echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
    exit 1
fi

headers () {
    join -a1 -a2 -j 2 $2 $1 |while read ip port
     do
        echo -n "$ip $port:"
        OUT=$(nmap -p "$port" --script=http-headers.nse "$ip" | tac | awk -F: 'NR<=13&&/Apache/{print $2; exit}')   
        if [[ "$OUT" == *Apache* ]]; then
            echo $ip $port >> right.txt
        else
            echo $ip $port >> wrong.txt
        fi
     done
} 

case $3 in 
  "h") headers ;;
  "d") echo data;;
  "b") echo both;;
  "*") echo "wrong input"
       exit;;
esac
#/bin/bash
echo“读取数据-标题-两者”
如果[$#-ne 3];然后
echo“用法:./nmap”
出口1
fi
标题(){
读取ip端口时加入-a1-a2-J2$2$1 |
做
echo-n“$ip$端口:”

OUT=$(nmap-p“$port”--script=http-headers.nse“$ip”| tac | awk-F:“我能把复杂的命令放在{}中而不是打印“right”?@anubhava例如,如果我的条件是正确的,我如何在文件中回显结果,比如:这是awk,工作方式类似于编程语言。你可以在
{和}中添加你喜欢的复杂程度
。将输出重定向到文件:
awk'NR>=3&&NR output.txt}“
但我没有理解你所说的
我的情况是正确的,比如:
最好编辑你的问题并澄清一下。很抱歉,它不起作用…所以我把我的解决方案放在回答部分,但仍然有一些问题…谢谢。当运行脚本时。/nmap2.sh port ip h,它会显示这样的内容:./nmap2.sh port ip h读取数据-头-两个连接:丢失操作数请尝试“join--help”以获取更多信息。您是否将名为port和ip的文件放在运行脚本的同一文件夹中?是否可以直接运行命令
join-a1-a2-j2 ip port
,是否可以获得任何输出,是否出现相同的错误?@BMW I运行命令
join-a1-a2-j2 ip port | column-t
,并输出好的,没有错
if [ $3 == 'h' ]; then
    while read -r -u3 port; do
      while read -r -u4 ip; do    
         out="$ip $port"
         echo -n "$out: "
         nmap -p "$port" --script=http-headers.nse "$ip" |
         tail -n 13 | grep -q "Apaches" && echo "$out">>right.txt || echo "$out">>wrong.txt
      done 4< "$2"
    done 3< "$1"

elif [ $3 == d ]; then
    echo data
elif [ $3 == b ]; then 
    echo both
fi
#!/bin/bash

echo "Reading data - headers - both"

if [ $# -ne 3 ]; then
    echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
    exit 1
fi

headers () {
    join -a1 -a2 -j 2 $2 $1 |while read ip port
     do
        echo -n "$ip $port:"
        OUT=$(nmap -p "$port" --script=http-headers.nse "$ip" | tac | awk -F: 'NR<=13&&/Apache/{print $2; exit}')   
        if [[ "$OUT" == *Apache* ]]; then
            echo $ip $port >> right.txt
        else
            echo $ip $port >> wrong.txt
        fi
     done
} 

case $3 in 
  "h") headers ;;
  "d") echo data;;
  "b") echo both;;
  "*") echo "wrong input"
       exit;;
esac