Edit and replace the multiple values in a file in one iteration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit and replace the multiple values in a file in one iteration
# 1  
Old 03-23-2018
Edit and replace the multiple values in a file in one iteration

Hi All,

I am preserving OLD and NEW values and want to replace the values in one go instead of using multiple sed and mv commands. Please help.

Code:
echo "\nEnter the new qStart time '${CODE}' - (Hit Enter for No Change): \c"
read NEW
echo "\nEnter the new qStop time '${CODE}' - (Hit Enter for No Change): \c"
read NEW1
echo "\nEnter the new tStart time '${CODE}' - (Hit Enter for No Change): \c"
read NEW2
echo "\nEnter the new tStop time  '${CODE}' - (Hit Enter for No Change): \c"
read NEW3

After reading the values, doing additional checks and replacing starts at below code. Please suggest how I can do it one go instead of opening the file muultiple times and moving it across.

Code:
sed s/'\(.*.exch."${CODE}".qStart.*\)"${CURRENT}"\(.*read\)/\1"${NEW}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

sed s/'\(.*.exch."${CODE}".qStop.*\)"${CURRENT1}"\(.*read\)/\1"${NEW1}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

sed s/'\(.*Delay."${CODE}".tStart.*\)"${CURRENT2}"\(.*read\)/\1"${NEW2}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

sed s/'\(.*Delay."${CODE}".tStop.*\)"${CURRENT3}"\(.*read\)/\1"${NEW3}"\2'/g $LOCK_FILE > $LOCK_FILE.new
mv $LOCK_FILE.new $LOCK_FILE

# 2  
Old 03-23-2018
You can cram all the sed commands into one script:
Code:
sed 's/.../.../g; s/.../.../g; s/.../.../g' file

Are those back references really necessary, e.g. to discriminate possible duplicates? If not, try
Code:
sed "s/${CURRENT}/${NEW}/g; s/${CURRENT1}/${NEW1}/g; s/${CURRENT2}/${NEW2}/g; s/${CURRENT3}/${NEW3}/g;' $LOCK_FILE

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 03-23-2018
Thank you RudiC
Actually, I was trying to read the line in the file and replacing the 2nd column with the new value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

2. Shell Programming and Scripting

Multiple columns replace with null values.

I am trying to replace the partcular columns(Col3,col5,col20,col44,col55,co56,col59,col60,col61,col62,col74,col75,col88,col90,col91,col93,col94,col95) with empty Input file Col1,col2,col3,col4,col5------,col100 1,2,3,4,5,---------,100 3,4,5,6,7,---------,300 Output : ... (3 Replies)
Discussion started by: onesuri
3 Replies

3. Shell Programming and Scripting

Need Help to Edit multiple column of a file

Hello Team, I want to know if there is any one liner command , using which I can edit multiple column of a file. input file input.txt (comma separated), taran, 12.45, uttam, 23.40, babay karan, 12.45, raju, 11.40, rahulg I want to update, 2nd and 4th column, but want all those column... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

6. Shell Programming and Scripting

Retrieve multiple values for each iteration

Hi All, I tried to retrieve multiple values using for/foreach loop in each iteration. But couldn't get it. Please help me. Below are my try outs: #!/bin/ksh foreach {i,j,k} {5150 5540 5149 5640 5151 5920 5362 5965 5147 5895 5061} echo $i,$j,$k end Error: ./mulcns.ksh: foreach: not... (4 Replies)
Discussion started by: cns1710
4 Replies

7. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

8. Shell Programming and Scripting

Search for multiple string and replace with respective values

Hi, Can anyone help me to search for multiple strings within specified position and replace with respective string value. For example I need to search the string from the position 11 to 20 and if it contain ABC and then replace it by BCDEFGHIJ ... find AABZSDJIK and replace with QWE. and... (4 Replies)
Discussion started by: zooby
4 Replies

9. Shell Programming and Scripting

How to edit file sections that cross multiple lines?

Hello, I'm wondering where I could go to learn how to edit file sections that cross multiple lines. I'm wanting to write scripts that will add Gnome menu entries for all users on a system for scripts I write, etc. I can search an replace simple examples with sed, but this seems more complex. ... (8 Replies)
Discussion started by: Narnie
8 Replies

10. AIX

Locking a file when using VI to prevent multiple-edit sessions by diff users

At the office, we often have to edit one file with VI. We are 4-6 workers doing it and sometimes can be done at the same time. We have found a problem and want to prevent it with a file lock. Is it possible and how ? problem : Worker-a starts edit VI session on File-A at 1PM Worker-b... (14 Replies)
Discussion started by: Browser_ice
14 Replies
Login or Register to Ask a Question