Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Read file and add it into part of file

👤 Login to reply

    #1  
Old 04-18-2018
nimafire nimafire is offline
Registered User
 
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.
Sponsored Links
    #2  
Old 04-18-2018
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
one way:
Code:
awk -v qq='"' 'FNR==NR {f2=$0;next} /^allow_ports=/{sub(qq qq, qq f2 qq)}1' ports.txt main.conf

Sponsored Links
    #3  
Old 04-18-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
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
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
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

Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read file and add value console Programming 4 12-06-2012 10:47 AM
Read file and for each line replace two variables, add strings and save output in another file Andy_ARG Shell Programming and Scripting 5 03-03-2011 12:25 AM
To Read a File and Insert a part of the lines into the database Somanadh Shell Programming and Scripting 5 11-16-2009 06:04 AM
Add Date as part of file name amsh76 Shell Programming and Scripting 3 09-05-2005 09:18 PM
read in part of the file into another file whatisthis UNIX for Dummies Questions & Answers 3 09-02-2004 05:09 PM



All times are GMT -4. The time now is 04:17 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?