Extract numbers from a string using sed and regular expressions
is this ok? sed -r ‘s/.*_([0-9]*)\..*/\1/g’ with your example: kent$ echo “./pentaray_run2/Trace_220560.dat”|sed -r ‘s/.*_([0-9]*)\..*/\1/g’ 220560
is this ok? sed -r ‘s/.*_([0-9]*)\..*/\1/g’ with your example: kent$ echo “./pentaray_run2/Trace_220560.dat”|sed -r ‘s/.*_([0-9]*)\..*/\1/g’ 220560
Try following command: sed ‘/<tag>/ r file2.txt’ file1.txt It yields: <html> <body> <tag> Hello world </tag> </body> </html> EDIT for explanation why your command doesn’t work as you want: The r filename command adds its content at the end of the current cycle or when next input line is read. And you are using the … Read more
You can try this sed: sed -i.bak ‘s/^\(VAR5=\).*/\1VALUE10/’ file It gives: VAR1=VALUE1 VAR2=VALUE2 VAR3=VALUE3 VAR4=VALUE4 VAR5=VALUE10 VAR6=VALUE6
On macOS, \d is part of a regex feature set called enhanced features – note the distinction in name: enhanced, which is NOT the same as extended. Instead, enhanced features are a separate dimension from basic vs. extended, which can be activated for both basic and extended regexes. In other words: you can have enhanced … Read more
This is based on potong’s answer. The following code replaces ‘ll’ with ‘zz’, creates a backup file, displays the new text, and writes the change(s) into the file. $ echo hello > test $ sed -e ‘s/ll/zz/;w /dev/stdout’ -i .backup test hezzo $ cat test hezzo $ cat test.backup hello
This probably isn’t the answer you’re looking for, but you can’t. Mac OS X sed has no option to show the version number. There is not even a version number in the binary: $ strings $(which sed) $FreeBSD: src/usr.bin/sed/compile.c,v 1.28 2005/08/04 10:05:11 dds Exp $ $FreeBSD: src/usr.bin/sed/main.c,v 1.36 2005/05/10 13:40:50 glebius Exp $ $FreeBSD: src/usr.bin/sed/misc.c,v … Read more
The equivalent to x? is \(x\|\). However, many versions of sed support an option to enable “extended regular expressions” which includes ?. In GNU sed the flag is -r. Note that this also changes unescaped parens to do grouping. eg: echo ‘file_1.gz’|sed -n -r ‘s/.*_(.*)(\.gz)?/\1/p’ Actually, there’s another bug in your regex which is that … Read more
Your question is a little confusing since there’s no me in the original string to replace. However, I think I have it. Let me paraphrase: I have a sed command which can successfully replace a single quote ‘ with the word me. I want a similar one which can replace it with the character sequence … Read more
This part of the gnu sed manual you linked to explains that whether you should escape parentheses depends on whether you are using basic regular expressions or extended regular expressions. This part says that the -r flag determines what mode you are in. Edit: as stated in grok12’s comment, the -E flag in bsd sed … Read more
This thread is another example of how to over complicate things. This should do it: sed ‘0~30 s/$/string/g’ < inputfile > outputfile Every 30 lines “string” is inserted at the end of the line. If you want a new line with the word “string” just use “\n string”.