How to add properties in the end of the two first lines with double quote?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
2












We want to add the following properties:



-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M


in the two first lines that contain the word - HBASE_MASTER_OPTS



Example before change:



more ams-hbase-env-template.txt

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


Example after change (expected results):



export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M" 
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


What I did until now is that:



sed -i '/HBASE_MASTER_OPTS/ s/$/-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/' ams-hbase-env-template.txt


But this isn't right because:



  1. We want the properties before the end of double quote.


  2. Properties must be only on the first two lines that variable is HBASE_MASTER_OPTS.


  3. In case properties are already in line, then it will not append again on any next time!










share|improve this question



















  • 1




    Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again? And, if such a line is encountered, would it still count as being one of the first two lines?
    – Dirk Herrmann
    Aug 13 at 22:41






  • 1




    regarding to Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again - YES I MEAN THAT
    – yael
    Aug 13 at 22:49










  • @yael Can you update your example (before + after) so that your point 3 is covered? It would be helpful if your example would reflect all the points that you need on what you're asking... Thanks!
    – Filipe Brandenburger
    Aug 15 at 5:46














up vote
3
down vote

favorite
2












We want to add the following properties:



-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M


in the two first lines that contain the word - HBASE_MASTER_OPTS



Example before change:



more ams-hbase-env-template.txt

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


Example after change (expected results):



export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M" 
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


What I did until now is that:



sed -i '/HBASE_MASTER_OPTS/ s/$/-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/' ams-hbase-env-template.txt


But this isn't right because:



  1. We want the properties before the end of double quote.


  2. Properties must be only on the first two lines that variable is HBASE_MASTER_OPTS.


  3. In case properties are already in line, then it will not append again on any next time!










share|improve this question



















  • 1




    Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again? And, if such a line is encountered, would it still count as being one of the first two lines?
    – Dirk Herrmann
    Aug 13 at 22:41






  • 1




    regarding to Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again - YES I MEAN THAT
    – yael
    Aug 13 at 22:49










  • @yael Can you update your example (before + after) so that your point 3 is covered? It would be helpful if your example would reflect all the points that you need on what you're asking... Thanks!
    – Filipe Brandenburger
    Aug 15 at 5:46












up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





We want to add the following properties:



-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M


in the two first lines that contain the word - HBASE_MASTER_OPTS



Example before change:



more ams-hbase-env-template.txt

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


Example after change (expected results):



export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M" 
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


What I did until now is that:



sed -i '/HBASE_MASTER_OPTS/ s/$/-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/' ams-hbase-env-template.txt


But this isn't right because:



  1. We want the properties before the end of double quote.


  2. Properties must be only on the first two lines that variable is HBASE_MASTER_OPTS.


  3. In case properties are already in line, then it will not append again on any next time!










share|improve this question















We want to add the following properties:



-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M


in the two first lines that contain the word - HBASE_MASTER_OPTS



Example before change:



more ams-hbase-env-template.txt

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


Example after change (expected results):



export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M" 
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"


What I did until now is that:



sed -i '/HBASE_MASTER_OPTS/ s/$/-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/' ams-hbase-env-template.txt


But this isn't right because:



  1. We want the properties before the end of double quote.


  2. Properties must be only on the first two lines that variable is HBASE_MASTER_OPTS.


  3. In case properties are already in line, then it will not append again on any next time!







shell-script text-processing awk sed perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 20 at 13:30









GAD3R

22.8k154895




22.8k154895










asked Aug 13 at 18:54









yael

2,0341145




2,0341145







  • 1




    Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again? And, if such a line is encountered, would it still count as being one of the first two lines?
    – Dirk Herrmann
    Aug 13 at 22:41






  • 1




    regarding to Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again - YES I MEAN THAT
    – yael
    Aug 13 at 22:49










  • @yael Can you update your example (before + after) so that your point 3 is covered? It would be helpful if your example would reflect all the points that you need on what you're asking... Thanks!
    – Filipe Brandenburger
    Aug 15 at 5:46












  • 1




    Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again? And, if such a line is encountered, would it still count as being one of the first two lines?
    – Dirk Herrmann
    Aug 13 at 22:41






  • 1




    regarding to Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again - YES I MEAN THAT
    – yael
    Aug 13 at 22:49










  • @yael Can you update your example (before + after) so that your point 3 is covered? It would be helpful if your example would reflect all the points that you need on what you're asking... Thanks!
    – Filipe Brandenburger
    Aug 15 at 5:46







