0

我使用 bash 脚本。我有台词

3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80

我想从这行得到“22,80”。有人能帮我吗?

4

2 回答 2

1

As the delimiter before 20 and 80 is :, you can mainly do it with cut:

$ cut -d: -f2 file
22
80

With bash:

$ while read line
do
  echo ${line#*:}
done < file
22
80

Even with awk:

$ awk -F: '{print $2}' file
22
80

And to complete it, with sed:

$ sed  's/.*://' file
22
80
于 2013-06-26T11:41:42.000 回答
0
awk 'BEGIN{FS=":"} {print $2}' file
于 2013-06-26T11:42:39.470 回答