#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Cool How to paste two words in a file without space


    Hi Experts:

    I need your assistance in using paste to join two words together. Here I have two files:
    file1: Word
    file2: /strings together

    Using paste command in unix:
    paste file1 file2 > file3
    Output from file3: Word /strings together

    Note: There are spaces between Word and /strings

    I would like them to be like this
    Word/strings together

    Any assistance in how to get this to work in unix is greatly appreciated.

    Thanks,

    Frywonton
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,394
    Rep Power
    1688
    That seems to be how paste works, but you might be able to get it to drop the tab character it uses (by default) as the replacement to file1's newlines by usind -d '' to have the delimiter be 'blank'. I've not tested that so cannot be sure it will work.
    Failing that an alternate method could be used - either continue to use paste and use a 'strange' delimiter and then use sed or tr to edit out that character (so choose one you'd never see), or use a different method of concatentation entirely. Most methods would require you to deal with the (potential) newline at the end of file1 at some stage of the process.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480

    Works


    This is longer than paste but works in my tests:

    Code:
    gawk 'FILENAME==ARGV[1]{a[i++]=$0}FILENAME==ARGV[2]{printf"%s%s",a[j++],$0;print""}' firstFile secondFile
    (I was too lazy to figure out how many back slashes are needed to stick a \n into the string on the command line. That's why I instead used a separate print"" .)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Thumbs up


    Thanks Simon and b49P23TIvg for your prompt reply.
    Simon, I have been pulling my hair trying to figure out to use the type to joint without sucess. I am a newbie but willing to learn.

    Thanks b49P23TIvg! Your gwak scripts works. I will try and implement it to my other program. Greatly appprecaited your alternative solution. Thanks to both of you for your reply.

    Originally Posted by b49P23TIvg
    This is longer than paste but works in my tests:

    Code:
    gawk 'FILENAME==ARGV[1]{a[i++]=$0}FILENAME==ARGV[2]{printf"%s%s",a[j++],$0;print""}' firstFile secondFile
    (I was too lazy to figure out how many back slashes are needed to stick a \n into the string on the command line. That's why I instead used a separate print"" .)

IMN logo majestic logo threadwatch logo seochat tools logo