How to specify more spaces for the delimiter using cut?

前端 未结 12 597
遥遥无期
遥遥无期 2020-12-04 05:51

Is there any way to specify a field delimiter for more spaces with the cut command? (like \" \"+) ? For example: In the following string, I like to reach value \'3744\', wha

相关标签:
12条回答
  • 2020-12-04 06:12

    As an alternative, there is always perl:

    ps aux | perl -lane 'print $F[3]'
    

    Or, if you want to get all fields starting at field #3 (as stated in one of the answers above):

    ps aux | perl -lane 'print @F[3 .. scalar @F]'
    
    0 讨论(0)
  • 2020-12-04 06:19

    Personally, I tend to use awk for jobs like this. For example:

    ps axu| grep jboss | grep -v grep | awk '{print $5}'
    
    0 讨论(0)
  • 2020-12-04 06:21

    Actually awk is exactly the tool you should be looking into:

    ps axu | grep '[j]boss' | awk '{print $5}'
    

    or you can ditch the grep altogether since awk knows about regular expressions:

    ps axu | awk '/[j]boss/ {print $5}'
    

    But if, for some bizarre reason, you really can't use awk, there are other simpler things you can do, like collapse all whitespace to a single space first:

    ps axu | grep '[j]boss' | sed 's/\s\s*/ /g' | cut -d' ' -f5
    

    That grep trick, by the way, is a neat way to only get the jboss processes and not the grep jboss one (ditto for the awk variant as well).

    The grep process will have a literal grep [j]boss in its process command so will not be caught by the grep itself, which is looking for the character class [j] followed by boss.

    This is a nifty way to avoid the | grep xyz | grep -v grep paradigm that some people use.

    0 讨论(0)
  • 2020-12-04 06:24

    Shorter/simpler solution: use cuts (cut on steroids I wrote)

    ps axu | grep '[j]boss' | cuts 4
    

    Note that cuts field indexes are zero-based so 5th field is specified as 4

    http://arielf.github.io/cuts/

    And even shorter (not using cut at all) is:

    pgrep jboss
    
    0 讨论(0)
  • 2020-12-04 06:29

    My approach is to store the PID to a file in /tmp, and to find the right process using the -S option for ssh. That might be a misuse but works for me.

    #!/bin/bash
    
    TARGET_REDIS=${1:-redis.someserver.com}
    PROXY="proxy.somewhere.com"
    
    LOCAL_PORT=${2:-6379}
    
    if [ "$1" == "stop" ] ; then
        kill `cat /tmp/sshTunel${LOCAL_PORT}-pid`
        exit
    fi
    
    set -x
    
    ssh -f -i ~/.ssh/aws.pem centos@$PROXY -L $LOCAL_PORT:$TARGET_REDIS:6379 -N -S /tmp/sshTunel$LOCAL_PORT  ## AWS DocService dev, DNS alias
    # SSH_PID=$! ## Only works with &
    SSH_PID=`ps aux | grep sshTunel${LOCAL_PORT} | grep -v grep | awk '{print $2}'`
    echo $SSH_PID > /tmp/sshTunel${LOCAL_PORT}-pid
    

    Better approach might be to query for the SSH_PID right before killing it, since the file might be stale and it would kill a wrong process.

    0 讨论(0)
  • 2020-12-04 06:31

    One way around this is to go:

    $ps axu | grep jboss | sed 's/\s\+/ /g' | cut -d' ' -f3
    

    to replace multiple consecutive spaces with a single one.

    0 讨论(0)
提交回复
热议问题