remove an alias if it exists in zsh [duplicate] - if-statement

This question already has answers here:
why in an 'if' statement 'then' has to be in the next line in bash?
(4 answers)
Closed 2 years ago.
I'm trying to remove an alias set to the keyword clear in zsh, but first i would like to check if it exists.
here is what i've tried:
if whence -w clear | grep "alias" then
unalias clear
fi
but I can't figure out how to make if work with the output of the piline.

Did you forget a semicolon after grep "alias"? The following works just fine in zsh.
% alias clear=something
% alias
clear=something
% if whence -w clear | grep "alias"; then
unalias clear
fi
clear: alias
% alias
%

Related

Regex group match using shell [duplicate]

This question already has answers here:
How do I use grep to extract a specific field value from lines
(2 answers)
Closed 3 years ago.
I am trying to match a pattern and set that as a variable.
I have a file with many "value=key". I want to find the value for key "fizz".
In the file I have this string
fizz="something_cool"
I try to parse it as:
cat file | grep fizz="(.*)"
I was thinking it would give me the group output, and then I would be able to use $1 to select it.
I also play with escaping characters and sed and awk. But I could not manage to get it working.
You need to enable extended regex for using unescaped ( and ) and quote pattern properly to make it:
grep -E 'fizz="(.*)"' file
However awk might be better choice here since it will do both search and filter in same command.
You may just use:
awk -F= '$1 == "fizz" {gsub(/"/, "", $2); print $2}' file
something_cool

What to put in an if/then statement to test whether or not a string contains a pattern [duplicate]

This question already has an answer here:
Bash comparison operator always true
(1 answer)
Closed 3 years ago.
I am trying to find out how to test whether a string contains a pattern or not. I have as yet been unsuccessful.
I have tried using =~ but it does not seem to work.
IFS=;
count=0;
lspciarray=();
mylspciarrays=($(lspci -v));
for lspciarrays in "${mylspciarrays[#]}";
do lspciarray[$count]=$lspciarrays;
if [[ lspciarray[$count]=~^[0-9a-f][0-9a-f][:][0-9a-f][0-9a-f][.][0-9a-f]* ]];
then echo ${lspciarray[$count]};
fi;
((count++));
done;
unset IFS;
I am expecting to get only the lines that contain the pattern ^[0-9a-f][0-9a-f][:][0-9a-f][0-9a-f][.][0-9a-f]* but instead I still get the complete output from lspci -v
[[ $anything=~$anything ]]]
is always true, because =~ isn't treated as an operator without spaces around it; instead, you're checking whether the result of your expansions is or is not an empty string. Because it has a constant component (the =~), it's always non-empty, so the operation's result is always true.

Regex Substitution with only a fraction of string known [duplicate]

This question already has an answer here:
Replace all IP addresses in a file to a specified string
(1 answer)
Closed 4 years ago.
I have a string with contains \r\nexports.host = "192.168.24.76" The IP address here is not fixed and is variable.
I want to use regex to find this expression and replace the IP address in bash.
For example, make this \r\nexports.host = "192.168.24.77" and then change this to \r\nexports.host = "192.168.24.78" in the next iteration. Is there anyway I can do this with regex in a bash script?
For now, I am doing it like: sed -i -e 's/"192.168.24.76"/"'$1'"/g' ./dist/config.*.js but just found out that the initial IP address will not be static and can be any value
Could you please try following.
awk -v new_ip="your_new_ip" '
match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){
print substr($0,1,RSTART-1) new_ip substr($0,RSTART+RLENGTH);
next
}
1' Input_file
In case you want to save output's value into Input_file itself then append > temp_file && mv temp_file Input_file to above code too.

Regular expression not matching correctly in sed command [duplicate]

This question already has answers here:
Why doesn't `\d` work in regular expressions in sed? [duplicate]
(3 answers)
Closed 5 years ago.
I am trying to replace some character strings using sed in batch console. My input consists in a file with lines like these:
$ head qiimetax_sorted.txt
A61579.1.1437
D_0__Bacteria;D_1__Thermotogae;D_2__Thermotogae;D_3__Thermotogales;
D_4__Fervidobacteriaceae;D_5__Fervidobacterium;Ambiguous_taxa;D_7__;
D_8__;D_9__;D_10__;D_11__;D_12__;D_13__;D_14__
AAAA02020712.626.2096
D_0__Bacteria;D_1__Proteobacteria;D_2__Alphaproteobacteria;D_3__Rhizobiales;
D_4__Bradyrhizobiaceae;D_5__uncultured;D_6__Oryza sativa
Indica Group (long-grained rice);D_7__;D_8__;D_9__;D_10__;D_11__;D_12__;
D_13__;D_14__
Now I'm trying to erase the 'D_number__' string before the names with this sed command and it is not replacing anything:
sed -r 's/D_\d+__//g' qiimetax_sorted.txt > qiimesed.txt
Any idea of which is the problem?
Thanks!
Your regex syntax is perl like.
So if you want to keep it :
perl -pe 's/D_\d+__//g' qiimetax_sorted.txt > qiimesed.tx
or with sed :
sed -r 's/D_[0-9]+__//g' qiimetax_sorted.txt > qiimesed.tx

Changing file extensions with sed [duplicate]

This question already has answers here:
How to use sed to change file extensions?
(7 answers)
Closed 5 years ago.
If the arguments are files, I want to change their extensions to .file.
That's what I got:
#!/bin/bash
while [ $# -gt 0 ]
do
if [ -f $1 ]
then
sed -i -e "s/\(.*\)\(\.\)\(.*\)/\1\2file" $1
fi
shift
done
The script is running, but it doesn't do anything. Another problem is that file hasn't any extension, my sed command will not work, right? Please help.
sed is for manipulating the contents of files, not the filename itself.
Option 1, taken from this answer by John Smith:
filename="file.ext1"
mv "${filename}" "${filename/%ext1/ext2}"
Option 2, taken from this answer by chooban:
rename 's/\.ext/\.newext/' ./*.ext
Option 3, taken from this answer by David W.:
$ find . -name "*.ext1" -print0 | while read -d $'\0' file
do
mv $file "${file%.*}.ext2"
done
and more is here.
UPDATE : (in comment asked what % and {} doing?)
"${variable}othter_chars" > if you want expand a variable in string you can use it. and %.* in {} means take the value of variable strip off the pattern .* from the tail of the value for example if your variable be filename.txt "${variable%.*} return just filename.
Using a shell function to wrap a sed evaluate (e) command:
mvext ()
{
ext="$1";
while shift && [ "$1" ]; do
sed 's/.*/mv -iv "&" "&/
s/\(.*\)\.[^.]*$/\1/
s/.*/&\.'"${ext}"'"/e' <<< "$1";
done
}
Tests, given files bah and boo, and the extension should be .file, which is then changed to .buzz:
mvext file bah boo
mvext buzz b*.file
Output:
'bah' -> 'bah.file'
'boo' -> 'boo.file'
'bah.file' -> 'bah.buzz'
'boo.file' -> 'boo.buzz'
How it works:
The first arg is the file extension, which is stored in $ext.
The while loop parses each file name separately, since a name might include escaped spaces and whatnot. If the filenames were certain to have not such escaped spaces, the while loop could probably be avoided.
sed reads standard input, provided by a bash here string <<< "$1".
The sed code changes each name foo.bar (or even just plain foo) to the string "mv -iv foo.bar
foo.file" then runs that string with the evaluate command. The -iv options show what's been moved and prompts if an existing file might be overwritten.