0

Here is my code

#!bin/bash
IFS=$'\r\n'
GLOBIGNORE='*'
command eval
'array=($(<'$1'))'

sorted=($(sort <<<"${array[*]}"))
for ((i = -1; i <= ${array[-25]}; i--)); do
echo "${array[i]}" | awk -F "/| " '{print $2}'
done

I keep getting an error that says "line 5: array=($(<)): command not found" This is my problem.

As a whole my code should read in a file as a command line argument, sort the elements, then print out column 2 of the last 25 lines. I haven't been able to test this far so if there's a problem there too any help would be appreciated.

This is some of what the file contains:

290729 123456
  79076 12345
  76789 123456789
  59462 password
  49952 iloveyou
  33291 princess
  21725 1234567
  20901 rockyou
  20553 12345678
  16648 abc123
  16227 nicole
  15308 daniel
  15163 babygirl
  14726 monkey
  14331 lovely
  14103 jessica
  13984 654321
  13981 michael
  13488 ashley
  13456 qwerty
  13272 111111
  13134 iloveu
  13028 000000
  12714 michelle
  11761 tigger
  11489 sunshine
  11289 chocolate
  11112 password1
  10836 soccer
  10755 anthony
  10731 friends
  10560 butterfly
  10547 purple
  10508 angel
  10167 jordan
   9764 liverpool
   9708 justin
   9704 loveme
   9610 fuckyou
   9516 123123
   9462 football
   9310 secret
   9153 andrea
   9053 carlos
   8976 jennifer
   8960 joshua
   8756 bubbles
   8676 1234567890
   8667 superman
   8631 hannah
   8537 amanda
   8499 loveyou
   8462 pretty
   8404 basketball
   8360 andrew
   8310 angels
   8285 tweety
   8269 flower
   8025 playboy
   7901 hello
   7866 elizabeth
   7792 hottie
   7766 tinkerbell
   7735 charlie
   7717 samantha
   7654 barbie
   7645 chelsea
   7564 lovers
   7536 teamo
   7518 jasmine
   7500 brandon
   7419 666666
   7333 shadow
   7301 melissa
   7241 eminem
   7222 matthew
3
  • command eval? Why? You'll get much better behavior with readarray -t array <"$1" (of course, $1 needs to actually expand to something, meaning you need to pass your script an argument with the name of a file). Commented Dec 7, 2018 at 22:41
  • ...similarly, readarray -t sorted < <(printf '%s\n' "${array[@]}") is a less-awful way to sort your content. Commented Dec 7, 2018 at 22:45
  • ...to explain the reflexive "Why?", by the way, see BashFAQ #48, describing why "eval" is generally treated as a synonym for "evil"; it's easy to introduce security faults by using it badly, so generally preferable to just avoid outright. Commented Dec 7, 2018 at 22:46

2 Answers 2

2

In Linux you can simply do a

sort -nbr file_to_sort  | head -n 25 | awk '{print $2}'
Sign up to request clarification or add additional context in comments.

2 Comments

Would I be able to use $1 as the "file_to_sort" because I have to read in as command line argument? ex: ./script.sh file_to_sort
sort -nbr "$1" | awk 'NR==26{exit} {print $2}'
0

read in a file as a command line argument, sort the elements, then print out column 2 of the last 25 lines.

From that discription of the problem, I suggest:

#! /bin/sh
sort -bn $1 | tail -25 | awk  '{print $2}'

As a rule, use the shell to operate on filenames, and never use the shell to operate on data. Utilities like sort and awk are far faster and more powerful than the shell when it comes to processing a 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.