Find the answer to your Linux question:
Results 1 to 3 of 3
I need to split up lines in multiple files and join them using a simple shell script utilizing the korn shell. Say I have 2 files The first file is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2006
    Posts
    1

    spliting lines of text in multiple files and joing them


    I need to split up lines in multiple files and join them using a simple shell script utilizing the korn shell.

    Say I have 2 files
    The first file is like this:
    Mary Wednesday
    Joe Monday
    Sally Saturday

    The second file is like this:
    Sally 999.999.9990
    Mary 999.999.9998
    Joe 999.999.9993

    So the output would be:
    Joe Monday 999.999.9993
    Mary Wednesday 999.999.9998
    Sally Saturday 999.999.9990

    It would be sorted by the day of the week starting with sunday.

    I am a c programmer so this is something i am not use to. I do not remember linux to well but i would thinki need to use grep maybe.

    Any help would be greatly appreciated.

    Thanks
    Tim

  2. #2
    Just Joined! scottro's Avatar
    Join Date
    Feb 2006
    Posts
    57
    I've done something similar with FreeBSD. In my case, I had two files, num.txt and let.txt. The num.txt was 100 lines of 4 digit numbers and let.txt was 100 lines of 4 character strings.

    The pr utility, with the -m option will merge the two files. Let's say the first line of num.txt was 1234 and the first line of let.txt was abcd. I also use the -t flag with pr to get rid of header and footer lines that it adds. (Again, this is on FreeBSD, so I'm not sure of the flags in Linux.)
    Code:
    pr -m -t num.txt let.txt > string.txt
    Now I have string text, but it has made the two things into columns so that it looks like

    1234 abcd

    So, to get rid of the spaces I used awk. I note in the script I added the comment, "There has to be a better way to do this."

    Code:
    awk '{ print $1$2 }' string.txt > newstring.txt
    It's printing the first two fields, and writing it as I did $1$2 means there will be no space between them.

    It's a bit awkward (GROAN) but it works.

    It seems like what you have to do with the second file, the ones with the numbers is get rid of the name, again, awk comes to mind, maybe first using sort on the two files so the name, which is the first field, will make the two files have the name on the same line.

    So first sort the two files. (I have to run out now, see if this gives you enough to work with, my wife is screaming that it's time to go.)

  3. #3
    Just Joined! scottro's Avatar
    Join Date
    Feb 2006
    Posts
    57
    Ok, I have a few minutes. This is cumbersome but it works.

    file1

    Mary Wednesday
    Joe Monday
    Sally Saturday

    file2

    Sally 999.999.9990
    Mary 999.999.9998
    Joe 999.999.9993

    Code:
    sort file1 > sort1
    sort file2 >sort2
    sort1

    Joe Monday
    Mary Wednesday
    Sally Saturday

    sort2

    Joe 999.999.9993
    Mary 999.999.9998
    Sally 999.999.9990

    Get rid of the names in sort2

    Code:
    awk '{print $2}' sort2 > sort2a
    sort2a

    999.999.9993
    999.999.9998
    999.999.9990

    Now, let's merge them to another file
    Code:
    pr -m -t  sort1 sort2a > sort3
    sort3

    Joe Monday 999.999.9993
    Mary Wednesday 999.999.9998
    Sally Saturday 999.999.9990

    Let's get rid of the white space with awk. Although it shows up here as the way you want it, (I don't know how to make the white space show in a posting here) there were two tab stops between the day, eg Monday and the 999 number.



    In my earlier post, I had done print $1$2--actually, all that's necessary is to leave out commas, doing print $1 $2 would have also put my two columns together with no white space, which, in my case where I wanted 8 character strings, is what I wanted. You want spaces between each field however, so
    Code:
    awk '{ print $1, $2, $3 }' sort3 > final
    final

    Joe Monday 999.999.9993
    Mary Wednesday 999.999.9998
    Sally Saturday 999.999.9990

    This is roundabout, and there's probably an easier way to do it with perl (but I don't know perl well at all) and it's the Unix way, using several small utilities to accomplish something.

Posting Permissions

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