我有一个代码在日志文件中寻找特定模式。通常它以关联 ID = 开头(这是我在括号中寻找的内容)。现在我到目前为止编写的代码正在抛出一个语法:
echo "Please enter the UID: ";
read uid
uidAssoc(){
arg1=$1
echo $arg1
for arg1 in $(cat ~/jlog/server.log); do echo $word;done
| sed 's/.AssociationID = \([*])\'
}
uidAssoc $uid
有人可以就如何纠正 sed 命令并让其回显使用该命令找到的结果提出建议吗?
我尝试过使用 grep 来代替:
echo "Please enter the UID: ";
read uid
#Will parse through server log files for associaton
uidAssoc(){
arg1=$1
echo $arg1
for arg1 in $(cat ~/jlog/server.log); do echo $word; done|
grep Association ID ~/jlog/server.log |
grep -E -o "[a-z0-9]" ~/jlog/server.log
}
uidAssoc $uid
exit
以下是我的意见:
Please enter the UID:
1.2.124.113532.80.22187.2757.20140503.123509.1190654364
输出结果如下:
l
e
o
p
i
c
g
e
n
t
e
a
n
r
o
c
e
s
s
e
d
i
n
s
t
a
n
c
e
1
2
1
2
4
1
1
3
5
3
2
8
0
2
2
1
8
7
2
7
答案1
将其插入到您的 sed 段中。您的输出是在每个字符末尾发送换行符。
替换 sed (在 | 之后):
sed':a; N; $!ba; s / \ n / / g''s /.AssociationID =([*])\'
这将循环读取整个文件,然后用空格替换换行符。
更新:解释。
create a label via :a
append the current and next line to the pattern space via N
if we are before the last line, branch to the created label $!ba
($! means not to do it on the last line (as there should be one final newline)).
finally the substitution replaces every newline with a space on the pattern space
(which is the whole file).
您可能需要根据最终输出进行调整,但这应该可以解决 sed 每行一个字符的问题。
答案2
我在家里弄清楚了大部分内容。我有下面的代码片段,我只需要对其进行调整就可以弄清楚其余部分:
echo "Please enter the UID: ";
read uid
#Will parse through server log files for associaton
uidAssoc(){
arg1=$1
echo $arg1
for arg1 in $(cat ~/jlog/server.log); do echo $word;done |
awk 'match($i, /Association ID = /){ print substr($i, RSTART,RLENGTH)
}
' ~/jlog/server.log
}
uidAssoc $uid
exit