- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: extracting data from string
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
тАО08-11-2005 01:09 AM
тАО08-11-2005 01:09 AM
I need to extract some tagged line from a file formated like this:
flag_start
line1
line2
...
...
Serial_number1
linea
lineb
...
...
flag_stop
flag_start
line1
line2
...
...
Serial_number2
linea
lineb
...
...
flag_stop
In the file, there are several flagged patern with Serial_number1, and I would like to extract the whole pattern and write it in a new file ...
Can someone help me with this
Thanks in advance for your help
Best Regards
J├йr├┤me
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:15 AM
тАО08-11-2005 01:15 AM
Re: extracting data from string
My understanding of your request is that you want everything from "Serial_number1" to flag_stop in a different file. Assuming that's the case, it's easy:
sed -n -e '/^Serial_number1/,/^flag_stop/p' ${in_file} > ${new_out_file}
HTH;
Doug
------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:15 AM
тАО08-11-2005 01:15 AM
Re: extracting data from string
Not sure about exact what you want to do but, awk is a very useful tool for processing text files. Search the forum for "awk" and you will find a lot of examples how to use awk.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:22 AM
тАО08-11-2005 01:22 AM
Re: extracting data from string
Thanks for your answers, in fact, In each file, the pattern is flagged beetween flag_start and flag_stop.
In each file, I recognize the pattern by Serial_number, and there are several per file.
What I would like is to extract all the patterns (beetween flag_start and flag_stop) that have the same Serial_number.
Thanks in advance for your help
J├Г┬йr├Г┬┤me C
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:48 AM
тАО08-11-2005 01:48 AM
Re: extracting data from string
If the "serial number" pattern within this particular flagged block matched X, then extract the whole block?
If so then try something like:
awk -F"\n" 'BEGIN { matched=0 ; block="" } \
$1 !~ "^flag_s*" { block = block$1"\n" } \
$1 == "Serial_number1" { matched = 1 } \
$1 == "flag_stop" { if ( matched == 1 ) { print block } ; block="" ; matched=0 }' infile
Basically it sets a flagged called "matched" to zero. Then reads each line in turn, and appends it to a variable. If it gets to a matching serial number, it sets the "matched" flag to 1. Then when it comes to "flag_stop" it knows it's finished a block, and if at this time "matched" is 1, it prints the block, otherwise it doesn't.
This suit?
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:49 AM
тАО08-11-2005 01:49 AM
Re: extracting data from string
Can you give us like 10 to 20 lines of a real file.
The problem I'm having is figuring out if Serial_numberN appears multiple times or what.
The following script will print ALL of the data between flag_start and flag_stop. I suspect that's not what you want, but I thought I would post that much while I was asking my questions.
Create a file called useme.awk with the following in it:
BEGIN{daflag=0;}
/flag_start/ {daflag=1;next}
/flag_stop/ {daflag=0;next}
daflag==1 {print $0}
Run:
awk -f useme.awk < inputfile > outputfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:50 AM
тАО08-11-2005 01:50 AM
Re: extracting data from string
{ block = block$1"\n" } \
and thus removing the ^flag_s* ignorance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 01:53 AM
тАО08-11-2005 01:53 AM
Re: extracting data from string
I got some idea to extract the flagged patern to share with you.
Using combination of commands.
grep -n
where -n option give you line no. of startflag.
Extract the pattern using the head or tail command option to -/+
Hope this hint may give some more idea.
Regards,
Rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 02:02 AM
тАО08-11-2005 02:02 AM
Re: extracting data from string
How do you recognize a serial number?
Does it always start with "Serial" in the begin of a line?
You wanted the lines BETWEEN start and stop right, not inclusive right?
If so ten I think the perl below is what you need.
You'll need to adapt the 'Serial' match to the actual pattern, and you probably want a different algoritme for the file name generation ($file = ...)
hth,
Hein.
while (<>) {
if (/^flag_start/) {
$save = 1;
next;
}
if (/^flag_stop/) {
$save = 0;
if ($file) {
open (FILE,">> $file") or die "could not append to $file";
print FILE $line while ($line = shift @lines);
close FILE;
}
}
next unless $save;
push @lines, $_;
if (/^Serial/) {
chop;
$file = $_ . ".tmp" ;
}
}
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2005 02:29 AM
тАО08-11-2005 02:29 AM
Re: extracting data from string
I would like to include the lines.
I am sorry, I am very bad in scripting so ..
Where do I put the input filename .. in the script?
Thanks in advance
Best Regards
J├Г┬йr├Г┬┤me