LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-30-2010, 08:15 AM   #1
shawnamiller
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 1
Extract multiple lines of data from a text file.


I need to take out like I have listed below and extract all the ALU Numbers out so I use those in another command. The length of the file changes so the line number is not exact. If I can at least extract out the lines with the ALU numbers then it is easy enough to remove the column I don't need. Any suggestions on what I can use to grab just those lines.

Here is an example of the type of output I am working with.

Storage Group Name: miux11_sg
Storage Group UID: C2:F36:E8:F0:4AE:11:8A:FC:00:60:16:25:9F:FA
HBA/SP Pairs:

HBA UID SP Name SPPort
------- ------- ------
20:00:00:00:C9:43:46:3B:10:00:00:00:C9:43:46:3B SP B 6
20:00:00:00:C9:43:46:5F:10:00:00:00:C9:43:46:5F SP A 6
20:00:00:00:C9:43:46:5F:10:00:00:00:C9:43:46:5F SP B 7
20:00:00:00:C9:43:46:3B:10:00:00:00:C9:43:46:3B SP A 7

HLU/ALU Pairs:

HLU Number ALU Number
---------- ----------
0 24
2 303
3 311
4 327
5 304
6 312
7 25
8 20
10 297
11 305
12 313
13 102
14 287
1 23
9 22
17 328
18 222
15 191
Shareable: YES


Thanks for the help,
Shawn
 
Old 04-30-2010, 08:19 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,610

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
grep?
 
Old 04-30-2010, 08:23 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,432

Rep: Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878
You gave us what you started with but not what you would like to end up with??
I would say awk, but hard to tell without know what you want.

What have you tried???
 
Old 04-30-2010, 08:26 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
First, we need to define what is the unique attribute of the lines to be extracted. Looking at your sample, it could be:
1. Everything between "HLU Number ALU Number" and "Shareable"

2. Every line containing exactly 2 numbers and nothing else.

The second one is maybe the easiest (NOT TESTED):

Code:
sed -n -r '/^[0-9]+ [0-9]+$/p' filename
To extract the second number at the same time:

Code:
sed -n -r '/^[0-9]+ [0-9]+$/s/^[0-9]+ //p' filename
Good tutorials here:
http://www.grymoire.com/Unix/ look at both SED and AWK
 
Old 04-30-2010, 08:35 AM   #5
shawnamiller
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 1
Data that needs to be extracted.

In the output that I start with after the lines:
HLU Number ALU Number
---------- ----------

There are sets of numbers and I need to extract just those numbers. Ideally I only want the numbers under the ALU Number column. I only want to extract numbers and just up to the line that starts with "Shareable:"

If I extract the numbers under HLU Number as well I know how to run that output through awk to just get ALU Numbers.

So in the above example I want the output of:

24
303
311
327
304
312
25
20
297
305
313
102
287
23
22
328
222
191

Once I get that data I plan on putting them in a loop.
 
Old 04-30-2010, 08:39 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Did you try my solution?
 
Old 04-30-2010, 08:50 AM   #7
shawnamiller
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 1
Pixellany,
I tried the commands and did not get any output. The idea is what I am looking for because it is only lines with two numbers in it that I need to extract.
 
Old 04-30-2010, 09:07 AM   #8
shawnamiller
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 1
pixellany,

Thanks for pointing me in the write direction. The syntax I ended up using is this:
sed -n -r '/^[ ]+[0-9]+[ ]+[0-9]+$/ p'
 
Old 04-30-2010, 11:46 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I have just given you your first "thank you".

Why?
You posted a rational question
You posted follow-up
You took my knee-jerk suggestion, understood it, and then fixed it.
You posted your results.

You have no idea how rare this is----looking forward to seeing you here more.
 
  


Reply

Tags
scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to read lines in a text file and filter user data srimal Linux - Newbie 5 10-21-2009 07:41 AM
Extract uncommented lines from a file aarav2306 Linux - Newbie 6 07-11-2009 11:27 AM
[SOLVED] edit multiple lines of a text file into 1 line: schneidz Programming 2 04-09-2009 11:22 AM
Extract lines NOT on a block of text from a file Renan_S2 Programming 3 10-05-2008 04:14 PM
Batch Text Extract Multiple Files lixbie Programming 10 07-02-2008 09:56 AM


All times are GMT -5. The time now is 04:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration