LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-22-2010, 02:25 AM   #1
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
In need of some SED assistance


Hi all,

I'm not that familiar with sed and awk in order to be able to solve this problem on my own, so I'm calling on you for a bit of assistance.

I'm writing a Nagios plugin to check our Oracle tablespaces and the output is given in one line like this:
Code:
TABLESPACE_NAME USED_PERCENT ------------------------------ ------------ AXIONAL_INDEX 62.2645833 DATA 71.926875 SYSAUX .885200923 SYSTEM 1.04007771 TEMP 0 UNDOTBS1 .005340579 USERS 0 7 rows selected.
I've been playing around with sed like below to delete the obsolete info and change every second space into a newline:
Code:
cat myfile | sed -e 's/TABLESPACE_NAME USED_PERCENT ------------------------------ ------------ //' -e 's/ /\n/2' -e 's/ /\n/3' -e 's/ /\n/4' -e 's/ /\n/5' -e 's/ /\n/6' -e 's/ /\n/7' -e 's/ /\n/8'
which gives me an expected result:
Code:
AXIONAL_INDEX 62.2645833
DATA 71.926875
SYSAUX .885200923
SYSTEM 1.04007771
TEMP 0
UNDOTBS1 .008392338
USERS 0
7 rows selected.
My 'problem' is that I don't know up front how many tablespaces there are so I'd have to check all databases and 'hardcode' the tablespaces in my script. Is there any way to 'automate' this knowing that 'rows selected' preceded by a number is always the last line and using a sort of counter to auto-adjust the number to put in the -e 's/ /\n/2' part?

Also if you know of a 'cleaner' solution (I'm sure some of you can come up with a nifty one-liner) I'd appreciate the help.

Kind regards,

Eric
 
Old 07-22-2010, 03:01 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Is this what you are looking for:

cut -c74- infile | sed 's/\([A-Z][_A-Z0-9]* [0-9\.][0-9\.]*\) /\1\n/g'

I assumed that the first 74 characters are the header and always the same length (the cut command).

The sed command uses back-referencing (that what is found in the search part, all between \( and \), is used as the replace part, the \1 part).

The sed command looks for tablenames that contain these possible characters: Starts with A-Z, followed by A-Z, 0-9 or a _ (zero or more times)
This must be followed by a space and 0-9 or a dot followed by 0-9 and/or a dot (zero or more times). followed by a space.

What is found is printed and folowed by a return (the \n).

Example run:
Code:
$ cat infile
TABLESPACE_NAME USED_PERCENT ------------------------------ ------------ AXIONAL_INDEX 62.2645833 DATA 71.926875 SYSAUX .885200923 SYSTEM 1.04007771 TEMP 0 UNDOTBS1 .005340579 USERS 0 7 rows selected

TABLESPACE_NAME USED_PERCENT ------------------------------ ------------ AXIONAL_INDEX 62.2645833 DATA 71.926875 SYSAUX .885200923 SYSTEM 1.04007771 TEMP 0 UNDOTBS1 .005340579 USERS 0 DUMMY_A 1 DUMMY_B 2.2 9 rows selected

$ cut -c74- infile | sed 's/\([A-Z][_A-Z0-9]* [0-9\.][0-9\.]*\) /\1\n/g'
AXIONAL_INDEX 62.2645833
DATA 71.926875
SYSAUX .885200923
SYSTEM 1.04007771
TEMP 0
UNDOTBS1 .005340579
USERS 0
7 rows selected

AXIONAL_INDEX 62.2645833
DATA 71.926875
SYSAUX .885200923
SYSTEM 1.04007771
TEMP 0
UNDOTBS1 .005340579
USERS 0
DUMMY_A 1
DUMMY_B 2.2
9 rows selected
Hope this helps.

Last edited by druuna; 07-22-2010 at 03:06 AM.
 
1 members found this post helpful.
Old 07-22-2010, 03:12 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805

Original Poster
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi druuna,

Thank you so much, that's exactly what I need. And I realy appreciate the detailed explanation. Yet learned a lot more for future use. Thanks again.

Kind regards,

Eric
 
Old 07-22-2010, 03:18 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 07-22-2010, 03:50 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just as an alternative to have a gander at:
Code:
awk '{for(x=0; x < (NF - 5);x+=2)print $(x + 5),$(x + 6)}' in_file
It doesn't show the word 'selected' on the last line, but not sure if you needed that anyway
 
1 members found this post helpful.
Old 07-22-2010, 04:35 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805

Original Poster
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi grail,

Thanks for the awk alternative. Could you elaborate some more on that construction so that it makes sence to me too?

Kind regards,

Eric
 
Old 07-22-2010, 06:44 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sure:

1. for - most similar to a C for loop construct

2. NF - this is awk for the number of fields. In awk the standard delimeter for a field is whitespace, so in your example the simple space separates each field

3. x+=2 - step through loop in units of 2 as we wish to print pairs of information

4. $(x + 5) - the $ is similar to the one in bash that it evaluates and in this case it will evaluate the fields we wish to look at - first being 5 and 6

5. , - seems funny to comment on a comma but in awk this will enable print to output the OFS (output field separator), the standard being a space

So overall it says - loop through each line of the file and starting at positions 5 and 6 print the two fields separated by a space
 
1 members found this post helpful.
Old 07-22-2010, 01:46 PM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805

Original Poster
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi grail,

Thanks a lot for the explanation!

Reading up on the Grymoire manuals in regards to sed and awk at this moment. Thanks again guys for the help and sharing your knowledge.

Kind regards,

Eric
 
  


Reply



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
need your assistance scbops Red Hat 2 10-21-2009 09:11 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Need assistance please wennie Linux - Software 5 03-16-2005 07:24 AM
Need assistance spotslayer Linux - Software 1 11-18-2004 06:49 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration