Bash append values if keywords are present in the file

Hi Team,

i have a web ui where user will be passing values and the output will be saved to a file say test with the following contents .

These below mentioned values will change according to the user_input

Just gave here one example

Contents of file test is given below

Gateway oe:value 3.3.3.3
Hostname oe:value test.test.com
IP_Address oe:value 5.5.5.5
Netmask oe:value 255.255.254.0
Primary_DNS oe:value 1.1.1.1
Secondary_DNS oe:value 2.2.2.2

in this example there are six keywors present (Gateway, Hostname, IP_Address,Netmask,Primary_DNS,Seconday_DNS). sometimes user may fill only 4 fileds then there will be only four keywords.

what i am looking for is, if the keyword Gateway is present in the file test, append its corresponding value with the keyword (here Gateway=3.3.3.3 ) to test1 . Hostname is present, it should appned the value h ostname=test.test.com to test2 . Like that for all the keywords.

Expected output of test1 as per this example

 Gateway=3.3.3.3

Expected output of test2

 hostname=test.test.com

Expected output of test3

 IPADDR=5.5.5.5

Expected output of test4

 Netmask=255.255.254.0

Expected output of test5

 DNS1=1.1.1.1

Expected output of test6

 DNS2=2.2.2.2

Kindly help

Hello venkitesh,

If your Input_file is exactly same as shown Input_file and there is only 1 occurrence of string Gateway , then following may help you in same.

 awk '/Gateway/{A=1} A{print > "test"++i}'   Input_file
 

If you have any other requirements then kindly let us know all in all conditions with full details(with sample Input_file and expected output in code tags) please.

Thanks,
R. Singh

Hi Ravinder,

Thanks for your reply.

But this solution wont help for me.

as i mentioned in the question, there are six keywors present in this examplle (Gateway, Hostname, IP_Address,Netmask,Primary_DNS,Seconday_DNS). sometimes user may fill only 4 fileds then there will be only four keywors present in test. what i am looking for is if the keyword gateway is present anywhere in the file then append Gateway=3.3.3.3 in test1. like that for all keywords. if some keywords are not present it wont append anything

Kindly help

Hello venkitesh,

Could you please try following and let me know if this helps.
1st approach:

awk '/Gateway/{print > "test1";next} /Hostname/{print > "test2";next} /IP_Address/{print > "test3";next} /Netmask/{print > "test4";next} /Primary_DNS/{print > "test5";next} /Secondary_DNS/{print > "test6";next}'  Input_file

So above approach will create always line which have Gateway in it to file name test1 and so on always, if you want to append the data(let's say you have multiple line like you shown into your Input_file) then you could change > to >> to append it in above code.

2nd approach:

awk '/Gateway/{print > "test"++i;next} /Hostname/{print > "test"++i;next} /IP_Address/{print > "test"++i;next} /Netmask/{print > "test"++i;next} /Primary_DNS/{print > "test"++i;next} /Secondary_DNS/{print > "test"++i;next}' Input_file

If any string is NOT present in a Input_file then it will NOT increase the value of variable I, so let's say Gateway line is going to file named test1 and then Netmask should go to test3 BUT if in between IP_Address is not found then Netmask one will go to test2 then.

3rd approach:

awk '/Gateway/{i="";print >> "test"++i;next} /Hostname/{print >> "test"++i;next} /IP_Address/{print >> "test"++i;next} /Netmask/{print >> "test"++i;next} /Primary_DNS/{print >> "test"++i;next} /Secondary_DNS/{print >> "test"++i;next}'  Input_file

Let's say your Input_file have multiple occurrences of strings which you have mentioned above and you want to keep the files from test1 to test6 only so this code will help you to do so and it will keep appending the lines into their respective files.

I hope this helps you.

Thanks,
R. Singh

How about

split -dl1 test test
cf test*
test00:
Gateway oe:value 3.3.3.3
test01:
Hostname oe:value test.test.com
test02:
IP_Address oe:value 5.5.5.5
test03:
Netmask oe:value 255.255.254.0
test04:
Primary_DNS oe:value 1.1.1.1
test05:
Secondary_DNS oe:value 2.2.2.2

Hi Ravinder,

Thanks for providing the 3 approaches.

But the output what i am getting in test1 is

Hostname oe:value test.test.com

what i am looking for is

hostname=test.test.com

and for primary and secondary dns what i am getting in test4 and test5 is

Primary_DNS oe:value 192.168.0.1
Secondary_DNS oe:value 199.167.145.23

instead it would be great if it should append like this.

DNS1=192.168.0.1
DNS2=199.167.145.23

Thanks

Hello venkitesh,

Firstly you could THANK a person by using THANKS button on each post's LEFT most corner, secondly you should use code tags as per forum rules for sample Input_file or sample outputs. Could you please try following and let me know if this helps(I am taking my 3rd approach in previous post, if you need any other one you could try to edit it).

 awk '/Gateway/{i="";print "Gateway=" $NF >> "test"++i;next} /Hostname/{print "hostname=" $NF>> "test"++i;next} /IP_Address/{print "IP address="$NF >> "test"++i;next} /Netmask/{print "Netmask="$NF >> "test"++i;next} /Primary_DNS/{print "DNS1="$NF >> "test"++i;next} /Secondary_DNS/{print "DNS2="$NF>> "test"++i;next}'  Input_file
 

Thanks,
R. Singh

sed 's/ oe:value /=/' test | split -dl1 - test 
cf test*
test00:
Gateway=3.3.3.3
test01:
Hostname=test.test.com
test02:
IP_Address=5.5.5.5
test03:
Netmask=255.255.254.0
test04:
Primary_DNS=1.1.1.1
test05:
Secondary_DNS=2.2.2.2