Markus
Markus

Reputation: 121

Sorting terms on one line in tab delimted text file

My file contains a number of lines all of which contain a tab delimited sequence of terms. I would like to sort these tab delimited terms alphabetically within each line using the 'sort' command but seem to be unable to do it.

Thanks for your help

Markus

file format

Upvotes: 0

Views: 25

Answers (2)

JuniorCompressor
JuniorCompressor

Reputation: 20025

You could use awk to sort the fields of each row:

awk '{split($0,a);asort(a);for(i=1;i<=NF;i++)$i=a[i];print}' a.txt 

Upvotes: 1

backtrack
backtrack

Reputation: 8154

per_row = []

infile = open('Myfile.txt', 'r')

for line in infile:
    per_row.append(line.split('\t'))


sorted = [x for x in sorted(infile)]

print (sorted)

above is a python code to sort the file

Upvotes: 0

Related Questions