Unix/Linux Go Back    


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

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-18-2018   -   Original Discussion by nimafire
nimafire's Unix or Linux Image
nimafire nimafire is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 18 April 2018, 1:40 PM EDT
Posts: 60
Thanks: 17
Thanked 0 Times in 0 Posts
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 Unix and Linux 04-18-2018   -   Original Discussion by nimafire
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 June 2018, 3:11 PM EDT
Location: Foxborough, MA
Posts: 8,299
Thanks: 249
Thanked 833 Times in 778 Posts
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 Unix and Linux 04-18-2018   -   Original Discussion by nimafire
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 June 2018, 6:40 AM EDT
Location: Aachen, Germany
Posts: 12,922
Thanks: 429
Thanked 3,969 Times in 3,650 Posts
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 Unix and Linux 04-19-2018   -   Original Discussion by nimafire
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 31 May 2018, 9:41 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts


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
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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:06 PM.