Notepad++ Regex Remove Last x Lines From Files In Directory - regex

Have files beginning with 15 lines of needed info, followed by 3280 extra lines of unneeded data.
Have tried multiple regex patterns to remove last 3280 lines in all files in directory.
Tried similar to:
^.*(?:\R.*){3279}\z
Fails every time when trying to replace with "" (empty) in directory of files.
Using Notepad++ Find in Files option.
How can I remove the last n number of lines from every file in the directory?

You can use
Find What:      \A(.*(?:\R.*){14})(?s:.*)
Replace With: $1
Details:
\A - start of string (^ would do, too, here)
(.*(?:\R.*){14}) - fifteen lines
(?s:.*) - any text till end of the file (text).
The replacement is the backreference to Group 1 value.
Settings:

Related

Add word to the end of a each line in all files using regex notepad++

I have 10 files of 100 lines each. I need to do their translation.
This is one line in some file: "Client Notes,141"
and another simular word line in another file "Client Notes,700"
I want to modify all the concerned lines in all the 10 files to be:
"Client Notes,141,KundLinjer"
"Client Notes,700,KundLinjer"
"Client Notes,770,KundLinjer"
I tried with regular expressions and macros but I couldn figure it out
Thanks for help!
Assuming it is Notepad++ and not Notepad2:
Ctrl+H
Find what: \bClient Notes,\h*\d+\K
Replace with: ,KundLinjer
check Wrap around
check Regular expression
Replace all
Explanation:
\b : word boundary
Client Notes, : literally
\h* : 0 or more horizontal spaces
\d+ : 1 or more digits
\K : forget all we have seen until this position
Result for given example:
Client Notes,141,KundLinjer
Client Notes,700,KundLinjer
You may try the following find and replace:
Find:
^Client Notes,(\\d+)
Replace:
Client Notes,$1,KundLinjer
To make it apply to multiple files, use the directory option in the dialog to select the folder which contains the 10 files. If the 10 files be scattered in multiple places, then create a single folder containing those files. Also, make sure you do the find and replace with regex mode enabled.
In nodepad++ you can use the shortcut: Ctrl+H and under the Replace tab, search mode select Regular expression
Find:
(Client Notes, \d+)
Replace:
\1, KundLinjer
You have the option to:
Replace All in All Opened Documents.
Explanation
The brackets (client Notes, \d+) will 'capture' anything within the brackets to be used in our replacement \1 (if you had more captures, you could use \2, \3 etc..)
\d+ means any digit, one or more times (in your case 141, 700).
So we are replacing the text "Client Notes (AnyNumber)" with "Client Notes (AnyNumber) KundLinjer".
You could also replace (.*) with \1 KundLinjer if you want to add KundLinjer to all lines, no matter what.

regex How to find the last occurrence of char using regex

I ran a dir /s command to list files in folders & sub-folders, and to show the file size. My issue is that I use a "," as a separator between the file name and size.
Is their any way to find the last comma in the and replace it with";"?
My text file looks like this:
H:\IP Phones, Mobile Information.xls,152064
H:\Master Sheet Updated.xlsx,46446
The following regex will match a comma that is not followed by .* (any number of any character) and a comma.
,(?!.*?,)
Then just use the Replace... functionality in Sublime (Ctrl+H) and it should work.

Howto search and replace 3 jobs at once with regexp

I'm using a program that's called ASR (Actual Search and Replace) which has some powerful features build in to search a text with regexps and replace it.
I'm using it a lot and I kind of scripted it into my workflow.
Problem is, I need to replace three searches to correct a configuration file (strip the "-" from only these three lines), this is all manual work and very time consuming.
The config file has got the following lines randomly through the file and they can occur multiple times with different names and numbers. They are always on one single line.
<id>filename-33</id>
<source>#filename-33</source>
<url>{filename-33}</url>
The desired output should be:
<id>filename33</id>
<source>#filename33</source>
<url>{filename33}</url>
Both "filename" as the number "33" can be anything (filename is always a name lowercase no special characters and the number is always a number from 0 to 1000).
I know howto find and replace all three lines with:
<source>#(.*)- replace with <source>#$1
<url>{(.*)- replace with <url>{$1
<id>(.*)- replace with <id>$1
But this has to be done in three separate runs.
My question is, is it possible to do a search and replace with just one single find line and one single replace line ?
Regards,
Arjan
You can use use an alternation (using the | pipe operator) to create a single expression that will match all 3 patterns and create a single replacement.
Replacing this pattern:
(?:<source>(?=#)|<url>(?={)|<id>)([^-]+)-
with $1$2 should result in the correct output.
https://regex101.com/r/mS3mP9/3
Analysis of the expression:
( // begin capturing group
<source># // find the opening <source> tag followed by a #
| <url>{ // ...or find the opening <url> tag followed by a {
| <id> // ...or find the opening <id> tag
) // end capturing group
([^-]+) // capture everything that is not a hyphen
- // match and consume the hyphen
It can be done using ^(<(?:id|source|url)>(#|\{)?\w+)- and replacing it with $1 as shown here.

How to match all lines with common pattern in splunk regex

I am trying to extract a report of all incidents matching a certain pattern and then need to plot how many occurances of each type. For example the below lines.
File: ../../../transfer/200.FILETYPE1.0000003115.20160419-082708-089.xml successfully imported.
some other logs....
File: ../../../transfer/200.FILETYPE1.0000003116.20160419-082708-090.xml successfully imported.
some other logs...
File: ../../../transfer/201.FILETYPE2.0000003117.20160419-082708-091.xml successfully imported.
Please note that there are many filetypes but the pattern is same "/transfer/" prefix and "successfully imported." suffix and these prefix and suffix must match as other lines may also contain same file name before completion.
So in above case I need to find all such occurrences of above lines and find count of each FILETYPE1 and FILETYPE2 in splunk.
Can someone help me with regex that can match above pattern and give me all such lines so that I can extract counts of each file type?
Straight forward:
^File:.*FILETYPE\d.*$
# ^ beginning of the line
# File: literally
# .* anything to the end of the line
# FILETYPE + a number literally
# .* anything afterwards
# $ the end of the line
See a demo on regex101.com.
Hint: If you only have these two strings (FILETYPE1 and FILETYPE2) you might be faster with string functions only.
Edit FILETYPE1/FILETYPE2 for counting
\.\.\/.*\/\d+\.FILETYPE1\..*?\.xml
Regex demo
Try this one:
\/transfer\/.*FILETYPE(\d+).*successfully imported
The file type number will be captured by the capture group, so you can count the file occurrences
Regex Demo

How to delete first blank row in multiple files?

I have thousands of text file with empty first row. Is it possible to delete this row in all files at once?
You need a bat script like this
#echo off
for %%i in (*.txt) do (
more +1 "%%~fi">>temp
del "%%~fi"
ren temp "%%~nxi"
)
Save the above code as something.bat and run it at your directory.
This will work using Notepad++ (tested with version 6.2.3):
\A[\r\n]+
Explanation:
\A and \Z always match the beginning and end of the entire file, irrespective of the multiline setting.
Note: This regex is slightly more general than the OP asked. It will remove any number of consecutive initial blank rows terminated with any line break sequence (\r\n, \r or \n).
Nothing is worse than changing thousands of files only to find later that a couple have a different line break sequence or have multiple initial blank lines.
Alternative:
Another regex that works is:
(?<!.)[\r\n]+
Explanation:
This uses negative look-behind, (?<!), to make sure no character exists before the sequence of CRs and LFs.
Note: You must tick the . matches newline check box for this to work.