Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Playing with awk and variables

  1. #1
    Join Date
    Mar 2014
    Beans
    11

    Playing with awk and variables

    This is a script that is used to read values from two different files and combine them into a third, thought it would be a piece of cake but it seams my cake is a dirt cake

    #!bin/bash
    #
    # Get Hostname from hostname.txt file
    HOST=$(awk 'FNR==1 {printf $1}' /tmp/hostname.txt)
    # Get ip address from ipdata.txt
    IP=$(awk 'FNR==5 {printf $2}' /tmp/ipdata.txt)
    # Create new file with variables
    echo "Start File" > /tmp/iphost.txt
    echo $HOST >> /tmp/iphost.txt
    echo $IP >> /tmp/iphost.txt
    echo $HOST $IP >> /tmp/iphost.txt
    echo $IP $HOST >> /tmp/iphost.txt
    echo "File Done" >> /tmp/iphost.txt

    If I do cat /tmp/iphost.txt this is the output
    Start File
    myhost
    192.168.1.15
    myhost 192.168.1.15
    myhost.1.15
    File Done

    I have tried many variations to try and get the second to last line to be myhost 192.168.1.15 but have failed at every attempt so any help would be greatly appreciated

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Playing with awk and variables

    Hi montydepaola.

    Try quoting your variables:
    Code:
    echo "$IP" "$HOST"
    or
    Code:
    echo "$IP $HOST"
    Let us know if that helped.
    Regards.

    EDIT: I would also use regular awk's print instead of printf.
    Last edited by papibe; April 17th, 2014 at 01:35 AM.

  3. #3
    Join Date
    Mar 2014
    Beans
    11

    Re: Playing with awk and variables

    Quote Originally Posted by papibe View Post
    Hi montydepaola.

    Try quoting your variables:
    Code:
    echo "$IP" "$HOST"
    or
    Code:
    echo "$IP $HOST"
    Let us know if that helped.
    Regards.
    papibe
    same results as with the first code,
    thanks for the quick try

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Playing with awk and variables

    Did you try the other suggestion (print instead of printf)?

    Regards.

  5. #5
    Join Date
    Mar 2014
    Beans
    11

    Re: Playing with awk and variables

    Quote Originally Posted by papibe View Post
    Did you try the other suggestion (print instead of printf)?

    Regards.
    did that after i tried the different quoting options
    also tried to force a third variable at the beginning by adding this...
    DIP=ZYXW
    echo $DIP $IP $HOST > /tmp/iphost.txt
    echo "$DIP $IP $HOST" >> /tmp/iphost.txt
    echo $DIP" "$IP" "$HOST" >> /tmp/iphost.txt
    none of these work, but a strange thing does happen, the longer the variable DIP is the more numbers I see of IP

  6. #6
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Playing with awk and variables

    I suspect your hostname.txt file has some special (non-printing) characters in it - my guess would be Windows line endings (CR-LF) instead of plain newlines

    What do you see if you do

    Code:
    cat -net hostname.txt

  7. #7
    Join Date
    Mar 2014
    Beans
    11

    Re: Playing with awk and variables

    Quote Originally Posted by steeldriver View Post
    I suspect your hostname.txt file has some special (non-printing) characters in it - my guess would be Windows line endings (CR-LF) instead of plain newlines

    What do you see if you do

    Code:
    cat -net hostname.txt
    it shows
    1 tower$

    I created it with nano

  8. #8
    Join Date
    Mar 2014
    Beans
    11

    Re: Playing with awk and variables

    Quote Originally Posted by montydepaola View Post
    it shows
    1 tower$

    I created it with nano
    sorry wrong box...
    1 myhost$

  9. #9
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Playing with awk and variables

    Could you do the same with ipdata.txt?
    Code:
    cat -net /tmp/ipdata.txt
    Another idea to tell if there's special characters would be to add 'od' to translate them:
    Code:
    #!/bin/bash
    # Get Hostname from hostname.txt file
    HOST=$(awk 'FNR==1 {printf $1}' /tmp/hostname.txt)
    
    # Get ip address from ipdata.txt
    IP=$(awk 'FNR==5 {printf $2}' /tmp/ipdata.txt)
    
    # Create new file with variables
    echo "Start File" > /tmp/iphost.txt
    echo "$HOST" | od -a  >> /tmp/iphost.txt
    echo "$IP"   | od -a  >> /tmp/iphost.txt
    echo "File Done" >> /tmp/iphost.txt
    Regards.

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Playing with awk and variables

    it definitely looks like an issue with \r. It rolls the cursor back to the start of the line after printing the first var, so visually the 2nd one overwrites it.

    Code:
    $ ip=$'192.168.1.15\r'; host=$'myhost\r'
    $ echo "$ip $host"
     myhost.1.15
    you may add -F"[ \t\r]+" param to awk to include \r in its field separator regex (space+tab by default) so it doesn't get into values
    you may remove the \r from the files eg with sed 's/\r//g'
    you may strip the \r from variables ${ip//$'\r'/} ${host//$'\r'/}
    Last edited by Vaphell; April 17th, 2014 at 04:31 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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