showkey
showkey

Reputation: 368

How to run awk in vim to get the longest line and its order number?

We can get the length of the longest line and order number with command:

 awk '{ print length(), NR, $0 | "sort -rn" }' /tmp/test.txt |head -n 1

Now ,to open /tmp/test.txt with command vim.

:!awk '{ print length(), NR, $0 | "sort -rn" }' % |head -n 1

Press ENTER or type command to continue
head: cannot open ‘n’ for reading: No such file or directory
head: cannot open ‘1’ for reading: No such file or directory
sort: fflush failed: standard output: Broken pipe
sort: write error

How to fix it?

Upvotes: 0

Views: 483

Answers (1)

Michael Vehrs
Michael Vehrs

Reputation: 3363

Your command is quite inefficient. If you are using awk anyway, you should also let it calculate the length of the longest line:

 awk '{ l = length($0); a = (a > l) ? a : l } END {print a}' test.txt

Upvotes: 1

Related Questions