Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    xargs always adds whitespace


    Hi

    I want to make a single line command, to start a application which needs my ip address. The problem is, that a whitespace is always added in front of the ip address, thus the command doesnt work. How can I remove this first whitespace ?

    First part:
    Code:
    who -m | cut -d '(' -f2 | cut -d ')' -f1
    172.28.41.192
    The first parts puts out the correct string without a whitespace, but when I add xargs it looks like this:
    Code:
    who -m | cut -d '(' -f2 | cut -d ')' -f1 | xargs echo 'TEST='
    TEST= 172.28.41.192
    it should look like:
    Code:
    TEST=172.28.41.192

    thanks for your help.
    regards
    Erich

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    4,017
    Hi,

    the whitespace is the default delimiter of xargs.
    If I understand the man page correct, then you can change the delimiter but there needs to be one.
    And it cant be an empty string.

    You could set a variable though.
    This simplified regex matches ipv4 and ipv6 addresses of an who -m output (I was too lazy for an exact regex)
    Code:
    $ ip=$(who -m | sed -nre 's/.*\(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|([A-f0-9:]+:+)+[A-f0-9]+)\)$/\1/p')
    $ echo $ip
    10.0.2.2
    $ echo $ip | cat -A
    10.0.2.2$
    Single line:
    Code:
    $ ip=$(who -m | sed -nre 's/.*\(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|([A-f0-9:]+:+)+[A-f0-9]+)\)$/\1/p'); echo $ip
    10.0.2.2
    Last edited by Irithori; 12-06-2017 at 10:39 AM.
    You must always face the curtain with a bow.

  3. #3
    Hi

    Thanks for your help. I am using tcsh so I dont know exactly how to set the variable there

    my command in the end should look like this, (ip should be generated from command above of course)

    Code:
    ll - --owd=172.28.11.11 &
    thanks
    regards
    Erich

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    4,017
    I dont regularly use tcsh, so there might be more elegant solutions.
    But fwiw:
    Code:
    set ip=`who -m | sed -nre 's/.*\(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|([A-f0-9:]+:+)+[A-f0-9]+)\)$/\1/p'`; ll - --owd=$ip &
    You must always face the curtain with a bow.

  6. #5
    Hi

    Thanks very much for your fast help. Based on your command I modified it a little bit and got it to work, also when the dns name is shown instead of the ip (which happens on some of our machines)
    My command look like this now and works on all our machines:

    Code:
    set myip=`who -m | sed -nre 's/.*\((.*)\)/\1/p'` ; ll - --owd=$myip':6800' &
    regards
    Erich

Posting Permissions

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