tags: how to remove text before or after a specific character in word, bash remove character from string, sed remove character, sed remove character after match, sed remove character before match
sed remove character before match |
Many occasions when we might want to remove the text after the match character or special character in the big line so this post is very helpful for you if you.
Just for the example, I have one file like linuxtopic.com and i need only one value number so i will remove everything after the match, below steps are:
Used Command
1 - cat
2 - grep
3 - awk
4 - sed
This is output of command, written in in text file for this tutorial. In below output I need a only run_value without double quote and squarer bracket.
When we grep "run_value" from the file, we got below output
grep run_value /tmp/linuxtopi.com
for the more relevant output, we will use awk command in above command with the help of pipe "|".
grep run_value /tmp/linuxtopi.com | awk '{print $3}'
In the above screenshot, We print 3rd Colom with the help of awk command
Now we have only one word like run_value="[1000930]" but we need only a value so we will use sed command for remove all text or character before / after the match.
grep run_value /tmp/linuxtopi.com | awk '{print $3}' | sed 's/.*\[//g'
In the above command we remove everything after the match square bracket.
Explanation of command
sed 's/.*\[//g'
sed = is command
's = for search,
.* = everything after the match
\[ = character/word which we have to remove everything after the match
// = indicate remove/nothing
g' = global
We removed all text or characters before the match via above command now we will remove all the text or characters after the match
grep run_value /tmp/linuxtopi.com | awk '{print $3}' | sed 's/.*\[//g' | sed 's/\].*//g'
Yes!, I got it!, above case in perform with my custom command
I hope this topic gave you all the information you needed. If you have any further questions or would like more detailed directions feel free to contact us using any of the following sources.We look forward to talking to you.