Monday, January 19, 2015

Add to the first row only in Notepad++


I'm doing Find in Files in Notepad++ and I need to add some commas to the end of the first row of each csv in a folder.


Adding to every row with regular expression $ will not help.


Column 1 row 1 will always start with "Ab" and that string will be nowhere else in the whole csv, but what comes after Ab varies.


With regular expressions I tried: find "Ab....." and replace it with "Ab.....,," but column 2 row 1 does not have the same amount of characters in each csv so that didn't work.


So for example, I want to add 2 commas after d but not after 2 or 4 for thousands of csvs where "d" and "g" vary.


Abg, d
y, e,1,2
c, f,3,4

Answer



Using N++'s regular expression, you could do the following:


find: ^(Ab.*)$


replace: \1,,


This matches Ab only if it's at the beginning of the line (the ^ is the 'begging' switch) followed by anything (.* says to repeat an 'any character match') up to the end of the line (the $ is the 'eol' switch). The () allows us to save the regex results so we can then use them later.


On the replace line, the \1 says to use the first parenthesized match as our replacement (the (Ab.*) portion) followed by 2 commas.


NOTE: ensure you don't have the . matches newline checkbox checked when doing the regex search as the .* would then evaluate to mean 'the rest of the document'.


So given your example above:


Abg, d
y, e,1,2
c, f,3,4

Would then be


Abg, d,,
y, e,1,2
c, f,3,4

You could then do 'Replace all in all open documents' to apply this regex to all files you have opened.


Hope that can help.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...