# Importing, sorting and exporting lists

I have several TSV lists (.txt) of something like 2000*2000 numerics (e.g. 1.8645) to import (symmetric matrices), rearrange and sort them by each row. Finally the data should be exported. The lists are imported by using Import but what I've tried so far to manipulate the lists is not working.

An simplified example for a file to import looks like this. First I have to remove the 1st row and column.

0   1   2   3   4
1   0   2   7   1
2   2   0   8   5
3   7   8   0   9
4   1   5   9   0


Afterwards the data should look like this:

0   2   7   1
2   0   8   5
7   8   0   9
1   5   9   0


Then, the data should by sorted ascending by each row:

0   1   2   7
0   2   5   8
0   7   8   9
0   1   5   9


Finally this list is exported with Export.

This is what I wrote up to now:

SetDirectory[NotebookDirectory[]];
myFileNames = Take[FileNames["*txt"]];
lineStart = 2;
lineEnd = 5;
For[c = 1, c <= Length[myFileNames], c++,
Data = {};
aa = myFileNames[[c]];
myImportData = Import[aa, "TSV"];
Data = myImportData;
(*delete 1st row and column*)
For[cc = 1, cc <= lineEnd - lineStart + 1, cc++,
Delete[Data, {1, cc}];
];
For[cc = 1, cc <= lineEnd - lineStart + 1, cc++,
Delete[Data, {cc, 1}];
];
(*sort the data by each column*)
For[cc = 1, cc <= lineEnd - lineStart , cc++,
Sort[Data {cc, 1}];
];
(*transpose the matrix to achive sorted rows*)
Transpose[Data];
(*export the data*)
Export[ToString[aa] <> "_output.txt", Data, "TSV"];
];


For some reason even Delete is not working. My idea was to sort the columns and transpose the list to achieve the sorted rows.

If you know any good tutorial or something similar I'd really appreciate a hint where to find it. Thank you for your help!

## 1 Answer

Try this:

names = FileNames["*.txt"];
Export[StringInsert[#, "_output", -5],
Sort /@ Import[#, "Table"][[2 ;;, 2 ;;]], "TSV"] & /@ names

• Can you show a simple input/output ? – Sektor Feb 10 '15 at 9:51
• I worked on the example give by the OP and I got the same output. – Algohi Feb 10 '15 at 10:25
• Thanks alot. Even with the real data the code is working.I think I really have to improve my mathematica-skills. Do you have any suggestion how to learn to use mathematica in such way? – Jan Feb 10 '15 at 10:37
• My pleasure to help. thanks for accepting the answer. you can find good information here: mathematica.stackexchange.com/questions/18/… and here:mathematica.stackexchange.com/questions/18393/… – Algohi Feb 10 '15 at 10:40
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.1715162992477417, "perplexity": 1904.6262236266884}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-22/segments/1558232259757.86/warc/CC-MAIN-20190526205447-20190526231447-00484.warc.gz"}