Find the answer to your Linux question:
Results 1 to 4 of 4
Hi there i'm having a little problem with something i'm writing: basicly its a forum with only one file and two commands: ./placemessage ./getmessage<nr> the placemessage command I wrote like ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    2

    [SOLVED] Bash/awk/sed help

    Hi there i'm having a little problem with something i'm writing:

    basicly its a forum with only one file and two commands:
    ./placemessage
    ./getmessage<nr>

    the placemessage command I wrote like this:
    Code:
    #! /bin/bash
    echo "Nickname?";
    read USERNAME;
    while ["$USERNAME" = ""]
    do
            echo "Please enter a nickname:";
            read USERNAME;
    done
    echo "Subject?";
    read SUBJECT;
    while ["$SUBJECT" = ""]
    do
            echo "Please enter a subject:";
            read SUBJECT;
    done
    echo "message:";
    read MESSAGE;
    while ["$MESSAGE" = ""]
    do
    do
    	echo "plaats een bericht aub:";
    	read MESSAGE;
    done
    echo nickname: $USERNAME >> myforum.txt
    echo onderwerp: $SUBJECT >> myforum.txt
    echo bericht: >> myforum.txt
    echo  $MESSAGE >> myforum.txt
    date >> myforum.txt
    echo -END- >> myforum.txt
    echo "" >> myforum.txt
    this does work and generates the desired text in the file but when I enter text when prompted it gives me an error message on the place of the loop where I entered the text.

    IE:
    when entering example as username I get:
    "./plaatsbericht: regel 4: [example: opdracht niet gevonden"
    the same goes for subject/message.

    how can I get rid of this error? I don't want to redirect the error messages tho

    the second issue I have is when trying to read a message
    this is the code i've been going nuts about:

    this was an attempt to get the third message out of the file:
    Code:
    #!/bin/bash
    
    gawk -v RS='\n\n' '{print $3}' myforum.txt
    all I get is the third field and I have no idea how I get the third paragraph(record?)

    so if anyone could enlighten me a bit it would be greatly appreciated, i've been messing with this for several hours now and i'm still getting nowhere

    btw just giving me a code to copy/paste isn't going to help me an explanation on how it works would be more usefull to me!

    thanks in advance guys!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    For your first problem, the answer is that Bash is very stupid. Therefore, you actually need to leave spaces between the [ of the condition and the " of the variable:
    Code:
    while [ "$USERNAME" = "" ]
    Also be aware that there is a better way to do this:
    Code:
    while [ ! -z "$USERNAME" ]
    "-z" checks to see if the variable is empty.

    For your second problem, $3 is the third field, not the third line, as you saw. However, the problem is also that you don't want the third line; you want the third entry as separated by -END- and a blank line.

    I'm on my Mac right now, so I can't use gawk (which allows more options for the record separator, and I can check that later), but this works better than your current one anyway:
    Code:
    awk 'BEGIN { RS = "" }; (NR == 3) { print $0 }' myforum.txt
    What this does is change the record separator to a blank line. It then prints out the third record.

    What I want to test on gawk is using a regular expression for RS, particularly the regular expression "-END-\n\n", which would treat "-END-" followed by a blank line as the record separator. In the code above, the "-END-" is treated as part of the message; in this gawk regex, it will not be.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by Cabhan View Post
    entire post
    I knew it had to be a stupid little error in the first one

    the second one I knew I was wrong, I tried findin a way to show the third record but unfortunatly I couldn't find anything on the web nor in the book I have here.
    only stuff to seperate on the field seperator

    anyway in this:

    Code:
    awk 'BEGIN { RS = "" }; (NR == 3) { print $0 }' myforum.txt
    NR is the current record which we put on 3 right? (just making sure i'm getting all of it) and the print $0 is print the entire record?

    (my knowledge of awk is a bit limited, I just transfered to this class a month ago and the only language I actually master is java, thats why its all a bit overwhelming, but after all I love it, its a great way to quickly process files)

    anyway thanks a lot for your help, and for the quick reply to my post

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    NR means number of records, and technically means the number of records encountered so far, including the current one (so yes, it is most frequently used as the current record). $1, $2, etc. are fields 1, 2, etc., but $0 is the entire record.

    And awk is very complicated: whenever I use it, I need a manual to help me. But I'm glad that you're working hard on it. Good luck with your class!
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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