Read file and add it into part of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file and add it into part of file
# 1  
Old 04-18-2018
Read file and add it into part of file

Hello
let me explain senario.
there is a file which name is config and it store main software variable:

file main.conf contents:
Code:
update="1"
log_login="0"
allow_ports=""
deny_ports="21,22,23"

and there is a file which name is ports.txt
file ports.txt contents:
Code:
25,26,27

i want to write script, when it run, import all data from ports.txt into main.conf but between "" in allow_ports and save this new file as finall.conf
so finall.conf should be like this:

Code:
update="1"
log_login="0"
allow_ports="25,26,27"
deny_ports="21,22,23"

ive test several ways like use source cat ports.txt and more.
# 2  
Old 04-18-2018
one way:
Code:
awk -v qq='"' 'FNR==NR {f2=$0;next} /^allow_ports=/{sub(qq qq, qq f2 qq)}1' ports.txt main.conf

# 3  
Old 04-18-2018
Try also
Code:
sed '1 {h; d; n; }; /allow_ports/ { G; s/""\|$/"/g; s/\n//g; }' ports.txt main.conf
update="1"
log_login="0"
allow_ports="25,26,27"
deny_ports="21,22,23"

# 4  
Old 04-19-2018
Code:
allow_ports=$(< ports.txt)

while read line
do
   echo "$line" | grep -q "^allow_ports=" && line="allow_ports=\"$allow_ports\""
   echo "$line"
done < main.conf > finall.conf

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

How to print the specific part of the file name with file creation date?

Hello Folks, I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern. Filename: MobileProtocol.20171228T154200.157115.udr I want to get the count of files created on each day based on a pattern find. find . -type... (7 Replies)
Discussion started by: sadique.manzar
7 Replies

3. Shell Programming and Scripting

Read a file, add some text and send an email

Hi, If I am asking this question, you must have already figured out , that I am new to Unix, so here it goes I was trying to read a file, add some user defined content to it and send out an email , I did find out a way to achieve this, but looking at the code, it looks a bit crude to me, can... (3 Replies)
Discussion started by: karthikbhuvana
3 Replies

4. Programming

Read file and add value

i have a file outfile.txt which contain 12 22 i have written this program to read the file and show the output,but i dont know how to add these value and show the total. my-codes are #include<cmath> #include<cstdlib> #include<iostream> #include<fstream> using namespace std; int main ()... (4 Replies)
Discussion started by: console
4 Replies

5. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Shell Programming and Scripting

To Read a File and Insert a part of the lines into the database

Hi Guys I need to have a shell script which reads a log file and insert a part of each line into the database. Some sample lines in the file are as below. 20091112091359 MED_RQACK : user_data=60173054304,100232120,20091112091359,;ask_status=0;ask_reason=OK;msg_id=20091112091319... (5 Replies)
Discussion started by: Somanadh
5 Replies

8. Shell Programming and Scripting

I'm trying to read from a file and add to LDAP

Hi all I'm trying to write a script that reads from a file and then insert the record by a command line tool into LDAP. here is the file format FirstName LastName uid password and here is the script that I wrot: #!/bin/sh INPUT='/export/home/user/input' while read fname lname uid pass... (0 Replies)
Discussion started by: sh_ksa
0 Replies

9. Shell Programming and Scripting

Add Date as part of file name

How can I add date in format yyyymmdd to my existing file name ? For example my file name is test.dat then i want it to be test.datyyyymmdd (3 Replies)
Discussion started by: amsh76
3 Replies

10. UNIX for Dummies Questions & Answers

read in part of the file into another file

Hi, I justs started learning Unix on my own. I have a question: What command can I use when I need to read in part of the file into another file? I remember I saw it somewhere but I don't know what it is. Thanks (3 Replies)
Discussion started by: whatisthis
3 Replies
Login or Register to Ask a Question