当前位置: 技术问答>linux和unix
shell 编程:如何实现一个一个字符的读文件
来源: 互联网 发布时间:2015-07-03
本文导语: 我需要对一个文件进行一个一个字符的判断,若符合条件,则将该字符记下,并读取下一个字符,直到找到一个特定的字符串(一合法的ip地址),请问高手,shell能实现吗? | 100分阿,好人做到底: #!/b...
我需要对一个文件进行一个一个字符的判断,若符合条件,则将该字符记下,并读取下一个字符,直到找到一个特定的字符串(一合法的ip地址),请问高手,shell能实现吗?
|
100分阿,好人做到底:
#!/bin/env bash
#bashscript file:findipaddrs
if [ "$1" = "" ]
then
echo -e "Input the filename(s):c"
read filename
echo "-*********ip address in the file************-"
grep -oE '(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9
]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' $filename
else
echo "-*********ip address in the file************-"
grep -oE '(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9
]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' $1
fi
#!/bin/env bash
#bashscript file:findipaddrs
if [ "$1" = "" ]
then
echo -e "Input the filename(s):c"
read filename
echo "-*********ip address in the file************-"
grep -oE '(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9
]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' $filename
else
echo "-*********ip address in the file************-"
grep -oE '(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9
]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' $1
fi