sed

sed replace xml tag

試著忘記壹切 提交于 2020-12-26 22:59:33
问题 I want to replace value inside a tag in an xml file using sed. <version> xxxx-SS </version> I want to replace xxxx-SS with some shell variable $ver . The final result should be <version>$ver</version> The sed command should also quit after replacing the first instance. So far I have been able to only append to the xxxx-SS and not been able to quit after the first match. sed 's#\(<version>\)*\(</version>\)#\1'$ver'\2#g' test.xml This only appends the value between -SNAPSHOT tag.Basically makes

How to escape the ampersand character while using sed

♀尐吖头ヾ 提交于 2020-12-24 08:47:06
问题 I want to replace all single quotes in a string with two single quotes using sed. But when the string contains the & character, the sed command is not replacing single quotes that come after that. How can I escape the & character so that the single quotes after it are still replaced? 回答1: You don't need to escape anything in the input : $ echo "123 ' foo & b'ar" | sed "s/'/''/g" 123 '' foo & b''ar However, in the 'replacement' part of the s command & has a special meaning: it means 'match'.

Invalid reference \1 using sed when trying to print matching expression

為{幸葍}努か 提交于 2020-12-16 07:44:30
问题 Before I start, I already looked at this question, but it seems the solution was that they were not escaping the parentheses in their regex. I'm getting the same error, but I'm not grouping a regex. What I want to do is find all names/usernames in a lastlog file and return the UNs ONLY. What I have: s/^[a-z]+ |^[a-z]+[0-9]+/\1/p I've seen many solutions that show how to do it in awk, which is great for future reference, but I want to do it using sed. Edit for example input: dzhu pts/15

Invalid reference \1 using sed when trying to print matching expression

吃可爱长大的小学妹 提交于 2020-12-16 07:42:38
问题 Before I start, I already looked at this question, but it seems the solution was that they were not escaping the parentheses in their regex. I'm getting the same error, but I'm not grouping a regex. What I want to do is find all names/usernames in a lastlog file and return the UNs ONLY. What I have: s/^[a-z]+ |^[a-z]+[0-9]+/\1/p I've seen many solutions that show how to do it in awk, which is great for future reference, but I want to do it using sed. Edit for example input: dzhu pts/15

Invalid reference \1 using sed when trying to print matching expression

烂漫一生 提交于 2020-12-16 07:42:27
问题 Before I start, I already looked at this question, but it seems the solution was that they were not escaping the parentheses in their regex. I'm getting the same error, but I'm not grouping a regex. What I want to do is find all names/usernames in a lastlog file and return the UNs ONLY. What I have: s/^[a-z]+ |^[a-z]+[0-9]+/\1/p I've seen many solutions that show how to do it in awk, which is great for future reference, but I want to do it using sed. Edit for example input: dzhu pts/15

How can I use a regex variable for a find and replace command in bash?

这一生的挚爱 提交于 2020-12-13 04:39:28
问题 This is a follow-up to a previous question I had asked. For that question, I received an excellent answer by @markp-fuso demonstrating the use of an array of search patters for sed to perform a particular substitution task. In this question, I would like to perform the same find and replace task of replacing all forms of pow(var,2) with square(var) but I would like to do this by using a regex variable. The sample input and output files are below: InputFile.txt: pow(alpha,2) + pow(beta,2) (3

Extract string between parentheses

ε祈祈猫儿з 提交于 2020-12-11 14:29:30
问题 I've been trying to extract the substring in between parentheses (including parentheses) from: "WHITE-TAILED TROPIC-BIRD _Phaëthon lepturus_ (Hawaiian name—koae)" I tried this: str=$(echo $1 | sed 's/.*\(\([^)]*\)\).*/\1/'); echo $str What I wanted to get was: "(Hawaiian name—koae)" However, I've been getting an error called: bash: syntax error near unexpected token `(' What do I do wrong? 回答1: You may use sed -n 's/.*\(([^()]*)\).*/\1/p' Here, -n - suppresses default line output .*\(([^()]*)

Extract string between parentheses

こ雲淡風輕ζ 提交于 2020-12-11 14:26:48
问题 I've been trying to extract the substring in between parentheses (including parentheses) from: "WHITE-TAILED TROPIC-BIRD _Phaëthon lepturus_ (Hawaiian name—koae)" I tried this: str=$(echo $1 | sed 's/.*\(\([^)]*\)\).*/\1/'); echo $str What I wanted to get was: "(Hawaiian name—koae)" However, I've been getting an error called: bash: syntax error near unexpected token `(' What do I do wrong? 回答1: You may use sed -n 's/.*\(([^()]*)\).*/\1/p' Here, -n - suppresses default line output .*\(([^()]*)

Extract string between parentheses

守給你的承諾、 提交于 2020-12-11 14:24:10
问题 I've been trying to extract the substring in between parentheses (including parentheses) from: "WHITE-TAILED TROPIC-BIRD _Phaëthon lepturus_ (Hawaiian name—koae)" I tried this: str=$(echo $1 | sed 's/.*\(\([^)]*\)\).*/\1/'); echo $str What I wanted to get was: "(Hawaiian name—koae)" However, I've been getting an error called: bash: syntax error near unexpected token `(' What do I do wrong? 回答1: You may use sed -n 's/.*\(([^()]*)\).*/\1/p' Here, -n - suppresses default line output .*\(([^()]*)

How to extract email headers extending on multiple lines from file

你。 提交于 2020-12-06 20:23:45
问题 I am trying to extract the To header from an email file using sed on linux. The problem is that the To header could be on multiple lines. e.g: To: name1@mydomain.org, name2@mydomain.org, name3@mydomain.org, name4@mydomain.org, name5@mydomain.org Message-ID: <46608700.369886.1549009227948@domain.org> I tried the following: sed -n -e '/^[Tt]o: / { N; p; }' _message_file_ | awk '{$1=$1;printf("%s ",$0)};NR%2==0{print ""}' The sed command extracts the line starting with To and next line. I pipe