0

I have this bash script:

while IFS='"' read -r a ip c
do
    echo "ip: $ip"
    whois "$ip" | grep netname
done < <(head -10 file.log)

How can I sort the file.log file (e.g. with sort -n -r) before the first ten lines are taken and handed over to the while-loop?

2
  • head -10 file.log | sort -nr Commented Jan 14, 2015 at 11:26
  • sort -nr file.log | head -10 Commented Jan 14, 2015 at 11:26

2 Answers 2

3

If you want to get the top 10 lines after sorting, just sort it before using head:

while IFS='"' read -r a ip c
do
    echo "ip: $ip"
    whois "$ip" | grep netname
done < <(sort file.log | head -10)
         ^^^^^^^^^^^^^^^

Apply the sort flags you require, of course.

Sign up to request clarification or add additional context in comments.

Comments

0

Use this command,

sort -nr file.log | head -10

1 Comment

this will sort the top 10 lines, whereas the OP probably wants to get the top 10 sorted lines

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.