The script is as below:
a=`sed '/^project_folder=/!d;s/.*=//' conf.ini | tr -d '\r'`
b=`sed '/^version.versionLicense=/!d;s/.*=//' conf.properties`
path=${a}/${b}/env/123456/bin
echo $path
the content of conf.ini is project_folder=/data/AUTO-PUBLISH/iPEMS_01
the content of conf.properties is version.versionLicense=3.1.0.3
the output is /env/123456/binISH/iPEMS_01/3.1.0.3
my expected output is /data/AUTO-PUBLISH/iPEMS_01/3.1.0.3/env/123456/bin
Why this happened?
You should not use sed for what you are doing grep would be more than enough as you can see hereunder:
$ more conf.properties conf.ini
::::::::::::::
conf.properties
::::::::::::::
version.versionLicense=3.1.0.3
::::::::::::::
conf.ini
::::::::::::::
$ grep -oP '(?<=^project_folder=).*' conf.ini | tr -d '\r'
/data/AUTO-PUBLISH/iPEMS_01
$ grep -oP '(?<=version\.versionLicense=).*' conf.properties | tr -d '\r'
3.1.0.3
Your script becomes:
a=$(grep -oP '(?<=^project_folder=).*' conf.ini | tr -d '\r')
b=$(grep -oP '(?<=version\.versionLicense=).*' conf.properties | tr -d '\r')
path=${a}/{$b}/env/123456/bin
echo $path
And the output is:
/data/AUTO-PUBLISH/iPEMS_01/{3.1.0.3}/env/123456/bin
conf.properties will have \r, which will part of $path now.
Related
I have a command to grep a file with fullpath that contain a "TypeId: 0", here is the command
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'
and here is the result:
/home/username/app/data/store/0/part/.mv/521/1673332792072/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml.old
Now I confuse how to grep "numofvertice" from each file from that list.
Anyone have an idea to solve this?
You could try this:
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'|xargs -I{} grep "numofvertice" {}
Like this (GNU grep):
<STDIN> | grep -oP '\b\S+\.yaml' | xargs cat
Or with ack:
cd /home/username/app/data/store/0/part/.mv
ack -wl -e "TypeId: 0" | xargs cat
From ack --help:
-l, --files-with-matches
Only print filenames containing matches
I have a simple shell script for installing Jenkins plugins:
installPlugin() {
{...}
}
for f in ${plugin_dir}/*.hpi ; do
#without optionals
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
I'm calling this script from a Dockerfile like so:
RUN JENKINS_HOME=$JENKINS_HOME \
http_proxy=$http_proxy \
https_proxy=$https_proxy \
$JENKINS_HOME/install_plugin.sh \
ace-editor:1.1 \
Even though the unzip routine is not installed in the Dockerfile, when I run docker build the output is still SUCCESSFUL even though unzip is missing. I would like to fail the build if the following step fails:
deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
Any help is much appreciated!
LE
The script outputs the following:
/var/jenkins/install_plugin.sh: line 62: unzip: command not found
deps=
However, the docker build doesn't fail even if that script failed:
Successfully built 54f5a5ec567d
I ended up doing an empty check like this:
for f in ${plugin_dir}/*.hpi ; do
manifestMf=$(unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r')
if [ $manifestMf=="" ]; then
echo "Invalid META-INF/MANIFEST inside $f"
exit 1
fi;
#without optionals
deps=$(echo $manifestMf | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | grep -v "resolution:=optional" | tr '\n' ' ' )
# with optionals
#deps=$( unzip -p ${f} META-INF/MANIFEST.MF | tr -d '\r' | sed -e ':a;N;$!ba;s/\n //g' | grep -e "^Plugin-Dependencies: " | awk '{ print $2 }' | tr ',' '\n' | tr '\n' ' ' )
for plugin in $deps; do
installPlugin "$plugin" 1 && changed=1
done
done
done
I have a users.txt file with the following content:
[builders.ca]
UniqueID=DB#LqlFP
Login=buildca
Pass=5nFvZLwx
RelativePath=1
[DeluxeDoors.ca]
UniqueID=RgOkvU4Z
Login=DeluxDSM
Pass=L9pP3iaK
RelativePath=1
[Sonicwall.com]
UniqueID=JVpFoXad
Login=firewall
Pass=azasadsa
RelativePath=1
I wrote a script to replace all the passwords with random passwords in the file.
The script is:
users=($(cat users.txt | grep 'Login=' | cut -c 7-))
for user in "${users[#]}"; do
pass=$(cat users.txt | grep -A 2 $user | grep 'Pass' | cut -c 6-)
new_pass=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-zA-Z0-9' | head -c 8)
echo $pass;
echo $new_pass;
#perl -pi -e 's/$pass/$new_pass/g' users.txt
sed -i '' 's/"${pass}"/"${new_pass}"/g' users.txt
done
But it is not updating the passwords in the users.txt.
Any help would be highly appreciated.
sed -i '' 's/'"${pass}/${new_pass}"'/g' users.txt
^ ^
These seem to be missing, so the sed was getting ${pass} and ${new_pass} as literal strings, not the expanded variables.
Working with data and trying to convert a tab-delimited to control-a in a shell script. Using command-line, I would represent control-a by doing the following sequence, 'control-v, control-a'.
Here is my code in a .sh:
echo "tab delimited query here" | sed 's/ /'\001'/g' > $OUTPUT_FILE
However, this doesn't work. I've also tried the following:
'\x001'
x1
'\x1'
You can use tr here:
echo $'tab\tdelimited\tquery\there' | tr '\t' $'\x01'
To demonstrate that it has been replaced:
echo $'tab\tdelimited\tquery\there' | tr '\t' $'\x01' | cat -vte
Output:
tab^Adelimited^Aquery^Ahere$
sed alternative:
echo $'tab\tdelimited\tquery\there' | sed $'s/\t/\x01/g'
awk alternative:
echo $'tab\tdelimited\tquery\there' | awk -F '\t' -v OFS='\x01' '{$1=$1} 1'
I have a .txt file. And each line contains Chinese. I want to translate the Chinese to urlencoding.
How can I get it?
txt.file
http://wiki.com/ 中文
http://wiki.com/ 中国
target.file
http://wiki.com/%E4%B8%AD%E6%96%87
http://wiki.com/%E4%B8%AD%E5%9B%BD
I found a shell script way to approach it like this:
echo '中文' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' | tr '[a-z]' '[A-Z]'
So, I wanna embed it in awk like this, but I failed:
awk -F'\t' '{
a=system("echo '"$2"'| tr -d '\n' | xxd -plain | \
sed 's/\(..\)/%\1/g' | tr '[a-z]' '[A-Z]");
print $1a
}' txt.file
I have tried another way to write an outside function and call it in awk, code like this, failed it again.
zh2url()
{
echo $1 | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' | tr '[a-z]' '[A-Z]'
}
export -f zh2url
awk -F'\t' "{a=system(\"zh2url $2\");print $1a}" txt.file
Please implement it with awk command because I actually have another thing need to handle in awk at the same time.
With GNU awk for co-processes, etc.:
$ cat tst.awk
function xlate(old, cmd, new) {
cmd = "xxd -plain"
printf "%s", old |& cmd
close(cmd,"to")
if ( (cmd |& getline rslt) > 0 ) {
new = toupper(gensub(/../,"%&","g",rslt))
}
close(cmd)
return new
}
BEGIN { FS="\t" }
{ print $1 xlate($2) }
$ awk -f tst.awk txt.file
http://wiki.com/%E4%B8%AD%E6%96%87
http://wiki.com/%E4%B8%AD%E5%9B%BD