0

This question already has an answer here:

I want to replace create_test in the file with #create_test if the next line contains psfxxx_16_pi. How can I do it with sed for the whole file containing this?

Input file content:

create_test -type hard -outer { 1.0000 } { \
    psfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } { \
    psfxxx_16_pi/psfop/deadline_south_re_1 }

Output file:

#create_test -type hard -outer { 1.0000 } { \
    psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } { \
    psfxxx_16_pi/psfop/deadline_south_re_1 }

marked as duplicate by don_crissti, Jeff Schaller, Isaac, RalfFriedl, Romeo Ninov Nov 17 '18 at 5:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I can do it with perl, but I am looking for a compact one liner command using sed or awk. Thank you Mr. Shunz for the format. – mandrake00 Nov 16 '18 at 9:18
  • May be helpful? unix.stackexchange.com/questions/466798/… – Sparhawk Nov 16 '18 at 9:24
  • I'm always curious when I see requests for one-liners; what's wrong with a perl solution? – Jeff Schaller Nov 16 '18 at 17:02
2

Try

sed '/^create_test/ {N; /psfxxx/ s/^/#/}' file
#create_test -type hard -outer { 1.0000 } { \
    psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } { \
    psfxxx_16_pi/psfop/deadline_south_re_1 }

create_test -type hard -outer { 1.0000 } { \
    vsfxxx_16_pi/psfop/deadline_north_re_0 }

create_test -type hard -outer { 0.0000 } { \
    vsfxxx_16_pi/psfop/deadline_south_re_1 }

When encountering "create_test" it appends the next line, and if that contains "psfxxx", it prefixes the "#".

  • Thank you RudiC. I tried. It replaces only the first occurance in the input text file. output: #create_test -type hard -outer { 1.0000 } { \ psfxxx_16_pi/psfop/deadline_north_re_0 } create_ test -type hard -outer { 0.0000 } { \ psfxxx_16_pi/psfop/deadline_south_re_1 } – mandrake00 Nov 16 '18 at 9:32
  • Hi! RudiC. Your fix works. Great! my input file had an issue. It had create_ in one line and test... in the next. Fixed it and it's working fine now. You are a genius. – mandrake00 Nov 16 '18 at 9:43
  • You can see in my sample output that it replaces on matching records, and doesn't if no match. What's different in your input file? Hoppla - cross post. – RudiC Nov 16 '18 at 9:44
0

Another sed method:

$ sed -zE 's/create_test([^\n]*\n[^\n]*psfxxx_16_pi)/#create_test\1/g' file
#create_test -type hard -outer { 1.0000 } { \
    psfxxx_16_pi/psfop/deadline_north_re_0 }

#create_test -type hard -outer { 0.0000 } { \
    psfxxx_16_pi/psfop/deadline_south_re_1 
0

If awk will do:

awk '/psfxxx_16_pi/{prev = "#" prev} {print prev} {prev = $0} END {print}'

Here, I print the previous line (saved in prev) for each line, and save $0 for the next iteration in prev. If the line matches, I add # to prev. At the end, print the last line.

Not the answer you're looking for? Browse other questions tagged or ask your own question.