0

I am trying to sort a file alphabetically by the second column of each line, which contain uppercase names. An example file:

1:JOHN:Morgan:90:24 2:MIKE:Smith:95:11 3:JAYSON:Ty:99:9 4:TYLER:Edward:89:5 

This is my bash script:

file="/home/here.txt" while IFS=: read -r f1 f2 f3 f4 f5 do tput cup $f1 0 ;echo "$f2 $f1 $f3 $f4 $f5" done <"$file" 

I came up to here to sort them, but this only switches places of f2 with f1.

0

1 Answer 1

3

You can use this to sort the file by second column of your file alphabetically:

file="/home/here.txt" sort -t":" -k2 $file; # -t is delimeter and -k is column number to sort 

Read about sort in man pages to get more info.

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

1 Comment

thank you so much. ive been looking everywhere for this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.