Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following CSV file

this file defined which Linux machine exist in the system and there ip's

my target is to create host file from this file

please advice how to create host file as example 1 from my CSV file ( I need to match the IP address from CSV file and put it on the first field of the host file , then match the LINUX name and locate this name in the sec field – as example 1 )

remark - should be performed by sed or awk or perl .. , I need to write the solution in my bash script

CSV file

    , machine , VM-LINUX1 , SZ , Phy ,  10.213.158.18 , PROXY

    , VM-LINUX2 , SZ , 10.213.158.19 ,

    OLD HW , VM-LINUX3 , SZ , 10.213.158.20 ,

    , VM-LINUX4 , SZ , Phy ,  10.213.158.21 ,

    , VM-LINUX5 , SZ , Phy , OUT , EXT , LAN3 , 10.213.158.22 , INTERNAL 

    , VM-LINUX6 , SZ , Phy ,  10.213.158.23 ,

    , server , new HW , VM-LINUX7 , SZ , Phy ,  10.213.158.24 , OUT, LAN3

    , VM-LINUX8 , SZ , 10.213.158.25 ,

    OLD HW , machine , VM-LINUX9 , SZ , Phy , INT , 10.213.158.26 , LAN2, AN45, 

    , VM-LINUX10 , SZ , Phy ,  10.213.158.27 ,

    , VM-LINUX11 , SZ , Phy ,  LAN5 , 10.213.158.28 ,

example 1 ( host file )

    10.213.158.18 VM-LINUX1
    10.213.158.19 VM-LINUX2
    10.213.158.20 VM-LINUX3
    10.213.158.21 VM-LINUX4
    10.213.158.22 VM-LINUX5
    10.213.158.23 VM-LINUX6
    10.213.158.24 VM-LINUX7
    10.213.158.25 VM-LINUX8
    10.213.158.26 VM-LINUX9
    10.213.158.27 VM-LINUX10
    10.213.158.25 VM-MACHINE8


    10.213.158.26 STAR9
    10.213.158.27 TOP10
    10.213.158.28 SERVER11
share|improve this question

2 Answers 2

up vote 1 down vote accepted

cat file | perl -ne '/(\w+-LINUX\w+).*\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/;print "$2 $1\n";'

share|improve this answer
    
Last question is it possible to set argument in the perl syntax , for example in place LINUX to write $LINUX , and then set LINUX=MACHINE1 or LINUX=LINUX100 ? –  yael Sep 19 '12 at 9:05
    
no, '$' have different meaning in regular expression syntax. It's a oneliner - just replace '-LINUX' with any string you want –  DukeLion Sep 19 '12 at 9:06
    
but what I can do if I have allot of different names ( for example in place of LINUX could be other different names ) ? can perl one linear support this ? ( see also my update in my quastion ) –  yael Sep 19 '12 at 9:12
    
If your hostnames are not common pattern, are in different columns and you have no idea how to formalize script requirements - it's probably better to just open csv with oocalc or something and edit it manually –  DukeLion Sep 19 '12 at 9:29
perl -ne 'print "$1 $2\n" if m/(VM-LINUX\d+).*(\d+\.\d+\.\d+\.\d+)/;' < /PATH/TO/your.csv

or (if not even the order of the two fields is determined)

perl -ne 'print "$1 " if m/(VM-LINUX\d+)/; print "$1" if m/(\d+\.\d+\.\d+\.\d+)/; print "\n";' < /PATH/TO/your.csv
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.