1




1




Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again? And, if such a line is encountered, would it still count as being one of the first two lines?
– Dirk Herrmann
Aug 13 at 22:41




Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again? And, if such a line is encountered, would it still count as being one of the first two lines?
– Dirk Herrmann
Aug 13 at 22:41




1




1




regarding to Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again - YES I MEAN THAT
– yael
Aug 13 at 22:49




regarding to Your point 3 is not fully clear to me: Do you mean that if a line contains HBASE_MASTER_OPTS, but also already contains the properties, you do not want to add them again - YES I MEAN THAT
– yael
Aug 13 at 22:49












@yael Can you update your example (before + after) so that your point 3 is covered? It would be helpful if your example would reflect all the points that you need on what you're asking... Thanks!
– Filipe Brandenburger
Aug 15 at 5:46




@yael Can you update your example (before + after) so that your point 3 is covered? It would be helpful if your example would reflect all the points that you need on what you're asking... Thanks!
– Filipe Brandenburger
Aug 15 at 5:46










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Extending on Steve's answer:



awk '/HBASE_MASTER_OPTS/&&/ -XX:[+]UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/x++;print;next/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Update: Here's a more maintenance friendly version, where every string only occurs once.



awk 'BEGINp="-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"m=0/HBASE_MASTER_OPTS/m=1;++xm&&index($0,p)print;nextm&&x<=2gsub(/"$/," " p """)1' ams-hbase-env-template.txt


And some explanation:



  • In the first version in the pattern matching, [+] is used for the literal + character, because otherwise + would be interpreted as special character.

The remaining points are about the second version:



  • In the BEGIN clause, the properties string p is defined. All the following clauses apply to the lines of the input.


  • m=0: This clause has no condition, which means it applies to every line, such that for each line first m (for match) will be set to zero.


  • /HBASE_MASTER_OPTS/m=1;++x: If a line matches the HBASE_MASTER_OPTS string, m is set and our line counter is incremented.


  • m&&index($0,p)print;next: this clause checks if, in addition to a match indicated by m, also the properties string is contained in the input line. In such a case index will return an non-zero value. If so, we print the line as it is (not adding the properties again) and with next skip the remaining clauses and start processing the next line. Note that the following clauses were originally provided by Steve and only adapted by me:


  • m&&x<=2gsub(/"$/," " p """): if we reach this point and we have a matching line and our counter has not exceeded 2, we modify $0 (the input line) by prepending the terminal " with a blank and our properties string. Apart from that, no output is produced, which will be done by the next clause.


  • 1: This is a condition (always true) without an action, which means the action defaults to printing the input line.





share|improve this answer






















  • it still append the properties even if they are exist in the lines
    – yael
    Aug 13 at 22:56










  • ok I will wait for the fix
    – yael
    Aug 13 at 23:03










  • what to say , wonderful really ,
    – yael
    Aug 13 at 23:23

















up vote
3
down vote













How about this awk solution. Creates the desired output in the question.



awk '/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Try it online!






share|improve this answer




















  • one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
    – yael
    Aug 13 at 22:48










  • so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
    – yael
    Aug 13 at 22:50










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462371%2fhow-to-add-properties-in-the-end-of-the-two-first-lines-with-double-quote%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Extending on Steve's answer:



awk '/HBASE_MASTER_OPTS/&&/ -XX:[+]UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/x++;print;next/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Update: Here's a more maintenance friendly version, where every string only occurs once.



awk 'BEGINp="-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"m=0/HBASE_MASTER_OPTS/m=1;++xm&&index($0,p)print;nextm&&x<=2gsub(/"$/," " p """)1' ams-hbase-env-template.txt


And some explanation:



  • In the first version in the pattern matching, [+] is used for the literal + character, because otherwise + would be interpreted as special character.

