Page 1 of 1
grep using a variable (Solved)
Posted: Sat Nov 16, 2024 8:36 pm
by AntonioPt
Hello all im trying to achive some but for some stupid reason i don't get it what im doing wrong maybe some geek in bash can explain it for me
Thxx in advance
So my issue is
Code: Select all
#!/bin/bash
export SCRIPT_PATH="$(dirname $(readlink -f $0))"
export a="\\\[Desktop Entry\\\]"
export b="\\\[Desktop Action Window\\\]"
export c="\\\[Desktop Action Document\\\]"
export reg_1="(?s).*"
FP="${SCRIPT_PATH}/apps/sublime_text.desktop"
# LIKE THIS BOTH WAYS WORKS
line="\[Desktop Entry\](?s).*\[Desktop Action Window\](?s).*\[Desktop Action Document\](?s).*"
line="\[Desktop Entry\]${reg_1}\[Desktop Action Window\]${reg_1}\[Desktop Action Document\]${reg_1}"
grep -Poz "${line}" ${FP}
# using the loop it will not work
cnt=0
while read line ; do
if [ "$(sed -r "s/(.)/\1\n/g" <<< $line | sort | uniq | wc -l)" -eq 4 ] ; then
echo "linha => $line " # DEBUG LINE AND GIVES ME THE RIGHT OUTPUT
grep -Poz "$line" ${FP} # BUT ON THE OTHER HAND GREP WONT DISPLAY NUTTING
if [ ! -z "$( grep -oPz "`echo ${line}`" ${FP} )" ] ; then
echo "cnt = $cnt"
echo "cnt = $line"
fi
fi
cnt=$((cnt+1))
done <<<"$(echo -e {0..2}{0..2}{0..2}"\n")" | sed -r "s/0/${a}${reg_1}/ ; s/1/${b}${reg_1}/ ; s/2/${c}${reg_1}/"
Re: grep using a variable
Posted: Sun Nov 17, 2024 4:28 pm
by HerrBert
AntonioPt wrote: ↑Sat Nov 16, 2024 8:36 pm
Hello all im trying to achive some but for some stupid reason i don't get it what im doing wrong maybe some geek in bash can explain it for me
Thxx in advance
So my issue is
Code: Select all
#!/bin/bash
export SCRIPT_PATH="$(dirname $(readlink -f $0))"
export a="\\\[Desktop Entry\\\]"
export b="\\\[Desktop Action Window\\\]"
export c="\\\[Desktop Action Document\\\]"
export reg_1="(?s).*"
FP="${SCRIPT_PATH}/apps/sublime_text.desktop"
# LIKE THIS BOTH WAYS WORKS
line="\[Desktop Entry\](?s).*\[Desktop Action Window\](?s).*\[Desktop Action Document\](?s).*"
line="\[Desktop Entry\]${reg_1}\[Desktop Action Window\]${reg_1}\[Desktop Action Document\]${reg_1}"
grep -Poz "${line}" ${FP}
# using the loop it will not work
cnt=0
while read line ; do
if [ "$(sed -r "s/(.)/\1\n/g" <<< $line | sort | uniq | wc -l)" -eq 4 ] ; then
echo "linha => $line " # DEBUG LINE AND GIVES ME THE RIGHT OUTPUT
grep -Poz "$line" ${FP} # BUT ON THE OTHER HAND GREP WONT DISPLAY NUTTING
if [ ! -z "$( grep -oPz "`echo ${line}`" ${FP} )" ] ; then
echo "cnt = $cnt"
echo "cnt = $line"
fi
fi
cnt=$((cnt+1))
done <<<"$(echo -e {0..2}{0..2}{0..2}"\n")" | sed -r "s/0/${a}${reg_1}/ ; s/1/${b}${reg_1}/ ; s/2/${c}${reg_1}/"
Well, i'm not the geek you're waiting for. But at first sight there's two things that look terribly wrong to me...
<<<"$(echo -e {0..2}{0..2}{0..2}"\n")" | sed -r "s/0/${a}${reg_1}/ ; s/1/${b}${reg_1}/ ; s/2/${c}${reg_1}/"
echoes the numbers in a HERE string to the loop and pipes the output thru sed to replace the numbers with variables $a $b $c, so your grep search pattern will be the numbers from HERE string and will never match.
sed -r "s/(.)/\1\n/g" <<< $line
puts a newline after each character. This will never result in 4 after | sort | uniq | wc -l
, except you echo a three digit number with a leading space to the loop.
Re: grep using a variable
Posted: Sun Nov 17, 2024 4:47 pm
by AntonioPt
@HerrBert i found the solution and less hard work thxx for the tips,.... soon i hope to share my full code and final idea
grep -Pzo '(?s)\[Desktop\s+Entry\].*?\nName=[^\n]*' ${FP} | sed -rn '$ s/^Name=(.*)/\1/p' | tr '\0' '\n'
Re: grep using a variable
Posted: Sun Nov 17, 2024 10:03 pm
by AntonioPt
hi @HerrBert tell me some if you can
so now my gold is to grab from [Desktop Entry] till Name and then add new Name with new Language
Ex
[Desktop Entry]
Version=1.0
Type=Application
Name=Sublime Text
Name[pt]=Sublime Text
im using this sed but aint working since gonna add in all names
Code: Select all
sed -i "/^Name=.*/s/$/\nName\[${USER_LANG}\]=$(gettext "${UNIV_DATA}")/" ${FP}
and sublime text has more then one Name in it
gonna share sublime_txt desktop file
[Desktop Entry]
Version=1.0
Type=Application
Name=Sublime Text
Name[pt]=Sublime Text
GenericName=Text Editor
Comment=Sophisticated text editor for code, markup and prose
Exec=subl
Terminal=false
MimeType=text/plain;
Icon=sublime-text.png
Categories=TextEditor
StartupNotify=true
Actions=Window;Document;
[Desktop Action Document]
Name=New File
Name[pt]=Sublime Text
Exec=subl
OnlyShowIn=Unity;
[Desktop Action Window]
Name=New Window
Name[pt]=Sublime Text
Exec=subl
OnlyShowIn=Unity;
Re: grep using a variable
Posted: Mon Nov 18, 2024 12:23 pm
by Burunduk
AntonioPt wrote: ↑Sun Nov 17, 2024 10:03 pm
im using this sed but aint working since gonna add in all names
You can limit the range of lines where sed will search for a "Name=":
Code: Select all
sed -i -e '/^\[.*Entry/,/^\[/{/^Name=/a\' -e "the line you want to add" -e '}' sample.desktop
Re: grep using a variable
Posted: Mon Nov 18, 2024 1:06 pm
by AntonioPt
Burunduk wrote: ↑Mon Nov 18, 2024 12:23 pm
AntonioPt wrote: ↑Sun Nov 17, 2024 10:03 pm
im using this sed but aint working since gonna add in all names
You can limit the range of lines where sed will search for a "Name=":
Code: Select all
sed -i -e '/^\[.*Entry/,/^\[/{/^Name=/a\' -e "the line you want to add" -e '}' sample.desktop
my solution
Code: Select all
sed -i "/\[Desktop Entry\]/s/$/\nName\[${USER_LANG}\]=$(gettext "${UNIV_DATA}")/" ${FP}
i all ready got a solution the only issue for me its that will not be organized beside that works like i want as well but gonna test your way
Thank you gonna test it
Re: grep using a variable
Posted: Mon Nov 18, 2024 1:13 pm
by AntonioPt
Hello @Burunduk ,
Works 100% like i wanna it thankyou so much now i can move on
Re: grep using a variable
Posted: Mon Nov 18, 2024 2:19 pm
by MochiMoppel
Burunduk wrote: ↑Mon Nov 18, 2024 12:23 pmYou can limit the range of lines where sed will search for a "Name=":
Code: Select all
sed -i -e '/^\[.*Entry/,/^\[/{/^Name=/a\' -e "the line you want to add" -e '}' sample.desktop
I struggled and failed to make the a (append text lines) command work within an address range. It apparently works since you used the -e option which I never use because so far I've never seen a reason to use it. Maybe I should make friends with this option?
My solution avoids -e and as far as I can see achieves the same result with somewhat simpler code:
Code: Select all
sed -i "
/Entry\]$/,/\]$/ s/^Name=.*/&\nName[ja]=texto A/
/Document\]$/,/\]$/ s/^Name=.*/&\nName[ja]=texto B/
/Window\]$/,/\]$/ s/^Name=.*/&\nName[ja]=texto C/
" sample.desktop
Re: grep using a variable
Posted: Mon Nov 18, 2024 3:20 pm
by AntonioPt
MochiMoppel wrote: ↑Mon Nov 18, 2024 2:19 pm
Burunduk wrote: ↑Mon Nov 18, 2024 12:23 pmYou can limit the range of lines where sed will search for a "Name=":
Code: Select all
sed -i -e '/^\[.*Entry/,/^\[/{/^Name=/a\' -e "the line you want to add" -e '}' sample.desktop
I struggled and failed to make the a (append text lines) command work within an address range. It apparently works since you used the -e option which I never use because so far I've never seen a reason to use it. Maybe I should make friends with this option?
My solution avoids -e and as far as I can see achieves the same result with somewhat simpler code:
Code: Select all
sed -i "
/Entry\]$/,/\]$/ s/^Name=.*/&\nName[ja]=texto A/
/Document\]$/,/\]$/ s/^Name=.*/&\nName[ja]=texto B/
/Window\]$/,/\]$/ s/^Name=.*/&\nName[ja]=texto C/
" sample.desktop
Hi @MochiMoppel thxx for your tips but @Burunduk tip is better because the way im doing its to check one by one so that later i can translate desktop without replacing full file like Lang Packs do i all ready have a sample script that works needs to be improve now with a gui or via terminal
Let me share it
Just keep it in mind beta and more to do
To Collect all Names from Desktop and add translation if have all so and make a po file
http://patinsemlinhaslalom.altervista.o ... lect_all_2
To update desktop files in this demo will be Portuguese and the desktop files are in apps folder so that i don't screw up Original files just add mo file to pt folder and test it
http://patinsemlinhaslalom.altervista.o ... esk_update
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 12:19 am
by AntonioPt
Need a Small help @Burunduk , @MochiMoppel and @HerrBert
add ENTRY and NAME as vars since i wanna do all in a dynamic way but sed ... i even change ' to " but no results
sed: -e expression #2, char 72: extra characters after command
sed: -e expression #2, char 79: extra characters after command
sed: -e expression #2, char 75: extra characters after command
sed: -e expression #2, char 84: extra characters after command
sed -i -e '/^\[${ENTRY}/,/^\[/{/^${NAME}=/a\' -e "${NAME}\[${USER_LANG}\]=$(gettext "${UNIV_DATA}")" -e '}' ${FP}
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 3:10 am
by Flash
Antonio, if the subject of your last post is different from the rest of this thread, please start a new thread with your post instead of confusing things by adding it to this one. Thanks.
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 7:07 am
by Burunduk
AntonioPt wrote: ↑Wed Nov 20, 2024 12:19 am
... i even change ' to " but no results
You've got an error - this is a result too.
'
and "
are quite different in bash.
'$var'
is just a literal string, no expansion happens;
'a\'
is OK but "a\"
is wrong - the backslash escapes the quote, it should be "a\\"
;
Do you expect me to guess what your variables may contain? It should be only one line after the a\
command - no newline characters (or they should be escaped). If for some reason gettext returns more than one line, this may result in the "extra characters after command" error.
Maybe turn on the tracing mode (set -x
) to see what you get after the expansions.
The -e option is here only to keep the whole thing on the same line. It works also without -e:
Code: Select all
ENTRY="Desktop Entry"
NAME=Name
USER_LANG=en
SINGLE_LINE="text editor"
sed -i "/^\[$ENTRY/,/^\[/{/^$NAME=/a\
${NAME}\[${USER_LANG}\]=$SINGLE_LINE
}" sample.desktop
but it's an "extra chars" error when SINGLE_LINE=$'text editor\nname'
.
It's also possible to modify the MochiMoppel's code, just keep one of the three s commands and add your variables.
---
@MochiMoppel It's possible to eliminate "e" (if you really hate that letter) even from the sed's name, and it still works:
Code: Select all
sd '(?s)Entry\]\n.*?Name=.*?\n' '${0}Name[eo]=redaktilo\n' sample.desktop
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 7:16 am
by some1
Perhaps
sed using a variable
??
Might be about the importance of flyspecs.
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 7:49 am
by MochiMoppel
@AntonioPt The code you posted doesn't produce errors with my test data but it doesn't work either. Among other changes you eliminated the .* from Burunduk's code, which would allow your ENTRY variable to contain only a portion of the section name (e.g. 'Action Document' instead of 'Desktop Action Document' ).
This should work:
sed -e "/\[.*${ENTRY}/,/^\[/{/^${NAME}=/a\\" -e "${NAME}\[${USER_LANG}\]=$(gettext "${UNIV_DATA}")" -e '}' ${FP}
I know you don't like it, but my code also works
sed "/\[.*$ENTRY/,/^\[/ s/^$NAME=.*/&\n$NAME[$USER_LANG]=$(gettext "$UNIV_DATA")/" $FP
Tested with
ENTRY='Action Document'
NAME=Name
USER_LANG=ja
UNIV_DATA='some new text'
Add the -i option only after you are certain that the code produces the results you expect
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 12:42 pm
by AntonioPt
Thankyou all gonna try it and see the results
@Burunduk my idea was to avoid to write some code over and over so i did two list one of Desktop Entrys and another list of Names to grab and then 2 loops then your sed, but i allready notice that in BASH since are completed different from other languages and its a fresh new lang for me so sorry for any stupid question,... and thankyou for your explanations,..
@MochiMoppel yeah i all so notice my grep to match Desktop Entry till Name aint working has it should if Name[whatever] doesn't exist so he goes and grab the next Name[whatever] its not that i don't like your code its when i run for the first time Burunduk code work the way i wanna that's all
Guys im learning bash so if you guys notice that i'm asking a stupid question be patience since i don't mean nutting wrong ok ? and thankyou for your comprehension
@Flash since the issue add to with additional code this is why i comment here but thankyou for let me know anyway
Re: grep using a variable (Solved)
Posted: Wed Nov 20, 2024 9:25 pm
by Flash