Pasting some text with tabs into a here-document in a PuTTY window

5,296

Solution 1

When you type into a shell, the shell recognizes some characters as commands. For example, the carriage return character (the character sent by the Enter key) causes the shell to execute the command. The tab character causes the shell to perform completion. When you paste something into the PuTTY terminal window, from the shell's point of view, that's the same thing as if you'd typed these characters. So at the point the tab character is pasted, the shell performs completion, it doesn't insert a tab.

The easiest way to copy a file without it being transformed would be to use PuTTY's companion program PSCP or PSFTP to copy the file. This is the simplest way conceptually, but it does have the overhead of running another program, authenticating, choosing a directory, etc.

If you want something inline, you can paste directly into cat, rather than in a here document. Then you'd be pasting into the terminal's line editor, not into the shell's line editor. As the terminal's line editor is very primitive, only a few control characters have a special meaning there, not including tab. Press Ctrl+D at the beginning of a line to terminate the input.

[darkstar /]$ cat >text.txt
Paste
Ctrl+D
[darkstar /]$ 

I you want to transfer arbitrary data over a medium that interprets control characters, you can encode it into a form that uses only “tame” characters. Base64 is one; it doesn't use any control character and ignores whitespace and newlines. GNU coreutils, which is part of the basic installation on Linux and Cygwin, includes a base64 command. On the sender side, run base64 <file-to-decode, e.g.

  • On Windows: run base64 c:/path/to/test.txt from a Cygwin terminal
  • Copy the output.
  • In the shell in the PuTTY window, type `base64 -d >/tmp/test.txt and press Enter.
  • Paste the output from base64.
  • Press Ctrl+D.

Solution 2

If you would like a more simple solution than perl script, you can use sed, anyway that perl script is simple too.

echo "hello    world" | sed 's/\(\t\+\)/\t/g'

Anyway, using redhat 5 I don't see any problem using cat command

rpm -qf $(which cat)
coreutils-5.97-34.el5

Solution 3

I'm not entirely sure what problem you're having - on my RHEL install, cat works if I give it tab stops. However as a more general solution perhaps:

 #!/usr/bin/perl

 use strict;
 use warnings; 

 while ( <> ) { 
     s/\s+/\t/g; 
     print;  
 }

Will take 'input' on either STDIN (catted into) or as filenames (e.g. myscript.pl <filename>) and convert all whitespace to tab stops.

Share:
5,296

Related videos on Youtube

Elijah W. Gagne
Author by

Elijah W. Gagne

Updated on September 18, 2022

Comments

  • Elijah W. Gagne
    Elijah W. Gagne over 1 year

    I have a string with tab delimited data that looks like:

    h1  h2
    a1  b1
    a2  b2
    

    I produced it with Notepad on Windows. I created tab delimited data, ensuring that tabs and not spaces are used.

    enter image description here

    I connect to a Linux server via SSH using PuTTY. I would like to write the file to /tmp/test.txt and preserve the tabs. So I run cat <<EOF >/tmp/test.txt. I copy the text from Notepad and paste it into the putty session. Then I enter EOF.

    enter image description here

    However, that produces a file without tabs having the contents of:

    h1h2
    a1b1
    a2b2
    

    I've found that this works:

    sed 's/\\t/\t/g' > /tmp/test.txt << EOF
    h1\th2
    a1\tb1
    a2\tb2
    EOF
    

    However, it required that I change my input string to use '\t' instead of actual tabs. What is a more elegant/simple solution that allows me to take a string literal as-is from Windows and write it into a file on the remote Linux machine?

    I am SSHed into a Linux server from Windows via putty. The server is:

    • Distribution: Red Hat Enterprise Linux Server release 6.6 (Santiago)
    • Bash version: 4.1.2(1)-release (x86_64-redhat-linux-gnu)
    • cat: coreutils-8.4-37.0.1.el6.x86_64
    • minorcaseDev
      minorcaseDev almost 9 years
      Which distribution, version and bash version do you use?
    • Kondybas
      Kondybas almost 9 years
      I've try cat > file.txt with CTRL-D in the last line in the FreeBSD's csh and everything goes perfectly.
    • Elijah W. Gagne
      Elijah W. Gagne almost 9 years
      @Cyrus, I updated the question with those details.
    • Elijah W. Gagne
      Elijah W. Gagne almost 9 years
      @Kondybas, you're suggestion works and is definitely better than my use of sed. I would be interested if there's an even better answer.
    • minorcaseDev
      minorcaseDev almost 9 years
      With exact this RHEL and bash version I can't reproduce your problem.
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' almost 9 years
      cat just passes tabs through, like any other character. Here documents also pass tabs through. The problem is either that your script doesn't contain what you think it does or that what you're using to look at the output mangles it. If you need help figuring it out, you need to give us your exact script, not a script with potentially different whitespace. Post the output of base64 <yourscript.sh and of base64 /tmp/test.txt.
    • terdon
      terdon almost 9 years
      @Gilles on my system, cat > foo and pasting will preserve tabs while cat<<EOF >foo and pasting will not. I don't think there's any script involved here, the OP is pasting directly into the terminal.
    • terdon
      terdon almost 9 years
      @ElijahW.Gagne just clarify that you're pasting the string. Also, why don't you just use cat > file as already suggested? That's the simplest solution.
    • terdon
      terdon almost 9 years
      Apparently, that's how heredocs work. I haven't found it documented explicitly but heredocs are just a needlessly complex way to do what you're attempting. It had never occurred to me to use them before since cat > file and pasting is so much simpler. That's the standard way it would be done.
    • terdon
      terdon almost 9 years
      @Kondybas it's probably worth adding that as an answer since the OP was unaware of it.
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' almost 9 years
      @terdon If you're doing that, you're pasting into your shell's editor. It presumably does completion when you press Tab, rather than inserting a tab. Elijah, you stated in your question that the input to cat contained tabs. If you copy-pasted into a shell running in a terminal, so that you were not directly passing some known input into cat, you need to mention that, it isn't something we can guess.
    • Elijah W. Gagne
      Elijah W. Gagne almost 9 years
      @Gilles, thanks, I appreciate your edit and agree that the question is now outlined much better. When I originally posted, it did not occur to me that those details would matter.
    • user9303970
      user9303970 over 6 years
      Hello @ElijahW.Gagne I didn't find a way to PM you in stackexchange so I tried to PMd you through Facebook on this, if it's okay, could we please have a few words? I am having a similar issue here: unix.stackexchange.com/questions/424940/…
  • Elijah W. Gagne
    Elijah W. Gagne almost 9 years
    I definitely appreciate this answer, but I'm hopeful there is a simpler solution.