I have a question that I would like to make simpler, how do I split the second field of a file (space delimited) by an underscore and add the second part as a new field to the data (output as tab)? e.g. file.txt2 1_1232 2_345out.tab:2 1_123 1232 2_345 345I have it working with this but is there a simpler way? This seems way too convoluted...paste -d' ' file.txt <(cat file.txt | cut -d'_' -f 2- ) | tr [:blank:] \\t > out.temp && mv out.temp out.tab
split a file by a character and add it to the data
text processing;cut
All your transformations are shuffling data inside the same line of the file, so it would be a lot simpler to use a tool that manipulates data line by line, such as sed or awk.sed -e 's/^[^ \t]*[ \t][^ \t]*_\([^_ \t]*\)/&\1\t/' -e 'y/ /\t/' <<<'2 1_123'Explanation: use a regular expression to match the first field (up to the first space or tab), the second field up to its last underscore, and the second field after the last underscore (which is placed in a \(\) group so that it can be used in the replacement text). Keep the same text (& in the replacement) followed by the content of the matched group (\1). Any field after the second is left unchanged. Finally, replace all spaces by tabs.If your sed doesn't support \t to mean a tab, use a literal tab character instead.