Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Read File Into Variables

PsightoplazmPsightoplazm Member Posts: 332 ✭✭
I have a semi-lunatic professor that can't speak any english and has given us an assignment that requires command knowledge we have not been taught yet. Can anyone steer me towards the right direction for a shell script that needs to read a users E-Mail file and search it for specific text to pull out and load into a variables - So for example - if the file has a bunch of stuff looking like this...


[Some kind of header information here}
Some more information here,
[[header information for the first e-mail in this file]]
[e-mail subject]
the e-mail body
THE LINE I"M TRYING TO GET INFORMATION FROM
some more body information
THIS LINE I WANT TO PULL THE WORD "RED" INTO A VARIABLE
some more body
[end of first e-mail]
[[header for second e-mail]]

... and so-on

The project requires me to pull user information e-mailed to this script and have the script make a new user account based on it.
I am not writing here to get an easy A, I could write this in about 5 different languages, and maybe 100000 different ways, but since this is an introductory class to unix which I know very little about, our teacher's english is so bad that no one can understand him, and there isn't a single thing about this in our text books I was hoping someone out there might be able to help me out. Mostly I need help breaking down text from a file and put key information into variables, the rest I can figure out.

Thanks guys!
-P


><//~Psightoplasm`~

Comments

  • pgvnayak_mlpgvnayak_ml Member Posts: 19
    Psightoplasm,
    Use perl scripting in your program.
    Its not so difficult as you think.
    If you can do that in 5 different languages, you can learn in a day/ in a hour or two even.
    Its all that simple.
    I have to make a few reference before I give you the answer for that.
    And I have no time now. I am sorry for that.
    This is just a hint of how you can do it.
    Well if you think thats too difficult, you can use simple Unix commands to get this job done.
    Learn just the following commands and I think that would be sufficient to get your job done...
    Grep, Cut, Cat, pipe(|), redirection symbol (>), sort...
    Well that should get your job done.
    Get to it.


    : I have a semi-lunatic professor that can't speak any english and has given us an assignment that requires command knowledge we have not been taught yet. Can anyone steer me towards the right direction for a shell script that needs to read a users E-Mail file and search it for specific text to pull out and load into a variables - So for example - if the file has a bunch of stuff looking like this...
    :
    :
    : [Some kind of header information here}
    : Some more information here,
    : [[header information for the first e-mail in this file]]
    : [e-mail subject]
    : the e-mail body
    : THE LINE I"M TRYING TO GET INFORMATION FROM
    : some more body information
    : THIS LINE I WANT TO PULL THE WORD "RED" INTO A VARIABLE
    : some more body
    : [end of first e-mail]
    : [[header for second e-mail]]
    :
    : ... and so-on
    :
    : The project requires me to pull user information e-mailed to this script and have the script make a new user account based on it.
    : I am not writing here to get an easy A, I could write this in about 5 different languages, and maybe 100000 different ways, but since this is an introductory class to unix which I know very little about, our teacher's english is so bad that no one can understand him, and there isn't a single thing about this in our text books I was hoping someone out there might be able to help me out. Mostly I need help breaking down text from a file and put key information into variables, the rest I can figure out.
    :
    : Thanks guys!
    : -P
    :
    :
    : ><//~Psightoplasm`~
    :

    With regards,
    P.Guru Vinayak

  • PsightoplazmPsightoplazm Member Posts: 332 ✭✭
    Grep, Sort and cut are what I was looking for. The book we have is very very basic and doesn't have these commands in there and the professor... well - sometimes in life there are challenges :P

    Thanks again

    ><//~Psightoplasm`~

  • PsightoplazmPsightoplazm Member Posts: 332 ✭✭
    So I came up with this - it works, but leaves an error when exiting the while loop because I believe it's getting a null value sent to cut.

    Is there a better comparative operator OR a way to convert a grep into it's length value or something like that to get rid of the annoying error?

    [code]
    #!/bin/bash
    mkdir /root/project
    cp /etc/passwd /root/project/password.txt
    UName=`grep "Username:" /var/spool/mail/root | cut -f2 -d" "`
    FName=`grep "Firstname:" /var/spool/mail/root | cut -f2 -d" "`
    LName=`grep "Lastname:" /var/spool/mail/root | cut -f2 -d" "`

    Num=600
    while [ $Num = `grep $Num /root/project/password.txt | cut -f3 -d":"` ]
    do
    Num=$((Num+1))
    done

    echo "$UName:password:$Num:$Num:$FName $LName:/home/$UName:/bin/bash" >> /root/project/password.txt
    [/code]


    Thanks again for the help


    ><//~Psightoplasm`~

Sign In or Register to comment.