Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, i have 2 files: test0.txt 555-2397 Beckett, Barry 555-5116 Carter, Gertrude 555-7929 Jones, Theresa 555-9871 Orwell, Samuel test2.txt 555-9871 unlisted 555-5116 listed 555-2397 unlisted 555-7929 listed before using the ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    2

    using join + sort

    Hi, i have 2 files:
    test0.txt
    555-2397 Beckett, Barry
    555-5116 Carter, Gertrude
    555-7929 Jones, Theresa
    555-9871 Orwell, Samuel

    test2.txt
    555-9871 unlisted
    555-5116 listed
    555-2397 unlisted
    555-7929 listed

    before using the join command i need to sort the test2.txt content, so:
    Code:
    join test0.txt `sort -d test2.txt`
    it doesn't work

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    `sort -d test2.txt` will get replaced by Bash with the output of the sort command. But join is expecting two filenames.

    Fortunately, join expects that maybe you'll want to pass in output from another program, and provides a facility to do this. We can do what you want by running the command:
    Code:
    sort -d test2.txt | join test0.txt -
    What happens here? First, we run "sort -d test2.txt". We then pipe its output into our join command. Piping means that the output of one command becomes the input of another command.

    The join command joins test0.txt with a special file "-". "-" tells join to read its standard input as the second file, not to use an actual file.

    And this should work! Does it make sense to you?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Apr 2010
    Posts
    2
    yes, it works, thanks for your explanation.
    "-" mean it use its input from a pipe as file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...