I am trying to write a regex expression to replace one or more ‘+’ symbols present in a file with a space. I tried the following: Expected: Any ideas why this did not work? Answer Use gsub which does global substitution: sub function replaces only 1st match, to replace all matches use gsub.
Tag: regex
how can i use regex with locate command in linux
I want to use the locate command with regex but i am not able to use it. I want to find pip file which is in /usr folder. i am trying this locate -r “/usr/*pip” Answer To use globbing characters in your query you shouldn’t specify regex (as you do with -r option), so just do: From the man page:
Non-greedy repeat in gnu regular expression
I’m writing a network related program in linux. the program is in kernel space and not user space. All I need in this program is a regular expression library that supports all gnu regex library features plus non-greedy repeat. Is there any such library that has those features and also I can compile and use it in kernel space? Answer
Better bash script to create directory structure
I want to create folders with plain text with a structure like: I thought about using a base command like mkdir and found that post : Bash script that creates a directory structure, but it still needs to write structure like : So I would need to change the regex used in : What bash command could I use to
Use grep to match a pattern in a line only once
I have this: Which gives this: I understand whats happening. How do I stop grep from trying to continue matching after 1 successful match? How do I get only Answer You need to do the grouping: (…) followed by the exact number of occurrence: {<n>} to do the job: Hope it helps. Cheers!!
regular expression to exclude filetypes from find
When using find command in linux, one can add a -regex flag that uses emacs regualr expressions to match. I want find to look for all files except .jar files and .ear files. what would be the regular expression in this case? Thanks Answer You don’t need a regex here. You can use find with the -name and -not options:
Sed – How to Print Regex Groups in Multi-Line?
Input file (test): Desired output: What I have done: Question: How do you print the regex groups in sed (multiline)? Answer If you use sed like this: then it will print in different lines: But is this what you are trying to print? Or you want to print text in hrefs? Update 1: To get hrefs between well formed <a
linux find regex
I’m having trouble using the regex of the find command. Probably something I don’t understand about escaping on the command line. Why are these not the same? Bash, Ubuntu Answer Regular expressions with character classes (e.g. [[:digit:]]) are not supported in the default regular expression syntax used by find. You need to specify a different regex type such as posix-extended
Whats the difference between sed -E and sed -e
I’m working on some old code and I found that I used to use but I now try It seems to do the same thing, or does it? I kinda remember there being a reason I done it but I can’t remember and doing “man sed” doesn’t help as they don’t have anything about -E only -e that doesn’t make
Combining values from different files into one CSV file
I have a couple of files containing a value in each line. EDIT : I figured out the answer to this question while in the midst of writing the post and didn’t realize I had posted it by mistake in its incomplete state. I was trying to do: and was getting a weird output. I later realized that was happening