Find the answer to your Linux question:
Results 1 to 6 of 6
I have a file with similar information: (call it course.txt) course-name:email-address course-name:email-address course-name1:email-address coursename-2:email-address course-name2:email-address and wish to get an output as follows: email-address >> course.txt so in pseudo code ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Location
    SC
    Posts
    3

    awk redirect problem


    I have a file with similar information: (call it course.txt)

    course-name:email-address
    course-name:email-address
    course-name1:email-address
    coursename-2:email-address
    course-name2:email-address

    and wish to get an output as follows: email-address >> course.txt

    so in pseudo code I would have:

    awk -F: '{print $2}' >> "{print $1}' course.txt

    however, this doesn't work in real life. How do I get the redirection?

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Can you explain exactly what you're trying to do? I'm not following.

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Location
    SC
    Posts
    3
    The output of a database query gives me a course name, and an email address of a student on each line. There may be 3 students in a particular course name (course number and section are incorporated in the course name) or as many as 180 students. The query will give me information from all courses offered and email addresses of the students in that class.

    What I am trying to do is to get that information broken down to a new file per class with only that class's email addresses in it.

    The example file shows what the colon delimited info I am given to work this magic. I thought that, very simply, since the first argument is the class name, that a redirected output file could be made with the name of ($0) and that the email address ($1), could be appended to that text file.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Ok, I think I got it. So in the situation where source rows are:

    -----------------------
    ENG101:joe@some.com:deb@some.com
    SCI101:kim@some.com
    ALG101:naomi@some.com:shiniqua@some.com:louey@some .com
    -----------------------

    you would wind up with three files.

    1. ENG101.txt, which contains the lines:
    joe@ some.com
    deb@ some.com

    2. SCI101.txt, which contains the line:
    kim@ some.com

    3. ALG101.txt, which contains the lines:
    naomi@ some.com
    shiniqua@ some.com
    louey@ some.com

    Correct? This is your goal?

  6. #5
    Just Joined!
    Join Date
    Sep 2005
    Location
    SC
    Posts
    3
    That is correct EXCEPT that each line only contains:

    course-name:email-address

    but in essence you are correct in my goal. So, using your excellent data, the file would be:

    ENG101:joe@some.com
    ENG101:deb@some.com
    SCI101:kim@some.com
    ALG101:naomi@some.com
    ALG01:shiniqua@some.com
    ALG01:louey@some.com
    ALG02:dewey@some.com

  7. #6
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Ok, no problem. Here is what you can do:
    Code:
    awk -F : '{ print $2 >> $1 }' data-file
    Tested and verified.

Posting Permissions

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