Replace a string with pattern within that string
I want to replace a part of string with a pattern within that string.
for eg: if my string is "abcd#25 - efgh ijkl" it should print output as "abcd#efgh" if my string is "xxxx#100 - yyyy zzzz" it should print output as "xxxx#yyyy" The pattern is # followed by integer should be replaced with first word after # integer - Sorry for my bad english. |
Try the following:
Code:
sed 's/\(.*#\).*- \(.*\) \(.*\)/\1\2/' file |
Use the -r option to remove the need to backslash everything.
Code:
sed -r 's/(.*#).*- (.*) (.*)/\1\2/' file But when you use the extended regular expressions (sed -r/grep -E/egrep), everything is reversed. All regex characters are enabled by default, and escaping disables them. Check out the grep man page for details (and the info page for even more). |
Quote:
|
Code:
awk -F"[ #]" '{print $1"#"$4}' file |
All times are GMT -5. The time now is 11:51 PM. |