Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hi I'm trying to sed replace the entire string after a match.
my scenario will be inside my file I have the below values:
"XX|TESTFILE|MATCH" "CHANGEME"
"XX|TESTFILE|MATCH1" "CHANGEME1"
I need to replace the string after the text MATCH" " so my expected output will be
"XX|TESTFILE|MATCH" "THIS IS CHANGED"
"XX|TESTFILE|MATCH1" "CHANGEME1"
Hope it is clear and someone will help.
Thank you!
sed -ri '/MATCH\"/ {s/(^\"XX.* \")(.*)(\"$)/\1THIS IS THE CHANGE\3/}'
Using sed with -r flag for regular expressions, search for entries with MATCH followed by a double quote and then when encountered, split the line into three section. Print the first section followed by "THIS IS THE CHANGE" and then the third section.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have text file and it has content like this,
40 number of cpu
50 number of errors
and I need to insert comma between number and words. Its should be like this,
40, number of cpu
50, number of errors
Put comma after the first word in a line:
sed -E 's/^[[:space:]]*[^[:space:]]+/&,/' file.txt
Put comma after the first word in a line, only if it's numeric:
sed -E 's/(^[[:space:]]*[0-9]+)([[:space:]]|$)/\1,\2/' file.txt
Put comma after every numeric field:
sed -E 's/(^|[[:space:]])([[:digit:]]+)($|[[:space:]])/\1\2,\3/g' file.txt
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I extract substring from a string using key pattern and delimiter. e.g.
mystring = 'toto=1,2,3 mynames=ralf,john,franky myhobbies=tennis,soccer,naps'
I want to extract: ralf,john,franky
The pattern here is: mynames
The delimiter is: =
You can use this regular expression:
mynames=([^\s]+)
And then, look for the first group: $1
Here is a live example in JavaScript (works also in other languages):
var regex = /mynames=([^\s]+)/;
var text = "toto=1,2,3 mynames=ralf,john,franky myhobbies=tennis,soccer,naps"
console.log(regex.exec(text)[1]);
If you are looking for a regex which does only match everything after mynames=, so that you don't need to look for the first group, you can also use a positive lookbehind:
(?<=mynames=)[^\s]+
Here is a live example: https://regex101.com/r/mem6mA/1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
a= <<EOF
Password
:
7UV1ceFQ (You will be asked to change this after logging in for the first time)
EOF
I need to extract the value "7UV1ceFQ" using regular expression, I have tried using '/Password : 7UV1ceFQ/ but it's not working, I think it's because next line character is included, Can anyone please suggest me to exact this value?
▶ a[/^\S+(?=\s\(You will be)/]
#⇒ "7UV1ceFQ"
The regular expression above reads as:
starting with a new line start ^
get all non-space symbols greedy \S+
until the positive lookahead (?=\s\(You will be)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to find all words in a string.
The user can add words in any pattern like comma seperated or ; seperated or anything.
Input: Hello, Hi, test word
Output: array("Hello","Hi","test","word");
If a word is string that contains only word characters \w, you could do:
preg_match_all('/(\w+)/', $input_string, $matches);
Or use preg_split:
$words = preg_split('/\W+/', $input_string);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I convert the string russ(ai)(edocn)cup to russiancodecup using Ruby?
By using gsub with a block, you can replace any match of a regular expression by the result of this block.
s = "russ(ai)(edocn)cup"
s.gsub(/\(([^)]*)\)/) {$1.reverse} # => "russiancodecup"
Here the regular expression will match any non-) character between brackets. Then it will send reverse to $1 which is gonna be the content between brackets.
$0 will be the complete match and $n, the nth "submatch". (anybody for the correct word ?)