Bash: How to trim whitespace before using cut

浪尽此生 提交于 2020-01-22 17:50:06

问题


I have a line of output from a command like this:

[]$ <command> | grep "Memory Limit"
Memory Limit:           12345 KB

I'm trying to pull out the 12345. The first step - separating by the colon - works fine

[]$ <command> | grep "Memory Limit" | cut -d ':' -f2
           12345 KB

Trying to separate this is what's tricky. How can I trim the whitespace so that cut -d ' ' -f1 returns "12345" rather than a blank?


回答1:


Pipe your command to awk, print the 3rd column, a space and the 4th column like this

<command> | awk '{print $3, $4}'

This results in:

12345 KB

or

<command> | awk '{print $3}'

if you want the naked number.




回答2:


tr helps here.

$ echo "Memory Limit:           12345 KB" | tr -s " " | cut -d " " -f3
12345
  • tr -s " " - squeeze all spaces into one
  • cut -d " " -f3 - split into columns by space and select third



回答3:


You can use awk and avoid using any cut, sed or regex:

<command> | awk '/Memory Limit/{print $(NF-1)}'

12345
  • /Memory Limit/ will make this print only a line with Memory Limit text
  • $(NF-1) will get you last-1th field in input.



回答4:


do you have to use cut? cut is only for single character delimiter extraction. i don't think cut can be as easy as you would expect.

sed is straightforward:

$ echo "Memory Limit:           12345 KB" | sed 's/.*:\s*//'
12345 KB

explanation:

.*:\s* matches all letters before the last colon, then matches all the empty chars after that and delete them by substituting to an empty string.


it turns out that you were expecting a single number. then i would say just go ahead and match the numbers:

$ echo "Memory Limit:           12345 KB" | grep -o -P '\d+'
12345



回答5:


bash also has regular expression matching you can use.

result=$(<command> | grep "Memory Limit")
regex='Memory Limit:[[:space:]]+([[:digit:]]+)[[:space:]]KB'
if [[ $result =~ $regex ]]; then
  result=${BASH_REMATCH[0]}
else
  # deal with an unexpected result here
fi

The value of $regex can be adjusted as necessary.




回答6:


awk is perhaps the best for this task, but here is an unorthodox way

$ grep -oP "(?<=Memory Limit:)\s+[0-9]+" file | xargs

lookbehind for matching the label and output only matching, use xargs to eat up spaces



来源:https://stackoverflow.com/questions/38792983/bash-how-to-trim-whitespace-before-using-cut

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!