I am new to unix world , i would like to search string and return back to another sh script.
bascially i want to read file Sample.txt find the string between <ownername> Sample.txt < ownername> .
Gerneric way to find the string between <>x<>.Could you please help me to get the code for the same.
Narrative: grep for the tag and then some characters not < and then the string and then more not < and then the closing tag.
grep can find the lines with string in tags, but sed can dig out the string. This assumes one per line:
sed '
s/.*<ownername>\([^<]*\)<\/ownername>.*/\1/
t
d
'
Narrative: select in every line with a regex that captures the whole line but a substring captures the string between tags, and replace the whole line with that substring. If the replacement occurs, branch to end of script (print and go to next line) else delete line.
I like sed because the bits and pieces are reusable in sed, vi, ex, ed, grep, egrep, ksh command line editing, C, JAVA, PERL, awk.
Please clarify what the input format is. In your first posting, there is a space before the ending tag label, in your last one, there is no more space. Everyone is expecting a / there but your files might not be xml. In that case, that might be:
True, I stand corrected -- must have had my head tilted.
You just do not exit after the first hit is printed.
I have not used many options like post-s p only good with -n because they are utterly redundant to other, more modular, less limited, more general pieces, and space in my head is more precious than disk space. Commands are designed by committee, it seems.
Your right, it is not that efficient, but the idea is that there may be more then one pair of tags (each on the same line otherwise it will not work), to quit I'll gladly use your solution
sed 's|.*<\(owner_name\)>\(.*\)</*\1>.*|\2|;te;d;:e;q' infile
---------- Post updated at 22:02 ---------- Previous update was at 21:36 ----------
This should work with content spread over more than one line