The remaining points are about the second version:



  • In the BEGIN clause, the properties string p is defined. All the following clauses apply to the lines of the input.


  • m=0: This clause has no condition, which means it applies to every line, such that for each line first m (for match) will be set to zero.


  • /HBASE_MASTER_OPTS/m=1;++x: If a line matches the HBASE_MASTER_OPTS string, m is set and our line counter is incremented.


  • m&&index($0,p)print;next: this clause checks if, in addition to a match indicated by m, also the properties string is contained in the input line. In such a case index will return an non-zero value. If so, we print the line as it is (not adding the properties again) and with next skip the remaining clauses and start processing the next line. Note that the following clauses were originally provided by Steve and only adapted by me:


  • m&&x<=2gsub(/"$/," " p """): if we reach this point and we have a matching line and our counter has not exceeded 2, we modify $0 (the input line) by prepending the terminal " with a blank and our properties string. Apart from that, no output is produced, which will be done by the next clause.


  • 1: This is a condition (always true) without an action, which means the action defaults to printing the input line.





share|improve this answer






















  • it still append the properties even if they are exist in the lines
    – yael
    Aug 13 at 22:56










  • ok I will wait for the fix
    – yael
    Aug 13 at 23:03










  • what to say , wonderful really ,
    – yael
    Aug 13 at 23:23














up vote
2
down vote



accepted










Extending on Steve's answer:



awk '/HBASE_MASTER_OPTS/&&/ -XX:[+]UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/x++;print;next/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Update: Here's a more maintenance friendly version, where every string only occurs once.



awk 'BEGINp="-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"m=0/HBASE_MASTER_OPTS/m=1;++xm&&index($0,p)print;nextm&&x<=2gsub(/"$/," " p """)1' ams-hbase-env-template.txt


And some explanation:



  • In the first version in the pattern matching, [+] is used for the literal + character, because otherwise + would be interpreted as special character.

The remaining points are about the second version:



  • In the BEGIN clause, the properties string p is defined. All the following clauses apply to the lines of the input.


  • m=0: This clause has no condition, which means it applies to every line, such that for each line first m (for match) will be set to zero.


  • /HBASE_MASTER_OPTS/m=1;++x: If a line matches the HBASE_MASTER_OPTS string, m is set and our line counter is incremented.


  • m&&index($0,p)print;next: this clause checks if, in addition to a match indicated by m, also the properties string is contained in the input line. In such a case index will return an non-zero value. If so, we print the line as it is (not adding the properties again) and with next skip the remaining clauses and start processing the next line. Note that the following clauses were originally provided by Steve and only adapted by me:


  • m&&x<=2gsub(/"$/," " p """): if we reach this point and we have a matching line and our counter has not exceeded 2, we modify $0 (the input line) by prepending the terminal " with a blank and our properties string. Apart from that, no output is produced, which will be done by the next clause.


  • 1: This is a condition (always true) without an action, which means the action defaults to printing the input line.





share|improve this answer






















  • it still append the properties even if they are exist in the lines
    – yael
    Aug 13 at 22:56










  • ok I will wait for the fix
    – yael
    Aug 13 at 23:03










  • what to say , wonderful really ,
    – yael
    Aug 13 at 23:23












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Extending on Steve's answer:



awk '/HBASE_MASTER_OPTS/&&/ -XX:[+]UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/x++;print;next/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Update: Here's a more maintenance friendly version, where every string only occurs once.



awk 'BEGINp="-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"m=0/HBASE_MASTER_OPTS/m=1;++xm&&index($0,p)print;nextm&&x<=2gsub(/"$/," " p """)1' ams-hbase-env-template.txt


And some explanation:



  • In the first version in the pattern matching, [+] is used for the literal + character, because otherwise + would be interpreted as special character.

The remaining points are about the second version:



  • In the BEGIN clause, the properties string p is defined. All the following clauses apply to the lines of the input.


  • m=0: This clause has no condition, which means it applies to every line, such that for each line first m (for match) will be set to zero.


  • /HBASE_MASTER_OPTS/m=1;++x: If a line matches the HBASE_MASTER_OPTS string, m is set and our line counter is incremented.


  • m&&index($0,p)print;next: this clause checks if, in addition to a match indicated by m, also the properties string is contained in the input line. In such a case index will return an non-zero value. If so, we print the line as it is (not adding the properties again) and with next skip the remaining clauses and start processing the next line. Note that the following clauses were originally provided by Steve and only adapted by me:


  • m&&x<=2gsub(/"$/," " p """): if we reach this point and we have a matching line and our counter has not exceeded 2, we modify $0 (the input line) by prepending the terminal " with a blank and our properties string. Apart from that, no output is produced, which will be done by the next clause.


  • 1: This is a condition (always true) without an action, which means the action defaults to printing the input line.





