how to find and replace such a case - regex

i have some text files in a pre-defined directory, the files end with *.edi
Each of them refers in one separate line within itself:
data_file file_n.data
I have to convert this line in every .edi file to sth like
data_file 'another_directory/file_n.data'
i will add 'another_directory/ and ' in the end,
because the data will be in another directory.
and I have such 200 .edi files files, making it tedious to handle them manually!
any help from regexp?
I use UltraEdit engine for regexp by the way.
a_dir/
file1.edi
file2.edi
file3.edi
...
and each file refers in one line:
data file file_n.data
becomes:--->
data_file 'another_directory/file_n.data'

Just do a Replace in Files from the Search menu, using Perl Regular Expression Engine:
Find:
^(\s*data file\s+)(.+)$
Replace:
\1'another_directory/\2'
In Files/Types:
*.edi
Directory:
<whatever the directory with all your edi files is>
(tested in UltraEdit, works for me)

Replace the space(s) by space(s) followed by another_directory/ using appropriate metacharacters and escaping for /.

If I were using RegexBuddy's GREP tool, here's what I would do :
Match pattern:
^(data file\s{3})(file_n\.data)$
Options:
^$ match at line breaks.
Replace pattern:
\1'another_directory/\2'
I think UltraEdit should work pretty much the same.

ultraedit has find and replace in files. If you're in v13+ it also has PERL compatible regexes.
If you turn on PERL compatible regular expression you can use:
For Find What: " (file_\d+.data)" (without the double quotes)
For replace with: " 'another_directory/$1'" (without the double quotes)
For old style UltraEdit syntax regular expressions use:
Find What: " ^(file_[0-9]+^.data^)"
Replace With: " 'another_directory/^1'"
(without the quotes but the leading whitespace is significant)

Related

How to locate a mismatched text delimiter

I'm trying to remove double quotes that appear within a string coming from a dB because it's causing an stream error in another application. I can't clean up the dB to remove these, so I need to replace the character on the fly.
I've tried using sed, ssed, and perl all without success. This regular expression is locating the problem quotes, but when I plug it into sed to replace them with a single quote my output still contains the double quote.
sed "s/(\?<\!\t|^)\"(\?\!\t|$)/'/g" test.txt
I'm on Mac, if this looks a bit odd.
The regex is valid, but when I test on a tab-delimited file containing this:
"foo" "rea"son" "text's"
My output is identical to the above. Any idea what I'm doing wrong?
Thanks
I assume you want to turn all occurrences of " that are not on a field boundary (i.e. either preceded or succeeded by either a tab or the beginning/end of the string) by '.
This can be done using perl and the following substitution:
s/(?<=[^\t])"(?=[^\t\n])/'/g;
(With sed this is not directly possible as it does not support look-behind / look-ahead assertions.)
To use this code on the command line, it needs to be escaped for whatever shell you're using. Assuming bash or a similar sh-like shell:
perl -pe 's/(?<=[^\t])"(?=[^\t\n])/'\''/g' test.txt
Here I use '...' to quote most of the code. To get a single ' into the quoted string, I leave the quoted area ...', add an escaped single quote \', and switch back into a single-quoted string '.... That's why a literal ' turns into '\'' on the command line.

Regex Transform or Text Replace in Text Editor

