I have a requirement wherein i need to extract a 8 digit number, for eg: 87464898 from a .xml file. i will have only one such number in the file. how to achieve it using sed or awk?
Asked
Active
Viewed 140 times
-4
-
4Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. – Cyrus May 12 '19 at 12:11
-
And please, add a sample .xml file for testing. – James Brown May 12 '19 at 12:12
-
Why do you need to extract the number from a file when you already know what it is? Why can't you just do `echo '87464898'` to output that number? See [ask] and in [edit] your question to show a [mcve] with concise, testable sample input and expected output plus what you've tried so far. – Ed Morton May 12 '19 at 13:27
-
Do you know how to do something simpler (in sed or awk)? Like matching such a string? – Beta May 12 '19 at 16:51
-
`grep -E -o '[0-9]{8}' file.xml`? – Shawn May 12 '19 at 20:12
-
@Ed Morton: that number is generated by a tool and it would be a random number....which is getting generated in that xml file – user11458850 May 13 '19 at 00:43
-
OK, see [ask] (and the first 3 comments under you question) for how to write a question if you'd like to get help. – Ed Morton May 13 '19 at 01:39
1 Answers
0
sed -n 's/.*<request_id>\([0-9]*\)<.*/\1/p' test.xml
explanation
sed -n # no output by default
's/ # substitute
.*<request_id> # search pattern
\([0-9]*\) # extract all digits into arg1 (\1)
<.* # ignore all after <
/\1/p' # print only \1

UtLox
- 3,744
- 2
- 10
- 13
-
That's just a lengthy way to say `sed -n '/.*>\([0-9]\{8\}\)<.*/\1/p'` but without a [mcve] from the OP we're just guessing what what her input and expected output look like. – Ed Morton May 13 '19 at 13:41
-
Hi @UtLox ...your answer also gave me the correct solution...thanks...thanks for explaining also... – user11458850 May 14 '19 at 00:12
-
@UtLox... I used your command to extract the field of request_status from the xml...but its not giving any result...the command i am using is correct? Like for eg: the xml is ....
thyusl 78942479 Processing sed '/>request_status!d;s/.*>\(request_status\)<.*/\1/' test.xml – user11458850 May 14 '19 at 01:08 -