Sed and Regular Expression - regex

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

Related

Regular Expression Notepad++ to Find & replace strings

I have strings like following in a line
Q80a_Offline_MElDor_NET
Q80a_Offline_Mr_NET
Q80a_Offline_Mor_NET
I want to remove _NET from them using regex in Notepad++.
I also have following in the same line in the file which I don't want to touch.
Q80a_MElDor_NET
Q80a_Mr_NET
Q80a_Mor_NET
I can find these strings with following search string.
^Q80a_offline_[a-zA-Z]+_NET$
but not sure what to use as replace with regex expression
I want Q80a_Offline_MElDor_NET to be Q80a_Offline_MElDor
please help.
_NET$
Try this.Replace by empty string.See demo.
http://regex101.com/r/yR3mM3/55
or
^(Q80a_offline_[a-zA-Z]+)_NET$
Replace by $1.

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#".

How to add a comma to the last character of a line in a textfile

I have a very long text file full of email adresses, from an export of a newsletter db. To import them in another system I need to have these adresses separated by a comma.
I'm trying to do a search/replace using regex. So far I can find the last character of each line with :
[^\n]$
So that's for my "Search" field. But I don't know with what to put into "Replace". I'm very new to regex, is there a wildcard that would allow to do something like :
[any characted found],
You could search for ([^\n])$ and replace it by \1, – depending on your RegEx engine.
Look into sed...
sed 's/$/,/' filename
perl -pe 's/$/,/'⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠ ⁠⁠⁠⁠⁠ ⁠⁠⁠⁠⁠

Regular Expression - Capture and Replace Select Sequences

Take the following file...
ABCD,1234,http://example.com/mpe.exthttp://example/xyz.ext
EFGH,5678,http://example.com/wer.exthttp://example/ljn.ext
Note that "ext" is a constant file extension throughout the file.
I am looking for an expression to turn that file into something like this...
ABCD,1234,http://example.com/mpe.ext
ABCD,1234,http://example/xyz.ext
EFGH,5678,http://example.com/wer.ext
EFGH,5678,http://example/ljn.ext
In a nutshell I need to capture everything up to the urls. Then I need to capture each URL and put them on their own line with the leading capture.
I am working with sed to do this and I cannot figure out how to make it work correctly. Any ideas?
If the number of URLs in each line is guaranteed to be two, you can use:
sed -r "s/([A-Z0-9,]{10})(.+\.ext)(.+\.ext)/\1\2\n\1\3/" < input
This does not require the first two fields to be a particular width or limit the set of (non-comma) characters between the commas. Instead, it keys on the commas themselves.
sed 's/\(\([^,]*,\)\{2\}\)\(.*\.ext\)\(http:.*\)/\1\3\n\1\4/' inputfile.txt
You could change the "2" to match any number of comma-delimited fields.
I have no sed available to me at the moment.
Wouldn't
sed -r 's/(....),(....),(.*\.ext)(http.*\.ext)/\1,\2,\3\n\1,\2,\4/g'
do the trick?
Edit: removed the lazy quantifier

how to find and replace such a case

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)