- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Microsoft
- >
- Re: Extract pattern recursively to new file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 05:35 AM
тАО04-08-2011 05:35 AM
I want to extract each pattern between
I want to know how this can be done using awk or sed.Kindly input your thoughts
Regards,
Gyan
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 06:45 AM
тАО04-08-2011 06:45 AM
Solutionstartline=`grep -n "transaction id" myfile.xml | head -1`
startlineno=`echo ${startline}|cut -d: -f1`
transname=`echo ${startline}|cut -d"=" -f2|sed -e "1,1s/\"//g"
endlineno=`grep -n "/transaction" myfile.xml | head -1 |cut -d: -f1`
(( sl=${startlineno}+1 ))
(( el=${endlineno}-1 ))
cat myfile.xml | sed -n ${sl},${el}p > ${transname}
cat myfile.xml | sed -e "${startlineno},${endlineno}d" > tmpxmlfile
cat tmpxmlfile > myfile.xml
at this point, you have extracted the first xml block out of the file and placed in the transaction named file and deleted the extracted portion from the original file.
Now, for the rest of your homework studies, you need to build a while/do construct to go through the file until there is nothing left in it. It is only fair if you do part of the homework yourself :) Isn't it ?
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 06:50 AM
тАО04-08-2011 06:50 AM
Re: Extract pattern recursively to new file
There is probably some XML tool to do this for you.
But if it is for a one-off, controlled, situation then yo may try for example a quick PERL solution :
# perl -ne 'close F if m~^$1.tmp") if /^
explanation
# perl -ne ' ## start perl looping over input
close F if m~^print F if F; ## Print any line while file handle defined.
open (F,">$1.tmp") ## Open a file F for output with name from variable $1
if /^
Full example below.
hth,
Hein
# ls *.tmp
x.tmp
# perl -ne 'close F if m~^$1.tmp") if /^
-rw-r--r-- 1 Administrator None 70 Apr 8 10:34 dos.tmp
-rw-r--r-- 1 Administrator None 80 Apr 8 10:34 tres.tmp
-rw-r--r-- 1 Administrator None 80 Apr 8 10:34 uno.tmp
-rw-r--r-- 1 Administrator None 355 Apr 8 10:34 x.tmp
# cat dos.tmp
#
# cat x.tmp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 07:10 AM
тАО04-08-2011 07:10 AM
Re: Extract pattern recursively to new file
if /^
Broken down
^ ## at begin of line
' x.tmp ## close the program text, and specify input.
You surely noticed how I changed you input file example to be able to distiguish lines by id.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2011 04:52 AM
тАО04-09-2011 04:52 AM
Re: Extract pattern recursively to new file
startline=`grep -n "transactiion id" myfile.xml | head -1`
startlineno=`echo ${startline}|cut -d: -f1`
transname=`echo ${startline}|cut -d"=" -f2|sed -e "1,1s/\"//g"|sed -e "s/>//g"`
endlineno=`grep -n "/transactiion" myfile.xml | head -1 |cut -d: -f1`
s2=`expr "${startlineno}"`
s1=`echo $s2 | awk '{print $1 + 1}'`
e1=`echo $endlineno | awk '{print $1 - 1}'`
cat myfile.xml | sed -n ${sl},${el}p > $transname
but it is throwing exception sed: -e expression #1, char 1: unknown command: `,'
i think its because of the last line cat myfile.xml | sed -n ${sl},${el}p > $transname
I have still not tested it in perl as i am having problems with cygwin setup with perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2011 04:59 AM
тАО04-09-2011 04:59 AM
Re: Extract pattern recursively to new file
>> not tested it in perl as i am having problems with cygwin setup with perl
fwiw... I tested my answer under cygwin.
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2011 05:17 AM
тАО04-09-2011 05:17 AM
Re: Extract pattern recursively to new file
"I want to know how this can be done using awk or sed."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2011 06:33 AM
тАО04-09-2011 06:33 AM
Re: Extract pattern recursively to new file
contemplation:
Hmmm, you were entering your question in an HPUX forum. That suggests that PERL is available as just an other tool.
You really want to get perl in your toolbox... it is handy.
condemnation:
Anyway, the same perl method maps easily onto AWK:
awk '/^<\/tra/ {F=""} F {print $0>F} /^
explanation:
/^<\/tra/ {F=""} ## mark file as close at end of transaction
F {print $0>F} ## IF there is a filename then print on that file.
/^
demonstration:
# cp x.txt x.tmp
# dir *.tmp
x.tmp
# awk '/^<\/tra/ {F=""} F {print $0>F} /^
dos.tmp tres.tmp uno.tmp x.tmp
# cat dos.tmp
# cat x.tmp
#
salutation:
Regards,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2011 05:26 PM
тАО04-09-2011 05:26 PM
Re: Extract pattern recursively to new file
I am sure there are countless number of cygwin related forims, albeit not as active as this one and if you answer your question in one of those, sooner or later, you will get a response.
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-09-2011 07:19 PM
тАО04-09-2011 07:19 PM