Bash append values if keywords are present in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash append values if keywords are present in the file
# 1  
Old 03-21-2017
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

Code:
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 hostname=test.test.com to test2. Like that for all the keywords.

Expected output of test1 as per this example
Code:
 Gateway=3.3.3.3

Expected output of test2
Code:
 hostname=test.test.com

Expected output of test3
Code:
 IPADDR=5.5.5.5

Expected output of test4
Code:
 Netmask=255.255.254.0

Expected output of test5
Code:
 DNS1=1.1.1.1

Expected output of test6
Code:
 DNS2=2.2.2.2

Kindly help



Moderator's Comments:
Mod Comment For clarity's sake: Please use CODE tags structuredly (not blockwise where inapplicable) as required by forum rules!

Last edited by RudiC; 03-21-2017 at 06:11 AM.. Reason: Changed CODE tags.
# 2  
Old 03-21-2017
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.
Code:
 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
# 3  
Old 03-21-2017
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
# 4  
Old 03-21-2017
Hello venkitesh,

Could you please try following and let me know if this helps.
1st approach:
Code:
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:
Code:
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:
Code:
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
# 5  
Old 03-21-2017
How about
Code:
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

# 6  
Old 03-21-2017
Hi Ravinder,

Thanks for providing the 3 approaches.

But the output what i am getting in test1 is

Code:
Hostname oe:value test.test.com

what i am looking for is
Code:
hostname=test.test.com

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

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

Code:
DNS1=192.168.0.1
DNS2=199.167.145.23

Thanks

Last edited by venkitesh; 03-21-2017 at 08:37 AM..
# 7  
Old 03-21-2017
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).
Code:
 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to append array value to file before copying

The bash stores each uniqueid in an array and then passes them to %q to get the unique path. That seems to work what I am having trouble with is renaming each .png with the unique value in %q. I thought it was working but upon closer inspection, a .png file is being sent to scp.... but only 1 and... (21 Replies)
Discussion started by: cmccabe
21 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

3. Shell Programming and Scripting

Ftp with bash, append file where left off

I'm working on a bash script to finish uploading a file. I need a way to get $filesize so that "restart $filesize" will work. Here is my script: ftp -n -v <<END_SCRIPT open ftp.$domain user $user@$domain $password size $file restart $filesize put $file quit END_SCRIPTWayne Sallee... (9 Replies)
Discussion started by: WayneSallee
9 Replies

4. Shell Programming and Scripting

Search files in directory for keywords using bash

I have ~100 text files in a directory that I am trying to parse and output to a new file. I am looking for the words chr,start,stop,ref,alt in each of the files. Those fields should appear somewhere in those files. The first two fields of each new set of rows is also printed. Since this is on a... (7 Replies)
Discussion started by: cmccabe
7 Replies

5. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

6. Shell Programming and Scripting

Match value in column and append file with new values

Hi, I need help to match two files based on two columns. file_1 ID AA An Ca Ele Pro Su Ot Tra g13950 No No Yes No Yes Yes Yes Yes g05760 Yes No No No No Yes Yes Yes g12640 No No No No No No No No k17720 No Yes No No No No No Yes g05640 Yes Yes Yes No No Yes Yes Yes file_2 ... (8 Replies)
Discussion started by: redse171
8 Replies

7. Shell Programming and Scripting

Find keywords, and append at the end of line

Task: Find keywords in each line, and append at the end of line; if not found in the line, do nothing. the code is wrong. how to make it work. thanks a lot. cat keywords.txt | while read line; do awk -F"|" '{if (/$line/) {print $0"$line , ";} else print;}' outfile.txt > tmp ... (9 Replies)
Discussion started by: dtdt
9 Replies

8. UNIX for Dummies Questions & Answers

Passing values from file into array in Bash

Hi, I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column. For example: Sample file "file1.txt": 1 name1 a first 2 name2 b second 3 name3 c third and have arrays such as: line1 = ( "1" "name1" "a"... (3 Replies)
Discussion started by: ShiGua
3 Replies

9. Shell Programming and Scripting

Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example ... (13 Replies)
Discussion started by: adb
13 Replies

10. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies
Login or Register to Ask a Question