Timeline for How can I remove all text after a character in bash?
Current License: CC BY-SA 4.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 18, 2021 at 16:00 | comment | added | JakubKnejzlik |
@chovy afaik you need to use variable a='https://www.rt.com/news/510303-thailand-outbreak-coronavirus-testing/?utm_source=rss&utm_medium=rss&utm_campaign=RSS'; echo ${a%\?*}
|
|
Dec 25, 2020 at 8:47 | comment | added | chovy |
doesn't work for me: echo ${"https://www.rt.com/news/510303-thailand-outbreak-coronavirus-testing/?utm_source=rss&utm_medium=rss&utm_campaign=RSS"%\?*}
|
|
Oct 15, 2020 at 20:16 | history | edited | Serge Stroobandt | CC BY-SA 4.0 |
line spacing for improved clarity
|
Aug 28, 2019 at 21:29 | comment | added | Dennis Williamson | @kp123: It's the first example in my answer. The second time I show it (where "tomorrow" is removed), it's almost exactly the situation you're asking about. | |
Aug 28, 2019 at 21:20 | comment | added | kp123 | What if you have a string "Hello: world: this: is: me: again" and you want to truncate everything after the LAST occurrence of the ":", given that you do NOT know how many ":" characters are in the string. So here I would want the output to be "Hello: world: this: is: me". How would you do that? | |
Mar 10, 2019 at 16:41 | comment | added | Dennis Williamson |
@openCivilisation: You probably need to use regular expression matching then: a='hello:world:of:tomorrow'; pattern=''^([^:]*:).*$' ; [[ $a =~ $pattern ]]; echo "${BASH_REMATCH[1]}"`. That matches only if there is a colon. If you want that to be optional, a different pattern would have to be used. An explanation of Bash regexes (and regexes in general) is beyond the scope of these comments. You can find other questions that discuss this or post your own.
|
|
Mar 10, 2019 at 13:43 | comment | added | openCivilisation |
the example echo "${a%%:*}" generates hello . its really close to what I'm after, I'd just like to include the : if it exists as well to produce hello:
|
|
Mar 10, 2019 at 13:12 | comment | added | Dennis Williamson | @openCivilisation: What are you looking for? | |
Mar 10, 2019 at 12:05 | comment | added | openCivilisation | Is there another possible variation to generate hello: | |
May 6, 2018 at 15:42 | comment | added | Dennis Williamson | @EldadMor: Your escaping didn't show up, probably because you didn't enclose your code in backticks. | |
May 6, 2018 at 12:58 | comment | added | Eldad Mor | @Nazar: You need to escape the open parenthesis, like so: str="${str%(*}" | |
Jul 12, 2017 at 16:01 | comment | added | Nazar |
I am trying to remove (digit) from word(digit) to get only word, when using str="word(5)" , the str="${str%(*}" actually seems to add some character, instead of removing "(5)". Any ideas what's wrong?
|
|
Dec 16, 2016 at 18:21 | comment | added | Dennis Williamson | @SibbsGambling: They can't be nested. They have to be done as a sequence of separate operations. | |
Dec 16, 2016 at 4:42 | comment | added | Sibbs Gambling |
Nice. How should I nest these commands? I tried a=path/to/file_v1.ext; echo "${a##*/%_*}" , but I didn't get the desired file
|
|
Nov 28, 2016 at 23:40 | history | edited | Dennis Williamson | CC BY-SA 3.0 |
added another example
|
Nov 28, 2016 at 1:40 | comment | added | avtomaton |
Great answer, thanks a lot, but it will be nice to also add "${a##*:}" for getting only tomorrow =)
|
|
Jun 25, 2016 at 13:05 | comment | added | 3kstc |
@denniswilliamson How could I deleted 2 characters after a specific string like hello:world will become hello:wo by using ${a%:*} ?
|
|
Apr 14, 2014 at 3:13 | comment | added | Dodzi Dzakuma | This is native shell string manipulation, so no additional processes will be spawned. Reference Bash Parameter Substitution, Bash String Manipulation and Better Bash Scripting | |
Nov 13, 2010 at 1:24 | history | answered | Dennis Williamson | CC BY-SA 2.5 |