share|improve this answer














Extending on Steve's answer:



awk '/HBASE_MASTER_OPTS/&&/ -XX:[+]UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/x++;print;next/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Update: Here's a more maintenance friendly version, where every string only occurs once.



awk 'BEGINp="-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"m=0/HBASE_MASTER_OPTS/m=1;++xm&&index($0,p)print;nextm&&x<=2gsub(/"$/," " p """)1' ams-hbase-env-template.txt


And some explanation:



  • In the first version in the pattern matching, [+] is used for the literal + character, because otherwise + would be interpreted as special character.

The remaining points are about the second version:



  • In the BEGIN clause, the properties string p is defined. All the following clauses apply to the lines of the input.


  • m=0: This clause has no condition, which means it applies to every line, such that for each line first m (for match) will be set to zero.


  • /HBASE_MASTER_OPTS/m=1;++x: If a line matches the HBASE_MASTER_OPTS string, m is set and our line counter is incremented.


  • m&&index($0,p)print;next: this clause checks if, in addition to a match indicated by m, also the properties string is contained in the input line. In such a case index will return an non-zero value. If so, we print the line as it is (not adding the properties again) and with next skip the remaining clauses and start processing the next line. Note that the following clauses were originally provided by Steve and only adapted by me:


  • m&&x<=2gsub(/"$/," " p """): if we reach this point and we have a matching line and our counter has not exceeded 2, we modify $0 (the input line) by prepending the terminal " with a blank and our properties string. Apart from that, no output is produced, which will be done by the next clause.


  • 1: This is a condition (always true) without an action, which means the action defaults to printing the input line.






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 14 at 20:43

























answered Aug 13 at 22:52









Dirk Herrmann

31138




31138











  • it still append the properties even if they are exist in the lines
    – yael
    Aug 13 at 22:56










  • ok I will wait for the fix
    – yael
    Aug 13 at 23:03










  • what to say , wonderful really ,
    – yael
    Aug 13 at 23:23
















  • it still append the properties even if they are exist in the lines
    – yael
    Aug 13 at 22:56










  • ok I will wait for the fix
    – yael
    Aug 13 at 23:03










  • what to say , wonderful really ,
    – yael
    Aug 13 at 23:23















it still append the properties even if they are exist in the lines
– yael
Aug 13 at 22:56




it still append the properties even if they are exist in the lines
– yael
Aug 13 at 22:56












ok I will wait for the fix
– yael
Aug 13 at 23:03




ok I will wait for the fix
– yael
Aug 13 at 23:03












what to say , wonderful really ,
– yael
Aug 13 at 23:23




what to say , wonderful really ,
– yael
Aug 13 at 23:23












up vote
3
down vote













How about this awk solution. Creates the desired output in the question.



awk '/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Try it online!






share|improve this answer




















  • one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
    – yael
    Aug 13 at 22:48










  • so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
    – yael
    Aug 13 at 22:50














up vote
3
down vote













How about this awk solution. Creates the desired output in the question.



awk '/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Try it online!






share|improve this answer




















  • one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
    – yael
    Aug 13 at 22:48










  • so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
    – yael
    Aug 13 at 22:50












up vote
3
down vote










up vote
3
down vote









How about this awk solution. Creates the desired output in the question.



awk '/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Try it online!






share|improve this answer












How about this awk solution. Creates the desired output in the question.



awk '/HBASE_MASTER_OPTS/&&x<2gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"");x++1' ams-hbase-env-template.txt


Try it online!







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 13 at 22:24









steve

12.9k22149




12.9k22149











  • one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
    – yael
    Aug 13 at 22:48










  • so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
    – yael
    Aug 13 at 22:50
















  • one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
    – yael
    Aug 13 at 22:48










  • so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
    – yael
    Aug 13 at 22:50















one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
– yael
Aug 13 at 22:48




one problem is that when we run your CLI , let say we put the output to out.txt , on the next time awk will append the lines , so actually we get duplicate properties
– yael
Aug 13 at 22:48












so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
– yael
Aug 13 at 22:50




so can you fix the awk syntax , so on the next awk running it will not append the properties because they are already exists
– yael
Aug 13 at 22:50

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462371%2fhow-to-add-properties-in-the-end-of-the-two-first-lines-with-double-quote%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay