I need to find a value in file between >STRING<
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file: example here:
<hello>TestFile</hello>
<test>2018-04-30</test>
<greetings>mycomputer</greetings>
**<Iwanthis>6.1.0</Iwanthis>**
<example>20180411</example>
blah blah
I'm looking to grep the value (in this case 6.1.0) from between
6.1.0
is there an way to grep the file to grab the XXXX value?
regards
thanks all........
bash grep string
add a comment |Â
up vote
0
down vote
favorite
I have a file: example here:
<hello>TestFile</hello>
<test>2018-04-30</test>
<greetings>mycomputer</greetings>
**<Iwanthis>6.1.0</Iwanthis>**
<example>20180411</example>
blah blah
I'm looking to grep the value (in this case 6.1.0) from between
6.1.0
is there an way to grep the file to grab the XXXX value?
regards
thanks all........
bash grep string
2
Is this an XML file?
â Stephen Kitt
May 2 at 12:25
Sorry - this a standard file - Not XML
â Mike Smith
May 2 at 12:26
I should mention - I need a solution in Bash - sorry for not mentioning it!
â Mike Smith
May 2 at 12:27
Bash is just a shell, not much of a text editor. YouâÂÂre looking for a command-line solution? Grep, awk, sed, that sort of thing?
â Jeff Schaller
May 2 at 12:28
Yup - any of that would suffice once I can assign the XXXX into a variable to manipulate :P It's the special characters that are confusing me.
â Mike Smith
May 2 at 12:48
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file: example here:
<hello>TestFile</hello>
<test>2018-04-30</test>
<greetings>mycomputer</greetings>
**<Iwanthis>6.1.0</Iwanthis>**
<example>20180411</example>
blah blah
I'm looking to grep the value (in this case 6.1.0) from between
6.1.0
is there an way to grep the file to grab the XXXX value?
regards
thanks all........
bash grep string
I have a file: example here:
<hello>TestFile</hello>
<test>2018-04-30</test>
<greetings>mycomputer</greetings>
**<Iwanthis>6.1.0</Iwanthis>**
<example>20180411</example>
blah blah
I'm looking to grep the value (in this case 6.1.0) from between
6.1.0
is there an way to grep the file to grab the XXXX value?
regards
thanks all........
bash grep string
edited May 2 at 12:27
asked May 2 at 12:24
Mike Smith
11
11
2
Is this an XML file?
â Stephen Kitt
May 2 at 12:25
Sorry - this a standard file - Not XML
â Mike Smith
May 2 at 12:26
I should mention - I need a solution in Bash - sorry for not mentioning it!
â Mike Smith
May 2 at 12:27
Bash is just a shell, not much of a text editor. YouâÂÂre looking for a command-line solution? Grep, awk, sed, that sort of thing?
â Jeff Schaller
May 2 at 12:28
Yup - any of that would suffice once I can assign the XXXX into a variable to manipulate :P It's the special characters that are confusing me.
â Mike Smith
May 2 at 12:48
add a comment |Â
2
Is this an XML file?
â Stephen Kitt
May 2 at 12:25
Sorry - this a standard file - Not XML
â Mike Smith
May 2 at 12:26
I should mention - I need a solution in Bash - sorry for not mentioning it!
â Mike Smith
May 2 at 12:27
Bash is just a shell, not much of a text editor. YouâÂÂre looking for a command-line solution? Grep, awk, sed, that sort of thing?
â Jeff Schaller
May 2 at 12:28
Yup - any of that would suffice once I can assign the XXXX into a variable to manipulate :P It's the special characters that are confusing me.
â Mike Smith
May 2 at 12:48
2
2
Is this an XML file?
â Stephen Kitt
May 2 at 12:25
Is this an XML file?
â Stephen Kitt
May 2 at 12:25
Sorry - this a standard file - Not XML
â Mike Smith
May 2 at 12:26
Sorry - this a standard file - Not XML
â Mike Smith
May 2 at 12:26
I should mention - I need a solution in Bash - sorry for not mentioning it!
â Mike Smith
May 2 at 12:27
I should mention - I need a solution in Bash - sorry for not mentioning it!
â Mike Smith
May 2 at 12:27
Bash is just a shell, not much of a text editor. YouâÂÂre looking for a command-line solution? Grep, awk, sed, that sort of thing?
â Jeff Schaller
May 2 at 12:28
Bash is just a shell, not much of a text editor. YouâÂÂre looking for a command-line solution? Grep, awk, sed, that sort of thing?
â Jeff Schaller
May 2 at 12:28
Yup - any of that would suffice once I can assign the XXXX into a variable to manipulate :P It's the special characters that are confusing me.
â Mike Smith
May 2 at 12:48
Yup - any of that would suffice once I can assign the XXXX into a variable to manipulate :P It's the special characters that are confusing me.
â Mike Smith
May 2 at 12:48
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
If you can trust that there will be one opening tag and one closing tag on that line, you can use this as field separator for awk
:
awk -F "</*Iwanthis>" '(NF > 1)print $2' yourfile
This will fail on a lot of artificial cases. To be more strict, you could use sed
:
sed -n 's_.*<Iwanthis>(.*)</Iwanthis>.*_1_p' yourfile
The -n
suppresses default output, so you just get output on the matching line. I did chose the underscore as delimiter for the s
command to avoid escaping the slash. The s
command will replace the whole line by the part between the opening and closing tag (the 1
refers to the (.*)
part)
Of course you can still create weird cases to make this fail.
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
add a comment |Â
up vote
2
down vote
Sure looks like an XML file. Add a root note and use an XML parser:
$ echo '<r>'; cat file; echo '</r>'; | xmlstarlet sel -t -v //Iwanthis
6.1.0
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you can trust that there will be one opening tag and one closing tag on that line, you can use this as field separator for awk
:
awk -F "</*Iwanthis>" '(NF > 1)print $2' yourfile
This will fail on a lot of artificial cases. To be more strict, you could use sed
:
sed -n 's_.*<Iwanthis>(.*)</Iwanthis>.*_1_p' yourfile
The -n
suppresses default output, so you just get output on the matching line. I did chose the underscore as delimiter for the s
command to avoid escaping the slash. The s
command will replace the whole line by the part between the opening and closing tag (the 1
refers to the (.*)
part)
Of course you can still create weird cases to make this fail.
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
add a comment |Â
up vote
2
down vote
If you can trust that there will be one opening tag and one closing tag on that line, you can use this as field separator for awk
:
awk -F "</*Iwanthis>" '(NF > 1)print $2' yourfile
This will fail on a lot of artificial cases. To be more strict, you could use sed
:
sed -n 's_.*<Iwanthis>(.*)</Iwanthis>.*_1_p' yourfile
The -n
suppresses default output, so you just get output on the matching line. I did chose the underscore as delimiter for the s
command to avoid escaping the slash. The s
command will replace the whole line by the part between the opening and closing tag (the 1
refers to the (.*)
part)
Of course you can still create weird cases to make this fail.
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you can trust that there will be one opening tag and one closing tag on that line, you can use this as field separator for awk
:
awk -F "</*Iwanthis>" '(NF > 1)print $2' yourfile
This will fail on a lot of artificial cases. To be more strict, you could use sed
:
sed -n 's_.*<Iwanthis>(.*)</Iwanthis>.*_1_p' yourfile
The -n
suppresses default output, so you just get output on the matching line. I did chose the underscore as delimiter for the s
command to avoid escaping the slash. The s
command will replace the whole line by the part between the opening and closing tag (the 1
refers to the (.*)
part)
Of course you can still create weird cases to make this fail.
If you can trust that there will be one opening tag and one closing tag on that line, you can use this as field separator for awk
:
awk -F "</*Iwanthis>" '(NF > 1)print $2' yourfile
This will fail on a lot of artificial cases. To be more strict, you could use sed
:
sed -n 's_.*<Iwanthis>(.*)</Iwanthis>.*_1_p' yourfile
The -n
suppresses default output, so you just get output on the matching line. I did chose the underscore as delimiter for the s
command to avoid escaping the slash. The s
command will replace the whole line by the part between the opening and closing tag (the 1
refers to the (.*)
part)
Of course you can still create weird cases to make this fail.
edited May 2 at 12:56
answered May 2 at 12:51
Philippos
5,89211544
5,89211544
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
add a comment |Â
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I feel bad now - I tried for hours last night and did it 100% in just a few minutes...
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
I salute you SIR!!! Perfect - Does exactly what I wanted!!
â Mike Smith
May 2 at 13:25
add a comment |Â
up vote
2
down vote
Sure looks like an XML file. Add a root note and use an XML parser:
$ echo '<r>'; cat file; echo '</r>'; | xmlstarlet sel -t -v //Iwanthis
6.1.0
add a comment |Â
up vote
2
down vote
Sure looks like an XML file. Add a root note and use an XML parser:
$ echo '<r>'; cat file; echo '</r>'; | xmlstarlet sel -t -v //Iwanthis
6.1.0
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Sure looks like an XML file. Add a root note and use an XML parser:
$ echo '<r>'; cat file; echo '</r>'; | xmlstarlet sel -t -v //Iwanthis
6.1.0
Sure looks like an XML file. Add a root note and use an XML parser:
$ echo '<r>'; cat file; echo '</r>'; | xmlstarlet sel -t -v //Iwanthis
6.1.0
answered May 2 at 13:41
glenn jackman
45.8k265100
45.8k265100
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441310%2fi-need-to-find-a-value-in-file-between-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Is this an XML file?
â Stephen Kitt
May 2 at 12:25
Sorry - this a standard file - Not XML
â Mike Smith
May 2 at 12:26
I should mention - I need a solution in Bash - sorry for not mentioning it!
â Mike Smith
May 2 at 12:27
Bash is just a shell, not much of a text editor. YouâÂÂre looking for a command-line solution? Grep, awk, sed, that sort of thing?
â Jeff Schaller
May 2 at 12:28
Yup - any of that would suffice once I can assign the XXXX into a variable to manipulate :P It's the special characters that are confusing me.
â Mike Smith
May 2 at 12:48