Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Playing with awk and variables

  1. #11
    Join Date
    Mar 2014
    Beans
    11

    Thumbs up Re: Playing with awk and variables

    Quote Originally Posted by Vaphell View Post
    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'/}
    Awesome find,
    I changed the code to this and I no longer have a dirt cake...Thanks for all the help
    IP=$(awk 'FNR==5 {printf $2}' /tmp/ipdata.txt)
    IP=${IP//$'\r'/}
    echo $IP $HOST >> /tmp/iphost.txt

    I am kind of puzzled as to why this was not happening when I wrote the host before the ip...
    but I have other things to drink about this weekend without having that floating around in the back of my mind

    Thanks for all the help

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

    Re: Playing with awk and variables

    Glad you worked that out.

    As an alternative, if the whole the file is in Windows format (all '\r\n' instead of plain '\n'), you could change the Record Separator (RS) in awk:
    Code:
    IP=$(awk 'BEGIN{ RS="\r\n" }; FNR==5 {printf $2}' /tmp/ipdata.txt)
    Regards.

  3. #13
    Join Date
    Mar 2014
    Beans
    11

    Thumbs up Re: Playing with awk and variables

    Quote Originally Posted by papibe View Post
    Glad you worked that out.

    As an alternative, if the whole the file is in Windows format (all '\r\n' instead of plain '\n'), you could change the Record Separator (RS) in awk:
    Code:
    IP=$(awk 'BEGIN{ RS="\r\n" }; FNR==5 {printf $2}' /tmp/ipdata.txt)
    Regards.
    Thanks for the response papibe,
    I have made sure that all the files in use have been created and edited with either nano or gedit and never opened anywhere else.
    However this is a great idea as this project will be used by others so I will recode it just in case they use notepad
    or another editor to change the files.

    Thanks

Page 2 of 2 FirstFirst 12

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
  •