Here is the code someone posted days ago, but it didn't work well because it did't omit the newline character when $2 conbines. Can someone help me out?
Basically, this code is trying to remove the redundent line in a two column spreadsheet file based on the field one, but merge field 2 if field one is redundent.
Your code gave the line break in between $2,
H82421 RDH11^M /// DKFZP564M1462
H82435 BAGE4^M /// MLL3
H82527 C20orf17
H82532 -
H82706 ID2
H82801 -
H82812 TNRC6B
H82872 UACA
I could use some text editor to remove line breaks, but thought the complete code could be helpful for other people too. In addition, if the redundent lines are not sorted, what code should be? Thanks so much.
Are you by any chance viewing the output file in DOS/Windows?
"^M" normally appears when you do so..as these OSes do not recognize new lines(UNIX) properly
awk 'BEGIN{x="";i=0} NR == 1 {x=$1;y=$0} NR > 1 && x == $1 {printf("%s /// %s\n",y,$2);i=NR+1} NR == i {x=$1;y=$0} NR > i && x != $1 {printf("%s\n",y);x=$1;y=$0} END {printf("%s\n",y);}'
It probably could be a OS problem. You could check by looking for special charcters by using "cat -vet" on the output file. Regarding your question on what do we do if the input is unsorted wrt to the first column, we sort it first using:
sort -t' ' -k 1 testfile.txt
and pipe the output to the awk statement you used.