2

I want to add ; in the end of lines in a txt file containing ddl for multiple tables.

For example :

LOCATION:
 'hdfs://HDP**/apps/hive/warehouse/bps_uat.db/maaa'

As per above, I need to add ; in the end of all such lines.

4
  • 5
    Where should the ; be added? After LOCATION: or maaa' or both? If after maaa', can we assume that each line contains 'hdfs:// as prefix and the line starts with a space character? Please edit your question and provide some more examples as input with the desired output. Do you need vim or would any standard unix tool be fine?
    – Freddy
    Sep 24 '19 at 14:42
  • Thanks for the response @Freddy. I need to add ; after /maa'; in vim There are multiple lines that start with space character but yes for this line, it starts with a space and 'hdfs://
    – SweetyP
    Sep 24 '19 at 15:05
  • This file contains ddl for 400 tables and you can say I need to add ; in the end of each table ddl. In this case, the end of each ddl is with the lines in my question.
    – SweetyP
    Sep 24 '19 at 15:05
  • 1
    @SweetyP Rather than adding comments, you should edit the question to include all relevant details. Sep 25 '19 at 10:42
6

In addition to Freddy's answer, you can use the :global command. For example:

:g/maaa/norm A;

Which means:

:g/      " On each line matching this regex:
maaa     " 'maaa'
norm A;  " Run 'A;' as if I had manually typed

You'll have to adjust 'maaa' to whatever you need, since it's not clear from your question which lines you want to apply this to. :g/hdfs/norm A; for example will append a semicolon to any line containing the text "hdfs".

2
  • Oh yes this works as well. Thanks @DJMcMayhem !!
    – SweetyP
    Sep 24 '19 at 17:04
  • @SweetyP Happy to help :) Also, if you have more vim questions, there is a dedicated stackexchange site for vim
    – DJMcMayhem
    Sep 24 '19 at 17:05
4

To add a ; to all lines ending with maaa', you could use the following substitution. Press Esc, then enter

:%s/maaa'$/&;/

Explanation:

  • :%s/ replace one each line
  • maaa'$ match maaa' at the end of the line $
  • /&;/ replace with the matched pattern and ;


To add a ; to all lines starting with a space and 'hdfs:// and ending with a ', you could use the following substitution. Again, press Esc, then enter

:%s/^ 'hdfs:\/\/.*'$/&;/

Explanation:

  • :%s/ replace one each line
  • ^ 'hdfs:\/\/.*'$ match the beginning of the line ^, followed by a space character, followed by hdfs:// (/ need to be escaped with a \), followed by any character .*, followed by ' and the end of the line $
  • /&;/ replace with the matched pattern and ;

You can always undo an operation with Esc + u if something went wrong.

3
  • I tried the last option without ^ and it worked !! Thanks a lot for the answer Freddy.
    – SweetyP
    Sep 24 '19 at 16:56
  • @SweetyP That command definitely works. You may have typed it wrong, or maybe you have trailing whitespace
    – DJMcMayhem
    Sep 24 '19 at 16:58
  • :%s/ 'hdfs:\/\/.*'$/&;/ worked.
    – SweetyP
    Sep 24 '19 at 16:58

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.