Question

Basically after I sort I want my columns to be separated by tabs. right now it is separated by two spaces. The man pages did not have anything related to output formatting (at least I didn't notice it).

If its not possible, I guess I have to use awk to sort and print. Any better alternative?

EDIT: To clarify the question, the location of the double spaces is not consistent. I actually have data like this:

<date>\t<user>\t<message>.

I sort by date by year, month, day and time which looks like

Wed Jan 11 23:44:30 CST 2012

and then have the output of the sorted data like the original file that is

<date>\t<user>\t<message>.

EDIT 2: Seems like my testing for tab was wrong. I was copy pasting raw line from bash to my Windows box. That's why it didn't recognize as a tab instead it showed spaces. I downloaded whole file to windows and now I can see that the fields are tab separated.

Also, I figured out that separation of fields (\t \n , : ;, etc) is same in the new file after sorting. That means, in the original file if I have tab separated field, my sorted file is also going to be tab separated.

One last thing, the "correct" answer was not exactly the correct solution to the problem. I don't know if I can comment on my own thread and mark it as correct. If it is OK to do that, please let me know.

Thanks for the comments guys. Really appreciate your help!

Was it helpful?

Solution

You can use sed:

 sort data.txt  | sed 's/  /\t/g'
                         ^^
                         ||
                      2 blank spaces

This will take the output of your sort operation and substitute a single tab for 2 consecutive blanks.

OTHER TIPS

Pipe your output to column:

sort <whatever> | column -t -s\t

From what I understood the file is already sorted and what you want is to replace the two separating spaces by a TAB character, in that case, use the following:

sed 's/ /\t/g' < sorted_file > new_formatted_file

(Be careful to copy/paste correctly the two spaces in the regular expression)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top