I am trying to do an advanced text search and replace in a text editor (I can use SublimeText or VSCode)
Input:
parameters['myParameter1']
parameters['myParameter2']
Expected output:
myParameter1
myParameter2
And I have hundreds of similar scenario in the file. So that's why I'm thinking of using regex to solve this.
Thanks
Hope this will help you out.
Regex demo
Regex search: parameters\['([^']+)'\]
1. parameters\[' this will match parameters[' Optinally if you have other keywords then parameters and which can be dynamic for that you can use [a-zA-Z]+ this will include all lower case and upper case.
2. ([^']+) this will match all except ', () will capture first match in $1.
3. '\] this will match ending ']
Replacement: $1
Note: If you are using gedit ubuntu you have to replace it with \1
VSCode can search all files in a directory, and it supports RegExp too!
Use ctrl+shift+f to search all files, hit the .* icon for RegExp.
A search expression such as parameters['(.*)'] replaced with $1 should work!
Check out the official guide for more info: VSCode Basic Editing

Notepad++ replace text with RegEx search result

I would like replace a standard string in a file, with another that is a result of a regular expression. The standard text looks like:
<xsl:variable name="ServiceCode" select="###"/>
I would like to replace ### with a servicecode, that I can find later in the same file, from this URL:
<a href="/Services/xyz" target="_self">
The regular expression (?<=\/Services\/)(.*)(?=\" )
returns the required service code "xyz".
So, I opened Notepad++, added "###" to the "Find what" and this RegEx to the "Replace with" section, and expected that the ### text will be replaced by xyz.
But I got this result:
<xsl:variable name="ServiceCode" select="?<=/Services/.*?=" "/>
I am new to RegEx, do I need to use different syntax in the replace section than I use to find a string? Can someone give me a hint how to achieve the required result? The goal is to standardize tons of files with similar structure as now all servicecodes are hardcoded in several places in the file. Thanks.
You could use a lookahead for capturing the part ahead.
Search for: (?s)###(?=.*/Services/([^"]+)") and replace with: $1
(?s) makes the dot also match newlines (there is also a checkbox available in np++)
[^"] matches a character that is not "
The replacement $1 corresponds to capture of first parenthesized subpattern.
I am no expert at RegEx but I think I may be able to help. It looks like you might be going at this the wrong way. The regex search that you are using would normally work like this:
The parenthesis () in RegEx allow you to select part of your search and use that in the replace section.
You place (?<=\/Services\/)(.*)(?=\" ) into the "Find what" section in Notepad++.
Then in the "Replace with" section you could use \1 or \2 or \3 to replace the contents of your search with what was found in the (?<=\/Services\/) or (.*) or (?=\" ) searches respectively.
Depending on the structure of your files, you would need to use a RegEx search that selects both lines of code (and the specific parts you need), then use a combination of \1\2\3 etc. to replace everything exactly how it was, except for the ### which you could replace with the \number associated with xyz.
See http://docs.notepad-plus-plus.org/index.php/Regular_Expressions for more info.

Replace string with modified string

I have a file with 1000's of rows looking like
"20140611","20:19","C","IT","IT","HDR","HDPDIT","675605","000000135.97"," ..........
I am trying to replace all occurrences of string that matches this pattern :
quote then 6 numerics followed by a closing quote ( i.e. "675605" with "675605#")
Using edit plus regular expression search and replace, the search string is :
\"[0-9][0-9][0-9][0-9][0-9][0-9]\"
This will find all the occurrences I need
However I'm unable construct the correct replace with reg ex to replace the match with itself followed by the # sign e.g. "675605#
With sed you can have:
sed -r 's|"([0-9]{6})"|"\1#"|g' file
Add -i to modify it inline.
So my proposed regex - replacement form is:
"([0-9]{6})" - "\1#"
Quoted:
\"([0-9]{6})\" - \"\\1#\"
Regex:
\"([0-9][0-9][0-9][0-9][0-9][0-9])\"
Replacement string:
"\1#
DEMO
Replacement string would be "\1" if you want "675605#"
You need to use capturing groups. I don't know if you can use them in Edit Plus, but I think it should work:
Find what: \"(\d{6})\"
Replace with: \"\1#\"
Where \1 is a number captured in parenthesis.
Open your file in vim or vi editor using below command:
vi "filename"
then use this command it replace your "675605" pattern with "675605#"
:%s/675605/675605#/g
then
esc :wq
now you open your file it replaced all your "675605" pattern with "675605#".

Sed and Regular Expression

I've this issue: I've a CSV file that contains some data in this format:
100,30,1.704,,7
101,,suka,,5
and I should import this CSV inside mysql table
As you can seen, this csv has empty fields, denoted by double commas ',,' . I want using sed and regular expression, define pattern ,, and if it's matched substituing it with number 58 inside my csv file .
Anyone can help me, I don't knwo regex very well.
This might work for you:
sed ':a;s/,,/,58,/g;ta' file
something like this should do the trick:
sed -e 's/,,/,58,/g'
which will search ('s') or ',,' and replace it by ',58,' for all occurences ('g'; as opposed to only the first occurence))
In Vi editor ,go to command mode and type the below:
:%s/,,/,58,/g
You may try using notepad++ if you want regex options. Open Search and replace set the search mode as "Regular Expressions" replace ,, with ,58. I just tried it and it works... ,,,,,,,, to ,58,58,58,58