bloodstorm17
bloodstorm17

Reputation: 521

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.

Upvotes: 0

Views: 204

Answers (3)

sanjay
sanjay

Reputation: 1

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

where /etc/hosts is the name of a file.
(head -2 ) is used to retrieve top 2 lines from the file.
(tail -1) is used to retrieve only last one line outputed from (head -2).
(awk '{print$2}') is used to print the 2nd column of line outputted from (tail -1).

Upvotes: -1

Sean Bright
Sean Bright

Reputation: 120644

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:

#!/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>&-

Upvotes: 7

Brian Agnew
Brian Agnew

Reputation: 272237

Have you looked at using paste ?

e.g.

$ paste testA.txt testB.txt

etc. The -d operator will specify a separator character.

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.

Upvotes: 0

Related Questions