1

I am looking for an option in VI editor to display only the lines that match the particular string. After listing the lines, I would like to edit the word and that should reflect in the original file.

Eg:This is how my file is:

AAA1

X

Y

Z

AAA3

Z

Y

AB

AAA5

AAA8

I wish to change the sequence of AAA..like this

AAA1

X

Y

Z

AAA2

Z

Y

AB

AAA3

AAA4

I wish them to be in sequence. So, if I search and separate the lines on which AAA is there, I can edit that particular list.

I used vim /AAAA/g % | copen it opens all the lines which match the pattern AAAA. But when I edit them it is not being reflected in the original file.

  • you should use replace in vim : mode. use :%s/<matching pattern>/<replace string>/g – Raghvendra Feb 20 '17 at 9:45
  • You can use this for more details unix.stackexchange.com/questions/247329/… – Raghvendra Feb 20 '17 at 9:46
  • So, are you using vi or vim? They are not the same, nor they support the same options. If you are using vim, the answers here should do it. Otherwise, please specify in as much detail possible what editor, what version, OS, etc. – schaiba Feb 20 '17 at 9:57
  • Hi Schaiba, I am using vi editor. I updated my question. Thank you – Punith Kumar Feb 20 '17 at 14:14
0

You should use replace in vim for replacing particular match.

In this case use this pettern match.

:%s/<matching_pattern>/<replace_string>/g

This will match all the elements in the file with the specified replacing string.

  • 1
    Thank you for responding !! but replacing string and matching patterns are not fixed here replacing_string is not based on matching_pattern. There are AAAA001, AAAA002, AAAA003, and they are not in sequence, I would like to write them in sequence. – Punith Kumar Feb 20 '17 at 10:15
  • you can use sort method within vim to sort the element in ascending or descending order before performing replace method – Raghvendra Feb 20 '17 at 10:19
  • for sorting in vim you can use :%!sort -u – Raghvendra Feb 20 '17 at 10:20
0

I think you need something like this:

:let i=1 | g/^AAA[0-9]\+/s//\='AAA'.i/ | let i=i+1

You can read about "Substitute with ascending numbers"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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