日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

使用cut命令将空格用作定界符

發(fā)布時(shí)間:2023/12/31 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用cut命令将空格用作定界符 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文翻譯自:Use space as a delimiter with cut command

I want to use space as a delimiter with the cut command. 我想通過(guò)cut命令將空格用作定界符。

What syntax can I use for this? 我可以為此使用什么語(yǔ)法?


#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的實(shí)用程序(我制作的更智能,但速度較慢),可以將任何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.file

so the above command would break columns on whitespace and extract the (0-based) cols 6 2 8 7 in that order. 因此,以上命令將中斷空白列并按此順序提取(從0開(kāi)始)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. 通常,如果您使用空格作為定界符,則您希望將多個(gè)空格視為一個(gè)空格,因?yàn)槟馕隽藢⒛承┝信c空格對(duì)齊的命令輸出。 (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: 在這種情況下,一個(gè)單獨(dú)的cut命令是不夠的,您需要使用:

tr -s ' ' | cut -d ' ' -f 2

Or 要么

awk '{print $2}'

#4樓

I just discovered that you can also use "-d " : 我剛剛發(fā)現(xiàn)您也可以使用"-d " :

cut "-d "

Test 測(cè)試

$ 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支持小組鼓勵(lì)我發(fā)表單獨(dú)的答案:

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選項(xiàng)的定界符(在這種情況下為空格)作為單獨(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)實(shí)用程序的摘要顯示帶有強(qiáng)制性選項(xiàng)參數(shù)的選項(xiàng),則符合標(biāo)準(zhǔn)的應(yīng)用程序應(yīng)對(duì)該選項(xiàng)及其選項(xiàng)參數(shù)使用單獨(dú)的參數(shù)However , a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening characters . 然而 ,一個(gè)符合標(biāo)準(zhǔn)的實(shí)現(xiàn)應(yīng)允許應(yīng)用程序指定在同一參數(shù)串的選項(xiàng),選項(xiàng)參數(shù)中間沒(méi)有字符

In other words: In this case, because -d 's option-argument is mandatory , you can choose whether to specify the delimiter as : 換句話說(shuō):在這種情況下, 由于-d的option-argument是強(qiáng)制性的 ,因此您可以選擇是否將分隔符指定為

  • (s) EITHER: a separate argument (s)Ether:一個(gè)單獨(dú)的論點(diǎn)
  • (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),以下所有形式是等價(jià)的:

    • -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: 等價(jià)由shell的字符串文字處理解釋:

All solutions above result in the exact same string (in each group) by the time cut sees them : 上面的所有解決方案在時(shí)間cut都會(huì)得到完全相同的字符串 (在每個(gè)組中)

  • (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ù),后跟一個(gè)單獨(dú)的參數(shù),該參數(shù)包含空格char-不帶引號(hào)或\\前綴!

  • (d) : cut sees -d plus a space char - without quotes or \\ prefix! (d) : cut看到-d 加一個(gè)空格char-沒(méi)有引號(hào)或\\前綴! - as part of the same argument. -作為相同論點(diǎn)的一部分。

The reason the forms in the respective groups are ultimately identical is twofold, based on how the shell parses string literals : 各個(gè)組中的形式最終相同的原因是雙重的,這取決于外殼如何解析字符串文字

  • The shell allows literal to be specified as is through a mechanism called quoting , which can take several forms : Shell允許通過(guò)稱為quoting的機(jī)制 按原樣指定文字, 該機(jī)制可以采用幾種形式 :
    • single-quoted strings: the contents inside '...' is taken literally and forms a single argument 用單引號(hào)引起來(lái)的字符串: '...'的內(nèi)容按字面意義使用并形成單個(gè)參數(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 ( $(( ... )) ). 雙引號(hào)字符串:里面的內(nèi)容"..."還形成一個(gè)參數(shù),但受插值 (擴(kuò)展變量引用,如$var ,命令替換( $(...)或`...`或算術(shù)擴(kuò)展( $(( ... )) )。
    • \\ -quoting of individual characters : a \\ preceding a single character causes that character to be interpreted as a literal. \\引用單個(gè)字符 :單個(gè)字符前面的\\導(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 . 引用是通過(guò)引用刪除來(lái)補(bǔ)充的,這意味著一旦shell解析了命令行,它就會(huì)從參數(shù)中刪除引用字符 (用'...'或"..."或\\實(shí)例括起來(lái))-因此, 命令是被調(diào)用從未看到引號(hào)字符

#6樓

You can't do it easily with cut if the data has for example multiple spaces. 如果數(shù)據(jù)有多個(gè)空格,則用cut很難做到這一點(diǎn)。 I have found it useful to normalize input for easier processing. 我發(fā)現(xiàn)對(duì)輸入進(jìn)行標(biāo)準(zhǔn)化以簡(jiǎ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命令将空格用作定界符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。