2017-03-07 51 views
1

我有2個文件:使用awk的結果作爲參數來grep一個文件?

1 - userfile.txt與格式=>鮑勃:bobhash
2 - => bobhash passfile.txt與格式:bobpass

我想要得到的bobpass給出的用戶「bob」,如果可能,用單行命令。

所以,到目前爲止,我正在做這樣的:

awk -F: '$1="bob" {print $2}' userfile.txt 
# output: bobhash 
# copy by using mouse 

cat passfile.txt | grep 'bobhash' | awk -F: '{print $2}' 
#output: bobpass 

我不知道是否有更好的方法來做到這一點。我已經試過grep -f,但沒有運氣。

+0

是的,只需使用一個awk命令而不是多個awks + grep。您的問題包括簡潔,可測試的樣本輸入和預期輸出,您將獲得幫助。 –

回答

1

使用下列一行awk方法:

awk -F: 'NR == FNR{ if($1=="bob") p=$2; next}(p == $1){print $2}' userfile.txt passfile.txt 

在這裏,我們有兩個文件處理。
工作原理:

$ awk 'NR == FNR { # some actions; next} # other condition {# other actions}' file1.txt file2.txt

When processing more than one file, awk reads each file sequentially, one after another, in the order they are specified on the command line. The special variable NR stores the total number of input records read so far, regardless of how many files have been read. The value of NR starts at 1 and always increases until the program terminates. Another variable, FNR, stores the number of records read from the current file being processed. The value of FNR starts at 1, increases until the end of the current file is reached, then is set again to 1 as soon as the first line of the next file is read, and so on. So, the condition NR == FNR is only true while awk is reading the first file. Thus, in the program above, the actions indicated by # some actions are executed when awk is reading the first file; the actions indicated by # other actions are executed when awk is reading the second file, if the condition in # other condition is met. The next at the end of the first action block is needed to prevent the condition in # other condition from being evaluated, and the actions in # other actions from being executed, while awk is reading the first file.

+0

我想用同樣的方法可以打開'1到n'文件並操作它們的輸出。這是一個完整的答案。謝謝! – ioaniatr

+0

另一件事,我可以傳遞變量並將它們從第一個文件打印到另一個?例如'awk -F:'#code .... {print p:$ 2}'file1 file2'。所以,我可以輸出'bob:bobpass' – ioaniatr

+1

@ioaniatr,歡迎,你可以通過'-v'選項(賦值給一個變量)傳遞一個變量,比如'awk -v name = bob'{print name; }''。另外,你可以初始化內部變量,例如:'awk'BEGIN {ret = getline <「junk.txt」; if(ret == -1)打印「錯誤:」,ERRNO}'' – RomanPerekhrest

-1

下面是您已有的一些修改版本,可能會爲您提供所需內容。

簡單一個襯裏:

cat passfile.txt | grep "$(awk -v user="bob" -F: '$1=user {print $2}' userfile.txt)" | awk -F: '{print $2}' 

修改用於在腳本使用:

my_user="bob" 

cat passfile.txt | grep "$(awk -v user="${my_user}" -F: '$1=user {print $2}' userfile.txt)" | awk -F: '{print $2}' 

第二個選項添加在用戶通過作爲一個變量,而不是硬編碼它的能力。如果你在腳本中使用它,它可以動態設置,這使得這更加靈活。

另一種解決方案用awk只:

awk -F: -v user="$(awk -F: '$1="bob" {print $2}' userfile.txt)" '$1=user {print $2}' passfile.txt 
+0

UUOC,當你使用awk時你永遠不需要grep,因爲awk可以做grep可以做的所有事情。 –

+0

公平對賬單。我想我總是試圖通過對他們的代碼產生最小的影響來獲得他們想要的結果來解決這些問題。有時人們更容易以這種方式理解事物。但是,我編輯了我的答案,以顯示如何在沒有grep的情況下完成此操作,因爲您是對的,這不是必需的。 –

0

運行awk一次:

awk -v search=bob -F: 'NR==FNR && $1==search {hash=$2 }; 
    NR>FNR && $1==hash {print "pw=" $2 }' userfile.txt passfile.txt 

可以當沒有找到匹配(或兩次)的一些控制改變這一點。

0

在它的一些利益的情況下,這是一個全球性的解決方案,將滿足所有用戶userfile.txt與它們對應的密碼在passfile.txt

awk -F":" 'NR==FNR{a[$2]=$1;next}a[$1]{print a[$1],$2}' users pass 
+0

如果我想從「usersfile.txt」中獲取所有用戶,並且可能通過在最後添加'> newfile.txt'將它們存儲在新文件中,那麼它非常有用。 – ioaniatr

+0

@ioaniatr是的,這正是我的想法。您可以在屏幕上打印它們或將它們發送到新文件。作爲一個「不好的用法」,你可以打印所有用戶/密碼和grep給Bob :-) –