简体   繁体   English

从文本文件中检索信息。 的Linux

[英]Retrieving information from a text file. Linux

Basically I am trying to read information from three text files in which it contains unique information. 基本上,我试图从三个包含唯一信息的文本文件中读取信息。

The way the text file is setup is this: 文本文件的设置方式是这样的:

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
....

Now what its suppose to look like when i output it something like this 现在当我输出这样的东西时它看起来应该是什么样子

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

The code I am trying now is this: 我现在正在尝试的代码是这样的:

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

The result I am getting is just crazy wrong and just not what i want. 我得到的结果只是疯狂的错误,而不是我想要的。 I don't know how to fix this. 我不知道该如何解决。

I want to be able to call the information through variable form. 我希望能够通过可变形式调用信息。

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

Output is: 输出为:

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

Edit: Here is a solution using pure bash : 编辑:这是使用纯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>&-

Have you looked at using paste ? 您是否看过使用粘贴

eg 例如

$ paste testA.txt testB.txt

etc. The -d operator will specify a separator character. 等等-d操作符将指定一个分隔符。

A related utility is the SQL-like join , which you can use in scenarios where you have to join using fields common to your input files. 一个相关的实用程序是类SQL的join ,您可以在必须使用输入文件公用字段进行联接的情况下使用它。

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

where /etc/hosts is the name of a file. /etc/hosts是文件名。
(head -2 ) is used to retrieve top 2 lines from the file. (head -2 )用于从文件中检索前2行。
(tail -1) is used to retrieve only last one line outputed from (head -2) . (tail -1)仅用于检索从(head -2)输出的最后一行。
(awk '{print$2}') is used to print the 2nd column of line outputted from (tail -1) . (awk '{print$2}')用于打印从(tail -1)输出的行的第二列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM