Search xml for a value using sed - sed

I have a below xml file
<documents>
<document><title>some title1</title><abstract>Some abstract1</abstract></document>
<document><title>some title2</title><abstract>Some abstract2</abstract></document>
<document><title>some title3</title><abstract>Some abstract3</abstract></document>
<document><title>some title4</title><abstract>Some abstract4</abstract></document>
</documents>
I am trying to write a ksh script to fetch the abstract value based on title=title4
xmllint , xstartlet is not allowed in my machine (access issues)
I have tried with
sed -n '/abstract/{s/.*<abstract>//;s/<\/abstract.*//;p;}' connections.xml
How to modify this to search based on a title

Based on the example you have given:
sed -n '/title>.*title4<\/title>/{s#.*<abstract>##;s#</abstract>.*##;p}' file
Will give you:
Some abstract4

grep approach:
grep -Poz '<title>.*?title4</title><abstract>\K[^<>]+(?=</abstract>)' connections.xml && echo ""
The output:
Some abstract4

Related

Cloud Foundry: How do I get the contents of the VCAP_SERVICES environment variable? (and only this variable!)

When I deploy an app to Cloud Foundry and attach it to instances of Cloud Foundry services,
and I use the Cloud Foundry CLI to get the environment variables: cf env my-app,
then I get an output like:
Getting env variables for app my-app in org my-org / space my-space as user#company.com...
System-Provided:
VCAP_SERVICES: {
"service1": [
// ...
],
"service2": [
// ...
]
}
VCAP_APPLICATION: {
// ...
}
User-Provided:
VARIABLE1: value
VARIABLE2: value
Running Environment Variable Groups:
CREDHUB_API: https://credhub.company.com
No staging env variables have been set
How do I filter this output to get only the contents of the environment variable VCAP_SERVICES, so that when I test/debug my app locally, it behaves as if it was attached to the instances of the Cloud Foundry services?
My goal is to write a file named default-env.json containing only:
{
VCAP_SERVICES: {
"service1": [
// ...
],
"service2": [
// ...
]
}
}
Ideally, the command to produce this output should be a zsh one-liner.
cf env my-app | sed -n '/VCAP_SERVICES/,/VCAP_APPLICATION/p' | sed '$d' | sed '1s;^;{\n;' | sed '$s/$/}/' > default-env.json
Explanation
sed -n '/VCAP_SERVICES/,/VCAP_APPLICATION/p'
keeps only the section between the regular expressions VCAP_SERVICES and VCAP_APPLICATION.
sed '$d' deletes the last line (the line containing VCAP_APPLICATION).
sed '1s;^;{\n;' prepends {\n to the first line.
sed '$s/$/}/' appends } to the end of the file.
Credits
Handy one-liners for SED
BASH Prepend A Text / Lines To a File
SED: insert text after the last line?
Another option would be:
cf curl "/v2/apps/$(cf app --guid my-super-cool-app)/env" | jq -r '.system_env_json.VCAP_SERVICES'
Explanation:
$(cf app --guid <your-app-name) will run in a subshell and get the app guid for your app. You could alternatively just replace that bit with the guid for your app, if you know it already (it'll make the command faster).
cf curl "/v2/apps/<guid>/env" will return all of the env variables for your app.
jq -r '.system_env_json.VCAP_SERVICES' picks out the bit you want.
You could optionally redirect output to a file.
Other interesting bits from that API:
.application_env_json.VCAP_APPLICATION would give you VCAP_APPLICATION.
'.environment_json' would give you any env variables you've set

How format mac address inside json array

Need help in getting below the mac address inside the json file to re-reformatted using sed
cat 251.json
cat /tmp/251.json
[
"08:f1:ea:6d:03:3c",
"08:f1:ea:6d:03:3d",
"08:f1:ea:6d:03:3e",
"08:f1:ea:6d:03:3f",
"b8:83:03:81:4b:20",
"b8:83:03:81:4b:21",
"b8:83:03:84:d5:1c",
"b8:83:03:84:d5:1d"
]
The expected format is
[
"08f1.ea6d.033c",
"08f1.ea6d.033d",
"08f1.ea6d.033e",
"08f1.ea6d.033f",
"b883.0381.4b20",
"b883.0381.4b21",
"b883.0384.d51c",
"b883.0384.d51d"
]
This should work:
sed -E 's/:(.{2}):(.{2}):(.{2}):(.{2}):/\1.\2\3.\4/g' /tmp/251.json
In this way, you get the output to stdout. If you want to modify the file, add the -i option. You can check the result here.

Search and Replace within XML tag via sed over multiple lines

i want to change the value of a specific XML tag property.
There are many questions about how to handle sed, but the problem here is the newline within the tag.
I want to change the value after name= and it must be searched in the <package ... > tag
XMLStarlet is not an option.
Coverage.xml
<package branch-rate="0.031746031746" complexity="0.0"
line-rate="0.159420289855" name="include">
<classes>
<class branch-rate="0.0" complexity="0.0"
My best try so far:
sed -n '/<package/ {
:a
n
/<classes>/q
s/name=/xxxx/g
}' coverage.xml
Do you have an idea?
UPDATE 2: More of coverage.xml with approach of #RavinderSingh13
<package branch-rate="0.031746031746" complexity="0.0"
line-rate="0.159420289855" name="NEW_VALUE">
<classes>
<class branch-rate="0.0" complexity="0.0"
filename="NEW_VALUE"
name="NEW_VALUE">
If you are ok with awk, then as per your shown samples could you please try following once.(this will look only for package tag and its name value and for rest tags it will not do anything)
awk '/^>/{flag=""} /<package/{flag=1} flag && /name=/{sub(/name=.*\"/,"name=\"NEW_VALUE\"")} 1' Input_file
In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file to above code too.
I just did a little tweak using a for loop.
LINES=`awk '/<package /{print NR+1}' coverage.xml`
for i in ${LINES};
do
echo $i
sed -i ''"${i}"'s/name=.*/name="NEW_VALUE"/' coverage.xml;
done
The NR+1 helps to reach the second line in the package tag.

Using sed, delete from specific line until first match(not included)

I have some data looks like
1:Alice 2313
2:Desctop 456
3:Cook 111
4:.filename 50
...
...
100:Good 3
Dir num:10
File num:90
...
...
I want to delete all lines from specific line(ex. line 3) until the line "Dir num:" show up.
The idea output should be(according above example):
1:Alice 2313
2:Desctop 456
Dir num:10
File num:90
...
...
I have google several solutions likesed -i '/somestring/,$!d' file.
But these solutions are not suitable because of the specific line where deletion satarting.
How can I do this in 1 command without any tmp file?
Forgive my poor English, I'm not native English speaker.
You need to specify the address range from the specified line number (3) to the line matching the pattern (/Dir num/). However, it's not quite as simple as
sed '3,/Dir num/ d' file
because that will delete the "Dir num" line. Try this instead:
sed '3,/Dir num/ {/Dir num/! d}' file
That will, for the lines in the range, check that the line does not match the pattern: is the pattern is not matched, delete it.
Use the range: /pattern1/,/pattern2/ option of sed
$ sed -e '/2:Desctop 456/,/Dir num:10/{//!d}' inputFile
1:Alice 2313
2:Desctop 456
Dir num:10
File num:90
...
...

Capture repeating groups using sed

I'm looking to use sed to capture repeating groups in order to parse a log line
echo "14:14:52.449 [thread] INFO LOGGER - SYMBOL: FIELD1[1.0] FIELD2[2] FIELD3[141452 (2016-11-24 14:14:52.000)] FIELD4[4]" | sed -E "s/(\d\d:\d\d:\d\d.\d\d\d )(.*?\-)( .*?\:)(.*)( FIELD3\[.*?\]).*/\\1\\3\\5/"
I'm looking to capture only the following fields
14:14:52.449 SYMBOL FIELD3[141452 (2016-11-24 14:14:52.000)]
However I get the entire line back. Any help is deeply appreciated
14:14:52.449 [thread] INFO LOGGER - SYMBOL: FIELD1[1.0] FIELD2[2] FIELD3[141452 (2016-11-24 14:14:52.000)] FIELD4[4]
With sed:
sed -E "s/^(([0-9]{2}:){2}[0-9]{2}\.[0-9]{3}).*( [^:]*):.*( FIELD3\[[^]]*\]).*/\1\3\4/"