r/commandline • u/peak---- • Apr 28 '22
bash How to exclude string from line?
i have already posted this on different subs but haven't gotten any answers yet
Hi,
I have this (bash) script, where I want to print my OS name.
Here is the code:
printf "OS: " cat /etc/os-release | grep PRETTY_NAME=
and the output:
OS: PRETTY_NAME="Debian GNU/Linux bookworm/sid"
How do I exclude the "PRETTY_NAME=" thing?
EDIT: The answer is to use sed command.
printf "OS: "
cat /etc/os-release | grep PRETTY_NAME= | sed 's/^PRETTY_NAME=//'
8
Upvotes
8
u/[deleted] Apr 28 '22
The /etc/os-release file is actually a valid shell script, so the easiest solution is
But the more specific answer would be something like: -
EDIT TO add:-
If you are using bash and the line you want to edit is already in a variable then you can also use:-
3
u/schakalsynthetc Apr 28 '22
with sed(1)
the expression is a substitution, but with a zero-length replacement text so it's effectively a delete. (and remember to quote it to stop the shell from parsing the equal sign.)
6
u/NapoleonDeKabouter Apr 28 '22
Another solution:
grep PRETTY_NAME= /etc/os-release| cut -d= -f2-
4
u/eftepede Apr 28 '22
One command:
awk -F= '/^PRETTY_NAME/ {print $2}' /etc/os-release.4
u/NapoleonDeKabouter Apr 28 '22
Another solution:
echo $(source /etc/os-release && echo $PRETTY_NAME)
2
u/o11c Apr 28 '22
It's almost always a mistake to do
grep | sed, sincesedis semantically a superset ofgrep(though converting some advanced things is nontrivial - but often worth it).2
u/trullaDE Apr 28 '22
Another possible solution using awk instead of sed:
cat /etc/os-release | grep PRETTY_NAME= | awk ' BEGIN { FS="="} { print $2 } '
-> sets the field seperator of awk to "=" and then prints the second field.
1
u/XxVitaxX Apr 28 '22
Another possible solution:
1
u/Feeling-Newspaper-11 Apr 28 '22 edited Apr 28 '22
Alternative to 'positive lookbehind(?<=this_stuff)' because you can't use 'lookbehind' always if you don't know what you are looking for; you can only use 'lookbehind' for static length strings for example REDDIT not "R.*T" but with \K escape you can use as "R.*T\K"
\K escape on Perl Regex
Edit: typo
0
u/Marian_Rejewski Apr 28 '22
Don't need
cat. Don't needgrep.Do it right with one external command:
You can also do it with no externals: