-2

I have an xml file and I want to change one value on that file:

param name=swam_license_ports_overdraft_thresh

99

param name="strike_license_ports_overdraft_thresh"

99

The outcome I want is to sed the value 99 to 90 under param name=swam_license_ports_overdraft_thresh and the other value 99 under param name="strike_license_ports_overdraft_thresh" should not be changed.

Braiam
  • 35,991
Jason
  • 1
  • I updated your question a bit. I added "not" for the second 99, I hope you agree, otherwise, feel free to revert. – Bernhard Aug 15 '14 at 06:25
  • 2
    Is it always only the first occurrence you want to change, or always in the upper half, or always on line number 3? Can you maybe clarify this. – Bernhard Aug 15 '14 at 06:26
  • Hi @Bernhard I only want to change to value 99 to 90 on the first occurrence, and leave the rest as is. – Jason Aug 20 '14 at 12:24

3 Answers3

1

If I understand right, you want to remove only the first match, so you can do:

sed -e '/99/{s//XXX/;:a;n;ba}' file

With GNU sed, you can:

sed -e '0,/99/s//XXX/' file
cuonglm
  • 153,898
1

To remove the first match(preserve line)

awk '!x&&/99/{x++;$0=y}1' file

To remove the first match(dont preserve line)

awk '!x&&/99/{x++;next}1' file

To replace first match

awk '!x&&/99/{x++;$0="98"}1' file
0
sed -n 'h;/match/n;G;P;//D'

That will delete only the first match in any input file. It

  • overwrites hold space with every line

  • overwrites pattern space with the next input on match lines

  • Gets hold space appended to patern space

  • Prints up to the first occurring \newline character occurring in pattern space

  • Deletes same on match lines

The thing is though, that process results in every line following the first occurrence of match to also be match before it gets the next line. So it's always matching and never printing.

sed '/match/x;//x'

That one will replace the first occurring match in a file with a blank line.

  • on match lines sed exchanges hold and pattern spaces

  • if the new pattern space (old pattern space) also matches it exchanges them again

  • else it leaves the first occurring match in the new hold space and prints the blank pattern space

mikeserv
  • 58,310
  • hi thank you, I tried to do it, but i got no luck, this is how I execute it: sed -n 'h;/param name="swirec_license_ports_overdraft_thresh"/n;G;P;//D' – Jason Aug 21 '14 at 06:37
  • @Jason sed -n 'h;/swirec_license_ports_overdraft_thresh/n;G;P;//D' – mikeserv Aug 21 '14 at 14:48
  • Thank you sir, after I executed it, it only shows the whole content of the file, but nothing happened to the line I want to be replace – Jason Aug 22 '14 at 06:06
  • @Jason - can you post the file somewhere and give me a link to it? Or even just three of the sections you want edited and the expected output? I had a really hard time understanding the question - for which I apologize. But I really need the data so I can be sure. I'll help however I can. Could it be that another line in the file contained that content beforehand? – mikeserv Aug 22 '14 at 06:27