Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

👤 Login to reply

 
Thread Tools Search this Thread
# 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 to reply

« Previous Thread | Next Thread »
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
How to print the specific part of the file name with file creation date? sadique.manzar Shell Programming and Scripting 7 01-18-2018 09:41 AM
Save output of updated csv file as csv file itself, part 2 refrain Shell Programming and Scripting 2 04-11-2015 06:17 AM
Help in UNIX shell to copy part of file name to new file name lnemitz Shell Programming and Scripting 4 01-11-2014 09:26 PM
Taking part of one file name and putting it into a another file name jclanc8 Shell Programming and Scripting 2 05-16-2013 12:35 PM
[Solved] Printing a part of the last line of the specific part of a file wenclu Shell Programming and Scripting 6 09-04-2012 08:34 AM
Removing part of a file name and appending into a single file varlax Shell Programming and Scripting 8 05-04-2012 05:15 PM
Making replicates of a file with part of a line randomized for each file Scatterbrain26 UNIX for Dummies Questions & Answers 1 08-10-2011 05:55 PM
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
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 01:41 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:43 PM.

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