8

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"?

cuonglm
  • 150,973
  • 38
  • 327
  • 406
Just a learner
  • 1,766
  • 4
  • 22
  • 32
  • 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 08 '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 08 '16 at 17:05

4 Answers4

10

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.

cuonglm
  • 150,973
  • 38
  • 327
  • 406
2

You can use this sed command on your processed result

sed -e 'y/\t/\n/' processedresult.txt
rcjohnson
  • 899
  • 6
  • 12
  • 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 08 '16 at 09:46
  • @StéphaneChazelas You could just use a literal tab. – 123 Sep 08 '16 at 09: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 08 '16 at 09:51
  • You can always check the [POSIX specification](http://pubs.opengroup.org/onlinepubs/9699919799/). 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 08 '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 08 '16 at 17:09
  • @alexis, I agree with you. I meant that if one care about writing portable answers, he can always check the POSIX spec. – Stéphane Chazelas Sep 08 '16 at 18:52
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
Nidal
  • 8,856
  • 11
  • 55
  • 74
0

awk version:

awk -v OFS='\n' '{$1=$1; print}' input-file > output-file
jf1
  • 407
  • 2
  • 10