cut

Using CUT and Quartile to generate breaks in R function

对着背影说爱祢 提交于 2019-11-26 12:44:45
问题 Following some great advice from before, I\'m now writing my 2nd R function and using a similar logic. However, I\'m trying to automate a bit more and may be getting too smart for my own good. I want to break the clients into quintiles based on the number of orders. Here\'s my code to do so: # sample data clientID <- round(runif(200,min=2000, max=3000),0) orders <- round(runif(200,min=1, max=50),0) df <- df <- data.frame(cbind(clientID,orders)) #function to break them into quintiles

How to find the last field using &#39;cut&#39;

寵の児 提交于 2019-11-26 11:45:58
问题 Without using sed or awk , only cut , how do I get the last field when the number of fields are unknown or change with every line? 回答1: You could try something like this: echo 'maps.google.com' | rev | cut -d'.' -f 1 | rev Explanation maps.google.com's reverse will be moc.elgoog.spam cut uses dot as the delimiter and chooses the first field, which is moc lastly, we reverse it again (thanks for the reminder, @tom) to get com 回答2: Use a parameter expansion. This is much more efficient than any

How to split a string in shell and get the last field

好久不见. 提交于 2019-11-26 08:39:46
问题 Suppose I have the string 1:2:3:4:5 and I want to get its last field ( 5 in this case). How do I do that using Bash? I tried cut , but I don\'t know how to specify the last field with -f . 回答1: You can use string operators: $ foo=1:2:3:4:5 $ echo ${foo##*:} 5 This trims everything from the front until a ':', greedily. ${foo <-- from variable foo ## <-- greedy front trim * <-- matches anything : <-- until the last ':' } 回答2: Another way is to reverse before and after cut : $ echo ab:cd:ef |

How to make the &#39;cut&#39; command treat same sequental delimiters as one?

徘徊边缘 提交于 2019-11-26 03:47:10
问题 I\'m trying to extract a certain (the fourth) field from the column-based, \'space\'-adjusted text stream. I\'m trying to use the cut command in the following manner: cat text.txt | cut -d \" \" -f 4 Unfortunately, cut doesn\'t treat several spaces as one delimiter. I could have piped through awk awk \'{ printf $4; }\' or sed sed -E \"s/[[:space:]]+/ /g\" to collapse the spaces, but I\'d like to know if there any way to deal with cut and several delimiters natively? 回答1: Try: tr -s ' ' <text