6

The paste command can merge multiple lines into one. For example, if I have a file test.txt and it contains:

original text

a
aa
aaa
b
bb
bbb
c
cc
ccc

I can use the command paste -s -d '\t\t\n' test.txt to get:

processed result

a   aa  aaa
b   bb  bbb
c   cc  ccc

I want to know if there is a reverse command that can do the reverse of paste. For example, If I have "processed result", how can I get "original text"?

  • Something like: python -c 'import sys;for line in sys.stdin:print(*line.split(), sep="\n")' ? If you want to split a line only on tabs and not on any whitespace sequence replace .split() with .split("\t"). – Bakuriu Sep 8 '16 at 10:51
  • The reverse of paste is cut, but only when paste is used to merge multiple files (so that each column comes from a different file). For the line-merging use that your question is about, cut cannot reverse it. – alexis Sep 8 '16 at 17:05
8

There's no standard command.

You must do it your self, and depends on your input.

In this case:

tr '\t' '\n' <pasted_file.txt

will give you the original one.

Note that it assumes \t doesn't appear in your original file.

2

You can use this sed command on your processed result

sed -e 'y/\t/\n/' processedresult.txt
  • That won't work with most sed implementations. POSIXly, the behaviour is undefined if the backslash is followed by anything but /, backslash or n. tr '\t' '\n' would be more to the point and more portable. – Stéphane Chazelas Sep 8 '16 at 9:46
  • @StéphaneChazelas You could just use a literal tab. – 123 Sep 8 '16 at 9:50
  • I have only a single system on which to test answers I suggest. I try to ensure all answers I provide work on "my" implementation (Debian i686). While it may not be as portable as tr, my answer does work on my system. Thank you for your comments and suggestion . – rcjohnson Sep 8 '16 at 9:51
  • You can always check the POSIX specification. If it's standard, it should be fairly portable. Debian is a GNU system, GNU utilities have a lot of extensions over the standard so something working on Debian is not always a good indication of whether it's going to work on other Unix/Linux systems. – Stéphane Chazelas Sep 8 '16 at 10:38
  • @Stéphane, I think it's a little excessive to require people to consult the POSIX specification for every answer-- it will discourage answers rather than make them better. But if you know you're in this situation, rcjohnson, it won't hurt to add a disclaimer like "Works with GNU sed, at least; YMMV". Then the reader is warned (and maybe others will confirm it works). – alexis Sep 8 '16 at 17:09
0

You can replace character by sed or character by tr:

sed -e 's/< old string >/< new string >/g' input-file > output-file

s for replace, and g for replacing each occurrence of the < old string > in one line. Without g, replacement will be occurred only once per line.

tr '< old char >' '< new char >' < input-file > output-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.