Advertisement
metalx1000

BASH Shuffle and Match up Lists

Jan 25th, 2023
1,662
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.48 KB | None | 0 0
  1. #print odd numbered lines
  2. sed -n 1~2p names.lst
  3.  
  4. #print even numbered lines
  5. sed -n 2~2p names.lst
  6.  
  7. #randomize name list
  8. random="$(sort -R names.lst)"
  9.  
  10.  
  11. echo $random
  12.  
  13. #set Internal Field Separator to space
  14. IFS=" "
  15. echo $random
  16.  
  17. #put even names and odd names into lists
  18. odds="$(echo $random|sed -n 1~2p)"
  19. evens="$(echo $random|sed -n 2~2p)"
  20.  
  21. #print name list team up
  22. paste <(echo "$odds") <(echo "$evens")
  23.  
  24. #Formatted
  25. paste <(echo "$odds") <(echo "$evens")|column -t
  26.  
Advertisement
Comments
  • Valentin_2023
    1 year
    # Bash 0.72 KB | 0 0
    1. #! /bin/bash
    2. #usage team_up.sh FILE
    3.  
    4. [[ $# -eq  0 ]]  && printf "\nERROR: You must provide a file for the script to work.\n\n" 1>&2 && exit 1
    5.  
    6. readarray -t names < <(sort -R $1) #create an array with each line of the file; -t strips the new line character
    7. max=0 #used for proper formatting later in the script
    8.  
    9. for ((i=0; i<${#names[@]}; i+=2)) #i as in index; this way we get the even line numbers.
    10. do
    11.     if ((${#names[i]} > max)); then
    12.         max=${#names[i]}
    13.     else
    14.         max=$max #get the string lenghth of the longest name. Usage later in printf.
    15.     fi
    16. done
    17.  
    18. for ((i=0; i<${#names[@]}; i+=2)); do
    19.     printf -- "%-${max}s %s\n" "${names[i]}" "${names[i+1]}" #i+1, this way we get the odd line numbers
    20. done
    21.  
    22.  
Add Comment
Please, Sign In to add comment
Advertisement