使用cut命令将空格用作定界符
本文翻譯自:Use space as a delimiter with cut command
I want to use space as a delimiter with the cut command. 我想通過cut命令將空格用作定界符。
What syntax can I use for this? 我可以為此使用什么語法?
#1樓
參考:https://stackoom.com/question/3QUW/使用cut命令將空格用作定界符
#2樓
scut , a cut-like utility (smarter but slower I made) that can use any perl regex as a breaking token. scut ,類似于cut的實用程序(我制作的更智能,但速度較慢),可以將任何perl regex用作中斷令牌。 Breaking on whitespace is the default, but you can also break on multi-char regexes, alternative regexes, etc. 默認(rèn)打破空白,但是您也可以打破多字符正則表達(dá)式,替代正則表達(dá)式等。
scut -f='6 2 8 7' < input.file > output.fileso the above command would break columns on whitespace and extract the (0-based) cols 6 2 8 7 in that order. 因此,以上命令將中斷空白列并按此順序提取(從0開始)cols 6 2 8 7。
#3樓
Usually if you use space as delimiter, you want to treat multiple spaces as one, because you parse the output of a command aligning some columns with spaces. 通常,如果您使用空格作為定界符,則您希望將多個空格視為一個空格,因為您解析了將某些列與空格對齊的命令輸出。 (and the google search for that lead me here) (和谷歌搜索導(dǎo)致我在這里)
In this case a single cut command is not sufficient, and you need to use: 在這種情況下,一個單獨的cut命令是不夠的,您需要使用:
tr -s ' ' | cut -d ' ' -f 2Or 要么
awk '{print $2}'#4樓
I just discovered that you can also use "-d " : 我剛剛發(fā)現(xiàn)您也可以使用"-d " :
cut "-d "Test 測試
$ cat a hello how are you I am fine $ cut "-d " -f2 a how am#5樓
To complement the existing, helpful answers; 補(bǔ)充現(xiàn)有的,有用的答案; tip of the hat to QZ Support for encouraging me to post a separate answer: QZ支持小組鼓勵我發(fā)表單獨的答案:
Two distinct mechanisms come into play here: 兩種不同的機(jī)制在這里起作用:
(a) whether cut itself requires the delimiter (space, in this case) passed to the -d option to be a separate argument or whether it's acceptable to append it directly to -d . (a) cut 本身是否需要傳遞給-d選項的定界符(在這種情況下為空格)作為單獨的參數(shù),或者是否可以將其直接附加到-d 。
(b) how the shell generally parses arguments before passing them to the command being invoked. (b) shell在將參數(shù)傳遞給被調(diào)用的命令之前通常如何解析參數(shù)。
(a) is answered by a quote from the POSIX guidelines for utilities (emphasis mine) (a)由POSIX公用事業(yè)指南 (強(qiáng)調(diào)我的)引述
If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument [...] a conforming application shall use separate arguments for that option and its option-argument . 如果標(biāo)準(zhǔn)實用程序的摘要顯示帶有強(qiáng)制性選項參數(shù)的選項,則符合標(biāo)準(zhǔn)的應(yīng)用程序應(yīng)對該選項及其選項參數(shù)使用單獨的參數(shù) 。 However , a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening characters . 然而 ,一個符合標(biāo)準(zhǔn)的實現(xiàn)也應(yīng)允許應(yīng)用程序指定在同一參數(shù)串的選項,選項參數(shù)中間沒有字符 。
In other words: In this case, because -d 's option-argument is mandatory , you can choose whether to specify the delimiter as : 換句話說:在這種情況下, 由于-d的option-argument是強(qiáng)制性的 ,因此您可以選擇是否將分隔符指定為 :
- (s) EITHER: a separate argument (s)Ether:一個單獨的論點
- (d) OR: as a value directly attached to -d . (d)或:作為直接附加到-d的值。
Once you've chosen (s) or (d), it is the shell 's string-literal parsing - (b) - that matters: 選擇(s)或(d)之后, shell的字符串文字解析-(b)就很重要:
With approach (s) , all of the following forms are EQUIVALENT: 隨著辦法(S),以下所有形式是等價的:
- -d ' '
- -d " "
- -d \\<space> # <space> used to represent an actual space for technical reasons
With approach (d) , all of the following forms are EQUIVALENT: 使用方法(d) ,以下所有形式均等效:
- -d' '
- -d" "
- "-d "
- '-d '
- d\\<space>
The equivalence is explained by the shell 's string-literal processing: 等價由shell的字符串文字處理解釋:
All solutions above result in the exact same string (in each group) by the time cut sees them : 上面的所有解決方案在時間cut都會得到完全相同的字符串 (在每個組中) :
(s) : cut sees -d , as its own argument, followed by a separate argument that contains a space char - without quotes or \\ prefix!. (s) : cut將-d視為其自己的參數(shù),后跟一個單獨的參數(shù),該參數(shù)包含空格char-不帶引號或\\前綴!
(d) : cut sees -d plus a space char - without quotes or \\ prefix! (d) : cut看到-d 加一個空格char-沒有引號或\\前綴! - as part of the same argument. -作為相同論點的一部分。
The reason the forms in the respective groups are ultimately identical is twofold, based on how the shell parses string literals : 各個組中的形式最終相同的原因是雙重的,這取決于外殼如何解析字符串文字 :
- The shell allows literal to be specified as is through a mechanism called quoting , which can take several forms : Shell允許通過稱為quoting的機(jī)制 按原樣指定文字, 該機(jī)制可以采用幾種形式 :
- single-quoted strings: the contents inside '...' is taken literally and forms a single argument 用單引號引起來的字符串: '...'的內(nèi)容按字面意義使用并形成單個參數(shù)
- double-quoted strings: the contents inside "..." also forms a single argument, but is subject to interpolation (expands variable references such as $var , command substitutions ( $(...) or `...` ), or arithmetic expansions ( $(( ... )) ). 雙引號字符串:里面的內(nèi)容"..."還形成一個參數(shù),但受插值 (擴(kuò)展變量引用,如$var ,命令替換( $(...)或`...`或算術(shù)擴(kuò)展( $(( ... )) )。
- \\ -quoting of individual characters : a \\ preceding a single character causes that character to be interpreted as a literal. \\引用單個字符 :單個字符前面的\\導(dǎo)致該字符被解釋為文字。
- Quoting is complemented by quote removal , which means that once the shell has parsed a command line, it removes the quote characters from the arguments (enclosing '...' or "..." or \\ instances) - thus, the command being invoked never sees the quote characters . 引用是通過引用刪除來補(bǔ)充的,這意味著一旦shell解析了命令行,它就會從參數(shù)中刪除引用字符 (用'...'或"..."或\\實例括起來)-因此, 命令是被調(diào)用從未看到引號字符 。
#6樓
You can't do it easily with cut if the data has for example multiple spaces. 如果數(shù)據(jù)有多個空格,則用cut很難做到這一點。 I have found it useful to normalize input for easier processing. 我發(fā)現(xiàn)對輸入進(jìn)行標(biāo)準(zhǔn)化以簡化處理非常有用。 One trick is to use sed for normalization as below. 一種技巧是使用sed進(jìn)行如下標(biāo)準(zhǔn)化。
echo -e "foor\t \t bar" | sed 's:\s\+:\t:g' | cut -f2 #bar總結(jié)
以上是生活随笔為你收集整理的使用cut命令将空格用作定界符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JMeter学习-006-JMeter
- 下一篇: 全国青少年软件编程(Scratch)等级