Remove one single word before a delimiter using sed - sed

How should I remove a single word before a delimiter using sed
For e.g. if the input is hello I am eating: mango and delimiter is : the output should be hello I am : mango

kent$ sed 's/[^:[:space:]]*:/:/' <<<"hello I am eating: mango"
hello I am : mango
The sed one-liner, changes the first foo: into :

Related

Replace multiple lines using sed with other multiple lines

I am trying to find these two lines:
<paramsToUseForLimit></paramsToUseForLimit>
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>
and replace them with:
<paramsToUseForLimit/>
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>
<jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api#2.6.3">
<durationName>hour</durationName>
<count>2</count>
<userBoost>false</userBoost>
</jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>
from my file config.json
Can someone help me to do this with sed ?
sed -ie "s/Those two lines/Replaced with those 7 lines/g" /config.json
sed may not be the best tool for this task.
sed -ie '/^ <\(paramsToUseForLimit>\)<\/\1/{
N
/\n<\/hudson.plugins.throttleconcurrents.ThrottleJobProperty>/{
i \
<paramsToUseForLimit/>\
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>\
<jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api#2.6.3">\
<durationName>hour</durationName>\
<count>2</count>\
<userBoost>false</userBoost>\
</jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>
d
}
}' /config.json
/^.../{ - if pattern space matches first match line:
N - append next line of input to pattern space
/\n.../{ - if pattern space matches second match line:
i \ - insert new text (\ before embedded newlines)
d- delete original text, implicit print, and start next cycle
otherwise, implicit print (two lines if N ran, else one line)
start next cycle
This is quite fragile. For example, watch out for shell metacharacters in the inserted text.
With GNU sed with -z option and inspired with Escape a string for a sed replace pattern question and answer - you can properly escape the patterns and replace newlines with \ n characters and then pass to sed. Then it's simple:
KEYWORD=' <paramsToUseForLimit></paramsToUseForLimit>
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>'
REPLACE=' <paramsToUseForLimit/>
</hudson.plugins.throttleconcurrents.ThrottleJobProperty>
<jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api#2.6.3">
<durationName>hour</durationName>
<count>2</count>
<userBoost>false</userBoost>
</jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>'
ESCAPED_KEYWORD=$(printf '%s\n' "$KEYWORD" | sed -z 's/[]\/$*.^[]/\\&/g; s/\n/\\n/g');
ESCAPED_REPLACE=$(printf '%s\n' "$REPLACE" | sed -z 's/[\/&]/\\&/g; s/\n/\\n/g')
sed -z "s/$ESCAPED_KEYWORD/$ESCAPED_REPLACE/" input_file.txt

sed is replacing whole line instead of just the string

I want to just replace few strings in file with nothing, but sed replaces the whole line. Can someone help me with this?
line in file.xml:
<tag>sample text1 text2</tag>
My code:
sed "s/'text1 text2'//" file.xml 2>/dev/null || :
I also tried
sed -i -e "s/'text1 text2'//" file.xml 2>/dev/null || :
expected result:
<tag>sample</tag>
Actual result:
The whole line is removed from file.
Others:
text1 and text 2 are complex text with .=- characters in it
What can I do to fix this?
TIA
Remove the single quotes:
sed "s/text1 text2//" file.xml
You could use
sed 's/\([^ ]*\)[^<]*\(.*\)/\1\2/' filename
Output:
<tag>sample</tag>
Grouping is used. First all characters till a space are grouped together, then all characters till a < are matched and all following characters are grouped into another group.

How to exclude end of lines of textfiles via terminal?

Given a file ./wordslist.txt with <word> <number_of_apparitions> such as :
aš toto 39626
ir 35938
tai 33361
tu 28520
kad 26213
...
How to exclude the end-of-lines digits in order to collect in output.txt data such :
aš toto
ir
tai
tu
kad
...
Note :
Sed, find, cut or grep prefered. I cannot use something which keeps [a-z] things since my data can contain ascii letters, non-ascii letters, chinese characters, digits, etc.
I suggest:
cut -d " " -f 1 wordslist.txt > output.txt
Or :
sed -E 's/ [0-9]+$//' wordslist.txt > output.txt.
Use awk for print first word in this case.
awk '{print $1}' your_file > your_new_file
awk solution to simply print input line excluding last column
$ awk '{NF--; print}' wordslist.txt
aš toto
ir
tai
tu
kad
Note:
This will only work in some awks. Per POSIX incrementing NF adds a null field but decrementing NF is undefined behavior (thanks #EdMorton for the info)
This doesn't check if last column is numeric and field separation in output will be single space only
If there can be empty lines in input file, use awk 'NF{NF--}1'
The following works :
sed -r 's/ [0-9]+$//g' wordslist.txt

How to extract from string all words between double quotes using SED

I'm trying to use a regexp, like
.*?"([^"]+).*?"/g
to extract all words between double quotes from string.
For example from:
< Header param1="1" param2="2" param3="" param4="" param5=5 param6="6"
>
I would like to get:
1 2 6
Yes, I know that I can use grep, but it is necessary do it by sed
There is no BRE or ERE than can do what you want so it can't be done in one regexp with sed. You CAN do this in sed instead if that's acceptable:
$ sed -E 's/^[^"]*"|"[^"]*$//g; s/"[^"]+"/ /g; s/ +/ /g' file
1 2 6

insert a double backslash using sed

I want to insert the line
text \\
into a file via sed. For one backslash to be inserted I would use three backslashes in the command. But escaping does not work for two backslashes:
sed -i 'text \\\\\\' $file
gives the following error:
sed: -e expression #1, char 57: unknown command: `
'
Do something like:
sed -i 's/OLDTEXT/text \\\\/' $file
You need 4 backslashes when trying to add 2. 3 for 1, 4 for 2 etc :)
With an input file like this:
apple
orange
grape
You can insert text \\ in front of orange like this:
sed -i 's/^orange/text \\\\\n&/' input.txt
What this literally does is match the line starting with orange, replace the matched string with text \\ + a newline + the matched string, effectively inserting text \\ right in front of the line, that is:
apple
text \\
orange
grape
Note that it takes 4 backslashes to get 2 inserted. I don't know you meant about 3 backslashes for 1, you must be overlooking something there.
This might work for you (GNU sed):
sed -i '/PATTERN/i\text \\\\' file
This will overwrite the original file and insert text \\ before the line(s) containing the word PATTERN.