使用bash脚本从linux中的文本文件导出信息

时间:2014-11-07 15:17:13

标签: bash file edit

我有这个特定文件:

Client 1: MAC 00:03:52:49:99:55
 First      : Fri Nov  7 09:50:11 2014
 Last       : Fri Nov  7 09:51:06 2014
--
 Client 1: MAC 00:03:52:04:88:55
 First      : Fri Nov  7 09:51:44 2014
 Last       : Fri Nov  7 09:51:44 2014
--
 Client 2: MAC 00:03:52:49:99:55
 First      : Fri Nov  7 10:50:10 2014
 Last       : Fri Nov  7 10:50:10 2014
--
 Client 3: MAC 00:03:52:04:66:55
 First      : Fri Nov  7 09:51:30 2014
 Last       : Fri Nov  7 09:51:30 2014
--

从这个包含许多重复项目的文件中创建一个像这样的新文件:

00:03:52:49:99:55
First      : Fri Nov  7 09:50:11 2014
Last       : Fri Nov  7 09:51:06 2014
First      : Fri Nov  7 09:50:11 2014
Last       : Fri Nov  7 09:51:06 2014
00:03:52:04:88:55
First      : Fri Nov  7 09:51:44 2014
Last       : Fri Nov  7 09:51:44 2014
00:03:52:04:66:55
First      : Fri Nov  7 09:51:30 2014
Last       : Fri Nov  7 09:51:30 2014

如何用Bash脚本搜索带有For-Loop的文件?重要的是,循环不会为MAC地址创建多于1个条目。 MAC应该是唯一的。

是的,我这一天都在尝试:/

#!/bin/bash
array=$(cat Kismet-20141107-09-48-19-1.nettxt | grep Client -A 3 | grep -v Manuf)
echo "Array size: ${#array[@]}"
echo "Array items:"
for item in ${array[*]}
    do
    if [ $item -eq 3 ]; then
         echo "$array[$item]"
    fi
done

不,不要求使用bash ..如果你有其他工具我会试试!

1 个答案:

答案 0 :(得分:0)

试试这个:

Shell> cat test1
#!/bin/bash

MACS=(`grep Client file|awk '{print $4}'|sort|uniq|xargs`)

for i in `echo ${MACS[*]}`; do
        echo $i
        grep $i file -A 2 | grep -vE 'MAC|--'
done
Shell> cat file
Client 1: MAC 00:03:52:49:99:55
 First      : Fri Nov  7 09:50:11 2014
 Last       : Fri Nov  7 09:51:06 2014
--
 Client 1: MAC 00:03:52:04:88:55
 First      : Fri Nov  7 09:51:44 2014
 Last       : Fri Nov  7 09:51:44 2014
--
 Client 2: MAC 00:03:52:49:99:55
 First      : Fri Nov  7 10:50:10 2014
 Last       : Fri Nov  7 10:50:10 2014
--
 Client 3: MAC 00:03:52:04:66:55
 First      : Fri Nov  7 09:51:30 2014
 Last       : Fri Nov  7 09:51:30 2014
--

输出:

Shell> ./test1
00:03:52:04:66:55
 First      : Fri Nov  7 09:51:30 2014
 Last       : Fri Nov  7 09:51:30 2014
00:03:52:04:88:55
 First      : Fri Nov  7 09:51:44 2014
 Last       : Fri Nov  7 09:51:44 2014
00:03:52:49:99:55
 First      : Fri Nov  7 09:50:11 2014
 Last       : Fri Nov  7 09:51:06 2014
 First      : Fri Nov  7 10:50:10 2014
 Last       : Fri Nov  7 10:50:10 2014