Visit Our UNIX and Linux User Community


Extra/parse lines from a file between unque lines through the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extra/parse lines from a file between unque lines through the file
# 1  
Old 01-27-2009
Extra/parse lines from a file between unque lines through the file

I need help to parse a file where there are many records, all of which are consistently separated by lines containing ^============= and "^ End of Report".

Example:
=============
1
2
3
4
End of record
=============
1
3
4
End of record
Etc....

I only need specific lines within start and finish.
Output
1,2,3
1,3
Etc...

As a bonus it would also help if I could extract allines start content and finish to a file as in
fileA
==============
1
2
3
4
End of record

fileB
==============
1
3
4
End of record

Etc...

I have had the need for this several times and never had much luck processing it myself.
Any suggestions will be greatly appreciated.
I have looked this up and just didn't find one with an example that fit my circumstances.
I prefer to use ksh
Any suggestions will be greatly appreciated.
#
#
# 2  
Old 01-27-2009
Quote:
Originally Posted by jouuu
I only need specific lines within start and finish.
what exactly does the make the 'specific lines' so 'specific'?
# 3  
Old 01-27-2009
Tools Think I have been staring at computers too long today to come up with the following!

Code:
> cat file155
=============
1
2
3
4
End of record
=============
1
3
4
End of record
=============
1
3
4
End of record

> sed "s/record/record~/" <file155 | tr "\n" "=" | tr "~" "\n" | sed "s/..=End of record//g" | tr -s "=" | tr "=" "," | cut -d"," -f2-
1,2,3
1,3
1,3

# 4  
Old 01-27-2009
WOW how does it work?
How does it only affect 1,2 and 3 and avoid 4.
I would like to use the command as a template for similar edits.
I find it really difficult from your syntax to reproduce the same behavior on a file with different start and finish and different lines to extract from.
# 5  
Old 01-27-2009
Hammer & Screwdriver Let me try and break it down

Code:
> sed "s/record/record~/" <file155 | tr "\n" "=" | tr "~" "\n" | sed "s/..=End of record//g" | tr -s "=" | tr "=" "," | cut -d"," -f2-

> sed "s/record/record~/" <file155 +++ add a ~ to end of word as marker
| tr "\n" "=" +++ translate new-line to = character; puts all on 1 line
| tr "~" "\n" +++ translate ~ to new-line; now 1 rec per line
| sed "s/..=End of record//g" +++ use sed to delete 2 chars & =End of record
| tr -s "=" +++ suppress multiple consecuticve = characters; as in breaks
| tr "=" "," +++ translate remaining = to ,
| cut -d"," -f2- +++ cut the data with , delim for fields 2 to end

I would suggest trying the sed command, then pipe that to include the first tr command. Keep adding commands one-at-a-time to see the progression of how the data is manipulated.

Last edited by joeyg; 01-27-2009 at 11:48 PM..
# 6  
Old 01-28-2009
I appreciate your help. Can that be applied a file with the following content.
datactr@acuransx:nbemmcmd -listmedia -mediaid 103549
NBEMMCMD, Version:6.5
====================================================================
Media GUID: c9f01152-eb8a-11dd-8000-9a518a76e394
Media ID: 103549
Partner: -
Media Type: HCART2
Volume Group: ---
Application: Netbackup
Media Flags: 1
Description: Rules for LTO Tapes
Barcode: 103549
Partner Barcode: --------
Last Write Host: legend
Created: 01/26/2009 14:22
Time Assigned: 01/27/2009 16:15
First Mount: 01/27/2009 16:07
Last Mount: 01/28/2009 07:02
Volume Expiration: -
Data Expiration: 02/27/2009 07:00
Last Written: 01/28/2009 07:00
Last Read: -
Robot Type: NONE
Robot Control Host: -
Robot Number: -
Slot: -
Side/Face: -
Cleanings Remaining: -
Number of Mounts: 11
Maximum Mounts Allowed: 0
Media Status: ACTIVE
Kilobytes: 192549351
Images: 11
Valid Images: 11
Retention Period: 2
Number of Restores: 0
Optical Header Size Bytes: 1024
Optical Sector Size Bytes: 0
Optical Partition Size Bytes: 0
Last Header Offset: 2136502
Adamm Guid: 00000000-0000-0000-0000-000000000000
Rsm Guid: 00000000-0000-0000-0000-000000000000
Origin Host: NONE
Master Host: acuransx
Server Group: UNRESTRICTED_SHARING_GROUP
Upgrade Conflicts Flag:
Pool Number: 8
Volume Pool: LTO-Tapes
Previous Pool Name: -
Vault Flags: -
Vault Container: -
Vault Name: -
Vault Slot: -
Session ID: -
Date Vaulted: -
Return Date: -

Where "^Media GUID:" is the start and "^Return Date" is finish and I want to extract any number or lines to be displayed across one line.
example output
Media ID,Media Status,Media Type,Data Expiration,Return Date
103549,ACTIVE,HCART2,02/27/2009 07:00,-

Netbackup has several commands that output data in this format. I was hoping for syntax that i could change start and finish lines and continue to extract lines between them.

Again thanks for you help with this JoeG.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

Removing extra lines from file

I have a file where data looks like this: === <?xml version="1.0" encoding="utf-8"?> <xml xmlns:s='uuid:XYZ' xmlns:dt='uuid:ABC' xmlns:rs='urn:schemas-microsoft-com:rowset' xmlns:z='#RowsetSchema'> <s:Schema id='RowsetSchema'> <s:ElementType name='row'... (7 Replies)
Discussion started by: vx04
7 Replies

3. Shell Programming and Scripting

Consolidate several lines of a CSV file with firewall rules, in order to parse them easier?

Consolidate several lines of a CSV file with firewall rules Hi guys. I have a CSV file, which I created using an HTML export from a Check Point firewall policy. Each rule is represented as several lines, in some cases. That occurs when a rule has several address sources, destinations or... (4 Replies)
Discussion started by: starriol
4 Replies

4. Shell Programming and Scripting

Check/Parse log file's lines using time difference/timestamp

I was looking at this script which outputs the two lines which differs less than one sec. #!/usr/bin/perl -w use strict; use warnings; use Time::Local; use constant SEC_MILIC => 1000; my $file='infile'; ## Open for reading argument file. open my $fh, "<", $file or die "Cannot... (1 Reply)
Discussion started by: cele_82
1 Replies

5. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

6. Shell Programming and Scripting

Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4... (6 Replies)
Discussion started by: darbs121
6 Replies

7. Shell Programming and Scripting

Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths. * /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB * /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB * /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB *... (4 Replies)
Discussion started by: senthilkc
4 Replies

8. Shell Programming and Scripting

Parse a file to display lines containing a word

Hi! I'm trying to create a shell script to parse a file which might have multiple lines matching a pattern (i.e. containing some word). I need to return all lines matching the pattern, but stripping the contents of that line until the pattern is matched For example, if my input file was ... (4 Replies)
Discussion started by: orno
4 Replies

9. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

10. Shell Programming and Scripting

remove extra lines in the file

Hi, I have some files, with some extra lines in weird characters on the top and bottom of the. I want to get rid of those line. Is there a way I can do that? example of the input file. I want to get rid of those lines in bold (B ... (8 Replies)
Discussion started by: CamTu
8 Replies

Featured Tech Videos