Hi I'm trying to work out how to select a line in a text file from a pattern/word string, and then replace that line with new text with an incrementing number for each line changed.
I was planning on using sed, but have no idea how to implement the increasing number for every line changed.
The aim, before and after:
Text before:
">text_1_lots of other bits of text"
other lines of text
">text_2_lots of other bits of text"
other lines of text
">text_3_lots of other bits of text"
other lines of text
Text after:
">text_1"
other lines of text
">text_2"
other lines of text
">text_3"
other lines of text
This is what I have so far:
sed -i "s/>text_*/>text_[0-n]/g"
I realise this doesn't work as the [0-n]
and wildcard *
are just representing what I'm trying to do, and are the closest things that I can think of to achieve that (even though I understand this is essentially pseudo code at the moment)
>text_[number_here]
(so keep the number but remove the rest ? – don_crissti Jul 08 '16 at 21:10sed -E 's/(>text_[0-9]{1,}).*/\1/' file
– steeldriver Jul 08 '16 at 21:13