簡體   English   中英

使用 awk 查找列值 *

[英]finding the column value * using awk

目前,我有一個包含多列的文件,在我的第 4,5 列,null 值顯示為 *

我努力了


awk '{if ($4==/*) {print}}' filename
awk '{if ($4==\*) {print}}' filename
awk '{if ($4=='*') {print}}' filename
awk '{if ($4=='\*') {print}}' filename
awk '{if ($4=='/*/') {print}}' filename

為了

awk '{if ($4=='/*/') {print}}' filename
awk '{if ($4==/*/) {print}}' filename

他們運行,但沒有打印出任何東西(但我知道文件中有 * 值)

和 rest 他們收到語法錯誤或正則表達式錯誤

我該怎么做才能解決這個問題???

awk字符串用雙引號括起來。 The awk script is normally put in single quotes, and if you also use single quotes around the awk strings, they would terminate the shell string, not delimit the awk string (and shell single quotes don't provide any escaping mechanism).

awk '$4 == "*"' filename

處理一行的條件放在動作之前,如果沒有動作塊,則默認打印該行。 所以上面的內容等價於:

awk '{if ($4 == "*") print}' filename

使用 awk 模式來實現條件

awk '$4 == "*" || $5 == "*" { print }'

Or Even
awk '$4 == "*" || $5 == "*"'

我會使用 function index()

awk 'index($4""$5, "*")' file

index(s, t)s中返回 substring t的 position ,如果在s中找不到t則返回0

上面的程序使用index() function 作為條件。 如果條件變為真, awk將打印當前行。 0 ,未找到,在 awk 中是錯誤的。 上述程序的更明確的版本是:

awk '{if(index($4""$5, "*") != 0){print}}' file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM