我发出了一个奇怪的行为。 我尝试创建一个脚本,但awk正在删除我的第一个字符,我不知道为什么。 当然这段代码现在没用,但我不能继续这个奇怪的问题......
#!/bin/bash
for entry in TF5*; do
if [[ ! $entry =~ ".desc" ]] ; then
awk '
BEGIN {print "BEGIN" }
{ print $0 " "}
END { print " - DONE -" }
' < $entry
fi
done
输出:
BEGIN
ort Name Status Vlan Duplex Speed Type
i1/0/2 v5.7-TF5-XXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX
i1/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX
i1/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
i1/0/5 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
i1/0/6 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
i1/0/7 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
e1/0/1 *** MOG2SWXXXXXXXX connected trunk full 10G 10GBase-SR
i2/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX
i2/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
i2/0/5 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
i2/0/6 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
i2/0/7 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
e2/0/1 *** MOG2SWXXXXXXXX connected trunk full 10G 10GBase-SR
o1 *** SW1+2-XXXXXXXX connected trunk a-full 10G
o4 *** TF5-NAXXXXXXXX connected trunk a-full a-1000
o6 *** TF5-NAXXXXXXXX connected trunk a-full a-1000
o7 *** TF5-NAXXXXXXXX connected trunk a-full a-1000
- DONE -
例如,如果我按{ print $0 " "}
更改{ print $0 " "}
新输出是:
BEGIN
t Name Status Vlan Duplex Speed Type
/0/2 v5.7-TF5-FXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX
/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX
/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
/0/5 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
/0/6 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
/0/7 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
/0/1 *** MOG2SWXXXXXXXX connected trunk full 10G 10GBase-SR
/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX
/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
/0/5 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
/0/6 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
/0/7 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX
/0/1 *** MOG2SWXXXXXXXX connected trunk full 10G 10GBase-SR
*** SW1+2-XXXXXXXX connected trunk a-full 10G
*** TF5-NAXXXXXXXX connected trunk a-full a-1000
*** TF5-NAXXXXXXXX connected trunk a-full a-1000
*** TF5-NAXXXXXXXX connected trunk a-full a-1000
- DONE -
真实档案:
cat -e TF5_MOG2_SW2-EU-A50R1-DC7-1
^M$
^M$
Port Name Status Vlan Duplex Speed Type^M$
Gi1/0/2 v5.7-TF5-FXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX^M$
Gi1/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX^M$
Gi1/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX^M$
Gi1/0/5 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX^M$
Gi1/0/6 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX^M$
Gi1/0/7 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX^M$
Te1/0/1 *** MOG2SWXXXXXXXX connected trunk full 10G 10GBase-SR^M$
Gi2/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX^M$
Gi2/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX^M$
Gi2/0/5 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX^M$
Gi2/0/6 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX^M$
Gi2/0/7 v8.12-TF5-XXXXXXXX connected trunk a-full a-1000 10/100/1000BaseTX^M$
Te2/0/1 *** MOG2SWXXXXXXXX connected trunk full 10G 10GBase-SR^M$
Po1 *** SW1+2-XXXXXXXX connected trunk a-full 10G ^M$
Po4 *** TF5-NAXXXXXXXX connected trunk a-full a-1000 ^M$
Po6 *** TF5-NAXXXXXXXX connected trunk a-full a-1000 ^M$
Po7 *** TF5-NAXXXXXXXX connected trunk a-full a-1000 ^M$
答案 0 :(得分:0)
awk并没有删除第一个字符 - 嵌入式CR就是问题 - SHELL将其解释为“在这一行上找到第一个位置”的指令......
注意 - 在Linux系统上测试 - 不确定您正在使用哪种操作系统...
## note the ^MS represents a non-printable character; function below is for illustration
function show_test_data { ## which included real'^MS'
cat << EOM
Port Name Status Vlan Duplex Speed Type
Gi1/0/2 v5.7-TF5-FXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX^MS
Gi1/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX^MS
Gi1/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX^MS
EOM
}
##
function show_data {
cat $entry
}
###
function stars {
printf "\n\t## $@ ##\n"
}
###
stars "No change"
show_data | \
awk 'BEGIN {print "BEGIN" } { print $0 " "} END { print " - DONE -" } '
stars " Change CR to Hash "
show_data | tr '\r' '#' | \
awk 'BEGIN {print "BEGIN" } { print $0 " "} END { print " - DONE -" } '
stars " Remove CR"
show_data | tr -d '\r' | \
awk 'BEGIN {print "BEGIN" } { print $0 " "} END { print " - DONE -" } '
stars
## No change ##
BEGIN
ort Name Status Vlan Duplex Speed Type
i1/0/2 v5.7-TF5-FXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX
i1/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX
i1/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
- DONE -
## Hash ##
BEGIN
Port Name Status Vlan Duplex Speed Type#
Gi1/0/2 v5.7-TF5-FXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX#
Gi1/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX#
Gi1/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX#
- DONE -
## Remove CR ##
BEGIN
Port Name Status Vlan Duplex Speed Type
Gi1/0/2 v5.7-TF5-FXXXXXXXX connected 10 a-full a-1000 10/100/1000BaseTX
Gi1/0/3 v5.22-TF5-XXXXXXXX connected 210 a-full a-1000 10/100/1000BaseTX
Gi1/0/4 v6.34-TF5-XXXXXXXX connected trunk full 1000 10/100/1000BaseTX
- DONE -
:)
戴尔