Bash script to replace text file from a lookup file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to replace text file from a lookup file
# 1  
Old 03-22-2015
Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script.

My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK.

Now, I need to update certain other files depending on what computer was selected.I have created an example scenario below.
I have a file called mainfile.txt and a lookup file called lookupfile.txt.
If I run my script and chose the option for Computer A, a variable is set in my script, e.g. $ComputerA

What I now need is for the ‘DomainName=’ in my mainfile.txt to update with the full domain name of ComputerA that’s detailed in the lookupfile.txt

Lookupfile.txt
Code:
ComputerAcomputerA.testdomain.com192.168.1.1
ComputerBcomputerB.testdomain.com192.168.2.1
ComputerCcomputerC.testdomain.com192.168.3.1

Mainfile.txt
Code:
DomainName=

From researching the issue I believe I can use awk or sed.However, I have had difficulty finding examples like I to get it working.
Can anyone provide a solution (and possibly an explanation of the solution).I can re-format my lookup.txt file if required Many thanks.

Last edited by Don Cragun; 03-22-2015 at 04:55 PM.. Reason: Get rid of dozens of font and size tags; add CODE tags.
# 2  
Old 03-22-2015
Please use code tags as required by forum rules!
Did you consider searching these fora for examples?

Your lookup file doesn't have any field separators, so a translation might run into trouble.
Given your table had <TAB> field separators,
Code:
grep $C file | cut -f2
computerA.testdomain.com

could do what you want. Use command substitution to assign to a variable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash lookup matching digits for secong file

In the bash below the user selects the file to be used. The digits of each file are unique and used to automatically locate the next file to be used in the process. The problem I can not seem to fix is that the full path needs to be referenced in the second portion and it is not currently. Is... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

3. Shell Programming and Scripting

Hostname lookup and create text file

I have a list of hostnames in a txt file . I need to do nslookup or other command on linux and get the ip address and if you dont find an ip address then put 0.0.0.0 instead in the output text file along with the hostname. So input host1 host2 host2.dd.ddd.net Output host1,... (2 Replies)
Discussion started by: gubbu
2 Replies

4. Shell Programming and Scripting

File field to replace lookup from another file

Hi All, I don't know how to fast do this field replace that need lookup from another file to form the update result:confused: I want to do it by general shell script Can anyone help to solve it ? Thanks for your kindly reply in advance. CK (0 Replies)
Discussion started by: ckwong99
0 Replies

5. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

6. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

7. Shell Programming and Scripting

Bash Script - Removing Year and Text from File Name

Hi, I have files with names in the following naming pattern, Name.of.moviemoretext-moretext.mov or Name of moviemoretext-moretext.mov And I would like to delete the and moretext-moretext leaving just the Name of movie. The Name of movie will always be different and the year will... (4 Replies)
Discussion started by: Monkey Dean
4 Replies

8. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies

9. UNIX for Dummies Questions & Answers

getting particular text after grep from lookup file

Hi I have two files a1 and b1 a1 has the following job names ab cd ef b1 has the following job details /*----------- ji -----------------*/ asdasd fgd saas dfdf asas fd gfg /*---------- ab ----------------*/ ara jhk dfhk asjla condition: s(abc_wf_hi) (10 Replies)
Discussion started by: napolayan
10 Replies

10. Shell Programming and Scripting

script - replace text in file

I have a file something like this (except much larger amount of rows/data) 0001 blue testing1 0002 blue testing2 0006 blue testing3 0232 red testing4 2143 blue testing5 3453 blue testing6 In a script I want to replace the word red with blue where the line begins with a... (4 Replies)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question