Find the answer to your Linux question:
Results 1 to 9 of 9
file a: ffffff eee ccc r 12 ddd fff file b: k ccc bbbb zzz nnn eeeee aaaaaaaaa 3 how to combine a and b to c shows like: 12 ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    5

    Question how to sort these files?

    file a:
    ffffff eee ccc r 12 ddd fff

    file b:
    k ccc bbbb zzz nnn eeeee aaaaaaaaa 3

    how to combine a and b to c
    shows like:

    12 3 aaaaaaaaa bbbb ccc ddd eee eeeee fff ffffff k nnn r zzz

    Please Help

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So there are a few steps here:

    1) Split the lines in each file to put a single word on each line

    2) Combine the two files

    3) Sort the resulting file

    4) Collapse all of the lines into a single line

    Knowing this, do you know how to do any of these steps?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    Quote Originally Posted by Cabhan View Post
    So there are a few steps here:

    1) Split the lines in each file to put a single word on each line

    2) Combine the two files

    3) Sort the resulting file

    4) Collapse all of the lines into a single line

    Knowing this, do you know how to do any of these steps?
    if you could help, i will appreciate.
    I dont really know the syntax in scripting. I understand the steps

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Do you know anything about programming or scripting? If not, that may be a good place to start. I'm hesitant to just give you a script, because then you don't actually learn anything from this.

    There's documentation available called man pages that can be accessed by "man COMMAND". This gives you a nice overview of various commands.

    For step 1, you may want to look into "cut". Step 2 would be "cat", 3 is "sort", and 4 is actually pretty complicated. For step 4, we could use sed, which is actually a small programming language that lets you manipulate text.

    If you look up the programs for 1 - 3 and give me some idea of how to do them, I'll give you the sed command.

    Out of curiosity, what do you need this for?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    May 2010
    Posts
    6
    jason,

    You can try with:

    Code:
    sed 's/\s/\n/g' filea fileb | sort -d | tr '\n' ' '
    
    Explanation:
    sed 's/\s/\n/g' filea fileb -->Convert spaces (s) to end of line (\n) for both files, the output combines both files content.
    sort -d --> Sort in dictionary order with option "-d"
    tr '\n' ' ' --> Convert again the end of lines (\n) to spaces putting the column in a single line (transpose)
    Hope it helps.

  6. #6
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    Quote Originally Posted by Cabhan View Post
    Do you know anything about programming or scripting? If not, that may be a good place to start. I'm hesitant to just give you a script, because then you don't actually learn anything from this.

    There's documentation available called man pages that can be accessed by "man COMMAND". This gives you a nice overview of various commands.

    For step 1, you may want to look into "cut". Step 2 would be "cat", 3 is "sort", and 4 is actually pretty complicated. For step 4, we could use sed, which is actually a small programming language that lets you manipulate text.

    If you look up the programs for 1 - 3 and give me some idea of how to do them, I'll give you the sed command.

    Out of curiosity, what do you need this for?
    Thanks for your information.
    I know programming...but not scripting.
    Im trying to work on it.

  7. #7
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    BTW, do I have to use For loop or IF statement here??
    I am really confused by those syntax...@.@

  8. #8
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    while read myline
    do
    echo $myline
    done < ~/Desktop/a > ~/Desktop/c
    while read mywords
    do
    echo $mywords
    done < ~/Desktop/b > ~/Desktop/d
    paste d c > ~/Desktop/e
    while read line
    do
    echo $line
    done < ~/Desktop/e > ~/Desktop/f
    awk '{gsub(" ","\n"); print}' f > ~/Desktop/g
    sort ~/Desktop/g > ~/Desktop/h
    rm c
    rm d
    rm e
    rm f
    rm g

    here is my code for the first 3 steps
    figuring out last step

  9. #9
    Just Joined!
    Join Date
    May 2010
    Posts
    6
    jason

    Did the solution I put above work for your problem?

Posting Permissions

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