Sort rows names and print the corresponding values below them.
text file contains
x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc
output required
x 
asd
asd
asd
asd
asd
asd
asd
b
axy
axc
Sort rows names and print the corresponding values below them.
text file contains
x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc
output required
x 
asd
asd
asd
asd
asd
asd
asd
b
axy
axc
 
    
     
    
    Use csv reader
with open("file.txt", "r") as f:
    oldx, newx = '', ''
    for row in csv.reader(f, delimiter=' '):
        newx = row[0]
        if newx != oldx:
            print(newx)
            oldx = newx
        print(row[-1])
 
    
    Here's a way to do it:
with open('infile.txt', 'r', encoding="utf-8") as f:
    rows = [row.split() for row in f.readlines()]
    print('rows:'); print(rows)
    column = [rows[0][0]] + [row[-1] for row in rows]
    print('column:'); print(column)
    with open('outfile.txt', 'w', encoding="utf-8") as g:
        for row in column:
            g.write(f'{row}\n')
# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
    print('contents of output file:')
    [print(row.strip('\n')) for row in f.readlines()]
Explanation:
rowscolumn whose first element is the top left element of rows, with the remaining elements coming from the right column of rowscolumn to the output file one at a time, each on its own line using \n as the line terminator\n since print() appends its own \n)Output:
rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd']]
column:
['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd']
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd
UPDATE: Addressing OP's modified question.
with open('infile.txt', 'r', encoding="utf-8") as f:
    rows = [row.split() for row in f.readlines()]
    print('rows:'); print(rows)
    columns = []
    col = []
    iRow = 0
    while iRow < len(rows):
        if iRow == 0 or rows[iRow - 1][0] != rows[iRow][0]:
            if iRow > 0:
                columns.append(col)
            col = [rows[iRow][0]]
        col.append(rows[iRow][-1])
        iRow += 1
    columns.append(col)
    print('columns:'); print(columns)
    
    #column = [rows[0][0]] + [row[-1] for row in rows]
    #print('column:'); print(column)
    with open('outfile.txt', 'w', encoding="utf-8") as g:
        isFirstCol = True
        for column in columns:
            if isFirstCol:
                isFirstCol = False
            else:
                g.write(f'\n')
            for row in column:
                g.write(f'{row}\n')
# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
    print('contents of output file:')
    [print(row.strip('\n')) for row in f.readlines()]
Output:
rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd'], ['b', '8', 'axy'], ['b', '9', 'axc']]
columns:
[['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd'], ['b', 'axy', 'axc']]
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd
b
axy
axc
