簡體   English   中英

從文本文件中檢索信息。 的Linux

[英]Retrieving information from a text file. Linux

基本上,我試圖從三個包含唯一信息的文本文件中讀取信息。

文本文件的設置方式是這樣的:

textA.txt
----------------
something.awesome.com
something2.awesome.com
something3.awesome.com
...

textB.txt
----------------
123
456
789
...

textC.txt
----------------
12.345.678.909
87.65.432.1
102.254.326.12
....

現在當我輸出這樣的東西時它看起來應該是什么樣子

something.awesome.com : 123 : 12.345.678.909
something2.awesome.com : 456 : 87.65.432.1
something3.awesome.com : 789 : 102.254.326.12

我現在正在嘗試的代碼是這樣的:

for each in `cat site.txt` ; do
    site=`echo $each | cut -f1`

    for line in `cat port.txt` ; do
        port=`echo $line | cut -f1`

        for this in `cat ip.txt` ; do
            connect=`echo $this | cut -f1`

            echo "$site : $port : $connect"
        done
    done
done

我得到的結果只是瘋狂的錯誤,而不是我想要的。 我不知道該如何解決。

我希望能夠通過可變形式調用信息。

paste testA.txt testB.txt testC.txt | sed -e 's/\t/ : /g'

輸出為:

something.awesome.com : 123 : 12.345.678.909
something2.awesome.com : 456 : 87.65.432.1
something3.awesome.com : 789 : 102.254.326.12

編輯:這是使用純bash的解決方案:

#!/bin/bash                                                                                                                         

exec 7<testA.txt
exec 8<testB.txt
exec 9<testC.txt

while true
do
    read site <&7
    read port <&8
    read connect <&9

    [ -z "$site" ] && break

    echo "$site : $port : $connect"
done

exec 7>&-
exec 8>&-
exec 9>&-

您是否看過使用粘貼

例如

$ paste testA.txt testB.txt

等等-d操作符將指定一個分隔符。

一個相關的實用程序是類SQL的join ,您可以在必須使用輸入文件公用字段進行聯接的情況下使用它。

head -2 /etc/hosts | tail -1 | awk '{print$2}'

/etc/hosts是文件名。
(head -2 )用於從文件中檢索前2行。
(tail -1)僅用於檢索從(head -2)輸出的最后一行。
(awk '{print$2}')用於打印從(tail -1)輸出的行的第二列。

暫無
暫無

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

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