shell script find and replace in regex linux [duplicate] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to use
sed -i -e "s/.*seb.*/ \"$ftp_login_template\"/" $ftp_dir
however I get this error:
sed: -e expression #1, char 34: unknown option to `s'
I don't understand why since this works perfectly:
sed -i -e "s/.*wbspassword.*/ \"wbspassword\": \"$password\",/" $user_conf
Any ideas as to what I'm doing wrong?
Could this be the problem?
ftp_login_template=\${user}:${password}:24:86::\/var\/lib\/clit.${user}\/downloads:\/bin\/false\"

The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.
Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string:
replacement="/my/path"
sed --expression "s#pattern#$replacement#"
Note that this is not bullet proof: if the replacement string later contains # it will break for the same reason, and any backslash sequences like \1 will still be interpreted according to sed rules. Using | as a delimiter is also a nice option as it is similar in readability to /.

Related

List files starting with two different prefixes - linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have files:
list-a1.jpg, list-a2.jpg
list-b1.jpg, list-b2.jpg
map-a1.jpg,map-a2.jpg
map-b1.jpg, map-b2.jpg
I want to list them using ls. I want to use regex but I have problem with prefixes. How to specify that my filename should start with "list-" or "map-"?
I tried to do:
ls [.map-.][.list-.][a-b][1-2].jpg
but it is not working as expected.
ls accepts multiple file parameters: ls [OPTION]... [FILE]...:
ls list-* map-*
For more control, you could take advantage of bash's curly brace expansion:
ls {list,map}-{a,b}{1,2}.*
You could use "extended globbing" - documentation:
shopt -s extglob
ls #(map|list)*

how to remove string before second ":" in notepad++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I want to get last two strings email#email.com:namesurname from those strings. I know how to remove the last item after : with :.* but how can i do that for first also for those below? Just give me a recommendation if anyone can.
jobapplication:::2017-05-29:email#email.com:namesurname
also like this one:
skills:email#email.com:namesurname
I dont have idea to start it and there are around 3200 job applications.
Use the regular expression ^.+\:(?=\w+\#) to find unwanted string then replace all matches by empty string.
Have you considered recording a quick macro in which you do it once, replace or whatever, then press home home and down arrow to advance to the next line? Then you could do Run For Rest of File and it'd be done. (Make a backup first. ;)) I find the quick macro feature of Notepad++ comes in handy for this kind of thing, and easier (for some of us) to remember how to use than arcane regexes.

Copy files that have pattern in this directory and change name of file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
Suppose I have these files in a directory
/var/mydir/web.php
/var/mydir/dbMuseum.php.example
/var/mydir/dbStreet.php.example
In Ubuntu Linux, what would be a one line command which would copy and rename all the files to the same names but without .example on the end?..giving the correct result:
/var/mydir/web.php
/var/mydir/dbMuseum.php.example
/var/mydir/dbMuseum.php
/var/mydir/dbStreet.php.example
/var/mydir/dbStreet.php
Just a bash for construct with parameter expansion would do:
for f in *.example; do mv -i "$f" "${f%.*}"; done
Ubuntu has rename (prename):
rename -n 's/\.[^.]+$//' *.example
-n would do the dry-run, remove -n for actual renaming to take place:
rename 's/\.[^.]+$//' *.example

Rename a set of files based on regular expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am looking to rename a large number of files that look like this on my ubuntu box:
MattTest Season 01 Episode 02 - Episode Name.mkv
Would like them to look like this:
MattTest S01E02 - Episode Name.mkv
Thanks
You could try the below rename command. \K keep up the previously matched characters from printing at the final. That is, character which are matched before \K are won't taken into consideration.
rename 's/^\S+\s+\K(.)\S*\s+(\S+)\s+(.)\S*\s+(\S+)/$1$2$3$4/' *.mkv
OR
rename 's/^\S+\s+\K(.)\S*\s+(\d+)\s+(.)\S*\s+(\d+)/$1$2$3$4/' *.mkv
Regex Demo

Weird grep corner case? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
If I have a file named 'test' with text literally:
\<.abc\>
and then run grep -E 'abc\> | [1-5]' test, I get no results as expected,
but when I run just grep -E 'abc\>' test, I get a match!
Why is this?
It looks like this problem was solved, but one other follow-up question:
If I wanted to use a regex like 'abc>' and for there to be no results (because no word ends with abc), how can I do this? (I also want to keep the quotes so that I can expand the regex).
grep -E 'abc\> | [1-5]' test, I get no results as expected
because you added spaces before and after the |, try:
grep -E 'abc\>|[1-5]' test
test here:
kent$ grep -E 'abc\>|[1-5]' <<<'\<.abc\>'
\<.abc\>