0

I work with bash script. I have lines

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

I want to get "22,80" from this lines.Can anybodey help me?

1

2 Answers 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
Sign up to request clarification or add additional context in comments.

3 Comments

perl -F: -lane 'print $F[1]' file ... +1 ;)
Jaypal and his new look to the rescue :) Thanks for improving my answer!
@fedorqui @jaypal : dang perl is so verbose compared to awk -F: '$_=$2'
0
awk 'BEGIN{FS=":"} {print $2}' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.