sort lines inside blocks in a file
I need to sort lines inside blocks in a file, not between blocks, and not sort blocks.
"Block": two new-line characters together sample file: xyz def opq ghi abc rstuu 442 fde 932 desired output: def opq xyz 442 abc ghi rstuu 932 fde Which language or command would you use ? (sort, msort, sed, awk, perl, pyhton, bash-script, etc) Which is the better for this ? Could anybody help me to write a script to do this ? Leandro (Argentina) |
Hey.... I dont know the better way to do it.. but here is my ugly solution....
[root@localhost ~]# cat /tmp/test xyz def opq ghi abc rstuu 442 fde 932 [root@localhost ~]# cat /tmp/test | awk 'BEGIN { id=1111; }{if ( NF == 1 ){print id$NF;}else{id++;print id;id++;}}' | sort | sed 's/^....//' def opq xyz 442 abc ghi rstuu 932 fde |
sort lines inside blocks
Thanks your solution.
But your solution only works if each line not contain any space character. In this case, your solution deletes the line. I've posted this file only as sample file. The lines can contain any character (can begin with space for example, can contain a space in any position, etc) This is the rule: line: between only one new line character block: between two or more new line characters (collection of lines) I hope you can help me. Thanks in advance. Leandro. Quote:
|
Here is an awk code, which stores in the indexes of an array the lines in a block. Every time it encounters an empty line, the indexes of the array are sorted and printed out. The END statement ensures the last block is printed even if there is no empty line at the end of the file:
Code:
!/^$/{ |
Another clue: if in the same block there are duplicated lines, the code above prints out just one of them. To be sure all of them are printed out, just count their occurrences:
Code:
!/^$/{ |
Thank you!
I have just only one question now. Does this script still work if there is no empty line at the beginning of the file ? How I have to execute your script ? awk -F your_script file_to_process Regards. Leandro. Quote:
|
Quote:
Quote:
|
Thank you very much!
It works ! I ran: awk -f script file Thank you again! Best Regards. Leandro. Quote:
|
You're welcome!
Bests :) |
Thank you very much!
It works ! I ran: awk -f script file Thank you again! Best Regards. Leandro. |
All times are GMT -5. The time now is 09:23 PM. |