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 10:41 AM
Help in UNIX shell to copy part of file name to new file name lnemitz Shell Programming and Scripting 4 01-11-2014 10:26 PM
Read a file, add some text and send an email karthikbhuvana Shell Programming and Scripting 3 04-13-2013 12:41 AM
Read file and add value console Programming 4 12-06-2012 11:47 AM
[Solved] Printing a part of the last line of the specific part of a file wenclu Shell Programming and Scripting 6 09-04-2012 09:34 AM
Removing part of a file name and appending into a single file varlax Shell Programming and Scripting 8 05-04-2012 06: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 06:55 PM
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 01: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 07:04 AM
Read a file and search a value in another file create third file using AWK King Kalyan Shell Programming and Scripting 11 06-19-2009 01:05 AM
Need help with awk - how to read a content of a file from every file from file list tanit Shell Programming and Scripting 7 03-10-2009 06:19 AM
I'm trying to read from a file and add to LDAP sh_ksa Shell Programming and Scripting 0 09-10-2005 08:52 AM
Add Date as part of file name amsh76 Shell Programming and Scripting 3 09-05-2005 10:18 PM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 02:41 PM
read in part of the file into another file whatisthis UNIX for Dummies Questions & Answers 3 09-02-2004 06:09 PM


All times are GMT -4. The time now is 06:32 AM.

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