Trying to grep instance id from within a text file - linux

I have a text file bak.txt with the following content:
^[[34mINFO^[[0m[0000] Your engine version 1.11.1-cs1 is compatible
^[[34mINFO^[[0m[0000] We detected local components of UCP instance H7LQ:WKR5:G2PX:4F3V:JQ47:WCIG:JV4W:V6SE:4WMR:TLZN:XYWH:MIEQ
^[[31mFATA^[[0m[0000] Re-run the command with "--id H7LQ:WKR5:G2PX:4F3V:JQ47:WCIG:JV4W:V6SE:4WMR:TLZN:XYWH:MIEQ" or --interactive to confirm you want to upgrade this UCP instance.
I am now trying to grep the UCP instance value from bak.txt file using the following command:
grep -Po '(?<=instance=)[^"]*' bak.txt
It is not working. Please suggest the correct way.

Try this grep:
grep -oP '(?<=instance )[^"]+' bak.txt

Related

Where is the config file store for the xfce4-plugin "notification area" alias "systray"

I am on Qubes OS. That means fedora-25 as dom0. I would like to change the configs for "notification area" alias "systray" plugin of xfce. How can I do it. I would like to delete/add one item.
The Gui only gives me the option to hide with ugly arrow on the side or to "clear all known applications". However, regarding the last option I am afraid to lose the notification area as it is and never get it back.
I looked with the "find" command for "xfce4" and "xfce4-plugins" and so on. All the files I could find, e.g. in ~/.config/xfce4, could not help me. I can nowhere find a config file for the plugin.
Thanks in advance :)
Known applications is stored as an array in xfconf, in the xfce4-panel channel and under the property /plugins/plugin-[id]/known-items, where the plugin id is dynamic and depends on the order plugins were added to panel.
You could hack your way messing with ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml but I strongly advise you not to, instead use xfconf-query to read and set values.
I'm going to write down some snippets below so you can use them to craft a script that suits your needs:
# Find the plugin id, can be empty if systray is not present
xfconf-query -c xfce4-panel -p /plugins -l -v | grep systray | grep -Po "plugin-\\d+" | head -n1
# Get array of current known apps
xfconf-query -c xfce4-panel -p /plugins/$PLUGIN_ID/known-items | tail -n +3
# Set array of known apps
xfconf-query -c xfce4-panel -p /plugins/$PLUGIN_ID/known-items -t string -s value1 -t string -s value2 ...

Running a Script to change a wp-config file DB Name

I teach students how to fix wordpress sites and I would like to write a script that looks at what information they have in their wp-config.php and add a single letter or number to the database name.
For example the line is such
define('DB_NAME', 'cpanelUser_NameofDB');
I would like to add a number or a line to the end of NameofDB
I can use this to isolate the cpanelUser_NameofDB
grep -i 'DB_NAME' wp-config.php | cut -d"'" -f4
but I'm not sure how to add information, nor if this is the correct script I should run to get there. I would also like it to not matter what the name of the database is since it will be ran on multiple sites. I'm sure I could use regex but I'm not too versed in that and would not know where to start. Help please!
You can use WP CLI on the server:
Go to the website root:
cd /var/www/mysite.com/htdocs
...and list all the wp-config values:
wp config list
...or set the value you need to change:
sudo wp config set DB_NAME put_my_custom_db_name_here --allow-root
See more WP CONFIG features here:
https://developer.wordpress.org/cli/commands/config/
You can use sed for this purpose to fix this line:
#!/bin/bash
id=34
sed -i "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" wp-config.php
flag -i means that we do change in file directly.
Alternatively you can make template file and generate new file while sed works on stdin and stdout
#!/bin/bash
id=34
cat wp-config.php-template| sed "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" > wp-config.php.$id

How to use Node.js repl with file as input

I wish to use a js file input as node.js repl, as if it was a command line code:
node -e '<my code>', but with a <filename> instead of an inline code.
How can this be done as the file name as an input, without needing to use another file (such as fs.readFileSync(..))
One way is to use the REPL's .load command and Node's -i flag.
echo ".load test.js" | node -i
Outputs:
$ echo ".load test.js" | node -i
Welcome to Node.js v12.8.1.
Type ".help" for more information.
> .load test.js
2 + 2;
4

How to parse xml sibling element value with SED [duplicate]

This question already has answers here:
How to parse XML using shellscript? [duplicate]
(10 answers)
Closed 7 years ago.
I have a xml file which contains following text:
<Cluster>
<Name>CLS_20</Name>
<JMXUserName>admin</JMXUserName>
<JMXPassword>mypwd</JMXPassword>
</Cluster>
<Server>
<Name>Server_20</Name>
<IpAddress>a.b.c.d</IpAddress>
<Port>1234</Port>
</Server>
<Server>
<Name>Server_21</Name>
<IpAddress>e.f.g.h</IpAddress>
<Port>1234</Port>
</Server>
I have the IP address of the server (a.b.c.d)
I want to retrieve name of the server (Server_20)
How can this be achieved with SED
Or with any other linux command.
Please help.
Sed is to use with caution on xml due to lot of variation in structure but based on this sample
sed -n '
/<Server>/ h
/<Server>/,\#</Server># {
H
\#</Server># {
x
s#.*<Name>\([^<]*\)<.*IpAddress>a.b.c.d<.*#\1#p
}
}' YourFile
Principle:
don't print unless explicitly asked
load section Server in buffer
when reaching end of section
change the whole by keeping only name attribut if the ipadresse value is the same as a.b.c and in this case print the result
Do not do this with sed; it will break horribly when benign formatting changes happen to the XML.
Use a proper XML-parsing tool. For example with xmlstarlet:
xmlstarlet sel -t -c '//Server[IpAddress="a.b.c.d"]/Name/node()' -n filename.xml
or with xmllint:
xmllint --xpath '//Server[IpAddress="a.b.c.d"]/Name/node()' filename.xml
or with old versions of xmllint that don't yet understand --xpath (if you are tempted to use this I encourage you to look at other tools):
echo 'cat //Server[IpAddress="a.b.c.d"]/Name/node()' | xmllint --shell filename.xml | sed '1d;$d'
or with the xpath utility from the XML::XPath Perl library:
xpath -q -e '//Server[IpAddress="a.b.c.d"]/Name/node()' filename.xml
...or with any of three dozen (dozen) other XML tools.
The heart of this is the XPath expression //Server[IpAddress="a.b.c.d"]/Name/node(). This consists of:
//Server refers to a Server node anywhere in the document
//Server/Name refers to a Name node that is the child of such a Server node
//Server/Name/node() refers to the contents of such a Name node
//Server[IpAddress="a.b.c.d"] refers to a server node that satisfies the condition IpAddress="a.b.c.d", which means that it has a child IpAddress node that contains a.b.c.d
Putting all that together, //Server[IpAddress="a.b.c.d"]/Name/node() refers to the contents of a Name node that is the child of a Server node anywhere in the document that has an IpAddress child node that contains a.b.c.d.

How to fetch the tags for ec2-describe-instances in a shell script

I want to extract the instance ID and the tags from the result of a command ec2-describe-instances and want to store the result in a text file. The result set gives :
But i want the tags owner and cost.centre also to be fetched
Kindly guide me how to do that
this will help to find instance-id
$ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-xxx | awk '{ print $8 }' | sort -n | grep "i-"
i-4115d38c
i-5d534697
i-6e679a45
i-7a659851
i-8d6bae40
i-cd6f9000
i-d264ad1e
i-d5888618
i-e2332e2e
ps considering that you already configured / run "aws configure"
If I understand the question correctly, I think you just need to add expressions to your 2nd grep:
ec2-describe-instances | grep -i "tag" | grep -i -e "name" -e "owner" -e "cost.centre"
This is going to be unnecessarily complicated to do in a shell script. Here are some suggestion:
you are using ec2cli. Don't use that. Use AWS-CLI instead. Because parsing the output in ec2cli is a pain. Whereas AWS-CLI provides output in JSON, it is way more easier to parse. Also, AWS is going to support AWS-CLI only henceforth.
The information that you need a perfect use-case for using a hash. You can either install and run AWs-CLI commands via a perl script and then capture the output in a hash. Perl is very powerful for handling such data structure.
OR, you can use one of the SDKs from AWS (I use Ruby SDK) and then capture the whole information in a hash and then print it the way you want.
Bottom line is, you need to capture the tags in a hash to make your life easier. Ans this becomes more and more prominent when you have multiple tags.
Using awk
ec2-describe-instances |awk 'BEGIN{IGNORECASE=1}/(name|owner|cost.center)/&&/tag/'
TAG instance i-c4 Name Rii_Win_SAML
TAG instance i-c42 Owner Rii Pandey
Here is another way to do without using jq and other parsing tools.
ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value | [0],Tags[?Key==`cost.centre`].Value | [0]]' --output text
This should work:
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "owner"},{Key: "costcenter"}]}))|"Instance ID: \(.InstanceId) Owner: \(.Tags[]|select(.Key=="owner")|.Value), Cost Center: \(.Tags[]|select(.Key=="costcenter")|.Value)"'
TL;DR: The way AWS does tags is a living nightmare, even with jq
Also, use AWS CLI, not old, unsupported tools.

Resources