LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 01-15-2009, 08:36 AM   #1
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Fields script...


Hi

I am given a file that looks like this:

/dev/rhdisk5,00,00,CL2C,08:ed,OPEN-9-CVS-CM,100,10346,5003,0013,---,SMPL,SMPL,SMPL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004286a02,00,7e,0x10800,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,None,LVM,---,---,---,---,0x50060e8004286a02
/dev/rhdisk6,00,00,CL2C,00:37,OPEN-9,7042,10346,5003,0013,---,SMPL,SMPL,PVOL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004286a02,00,7e,0x10800,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,vgJRI2d,LVM,JRI2d_ora:/global/bc ,---,---,---,0x50060e8004286a02
/dev/rhdisk7,00,00,CL4C,01:37,OPEN-9,7042,10346,5003,0013,---,SMPL,SMPL,SMPL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004286a22,00,7e,0x10900,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,hpvg,LVM,tst1lv:/tst1 lv00:N/A ,---,---,---,0x50060e8004286a22
/dev/rhdisk8,00,00,CL2D,07:06,OPEN-9,7042,10346,5003,0013,---,SMPL,SMPL,SVOL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004286a03,00,7e,0x10c00,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,None,LVM,---,---,---,---,0x50060e8004286a03
/dev/rhdisk9,00,00,CL4D,07:3f,OPEN-9,7042,10346,5003,0013,---,SMPL,SMPL,SMPL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004286a23,00,7e,0x10d00,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,hbvg,LVM,,---,---,---,0x50060e8004286a23
/dev/rhdisk10,00,00,CL2B,00:1d,OPEN-9,7042,10320,5003,0013,---,SMPL,SMPL,PVOL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004285001,00,7e,0x20400,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,vgJRI2d,LVM,JRI2d_ora:/global/bc ,---,---,---,0x50060e8004285001
/dev/rhdisk11,00,00,CL4B,00:57,OPEN-9,7042,10320,5003,0013,---,SMPL,SMPL,PVOL,SMPL,3,RAID5,10-16,L141F,L151F,L161F,L171F,XP,0x50060e8004285021,00,7e,0x20500,U787A10000000C9466AAB:U787A10000000C9 3E95C3:,hpvg,LVM,tst1lv:/tst1 lv00:N/A ,---,---,---,0x50060e8004285021


From this file I need to find the following: Port (field 4), Device (field 1), Type (field 6), and Serial # (field 8).

The following is what I tried using arrays and each time I give it the array index to print a field associated with it (i.e. array index 0 for field 1, index 3 for field 4, etc) it prints the whole file for each of them.

my code is:

#!/bin/ksh
echo "Device, Serial#, Type, Port, CU:Ldev"
set -A Xpinfo
Xpinfo=`cat xpinfo_maker.out`
echo $Xpinfo[0], $Xpinfo[7], $Xpinfo[5], $Xpinfi[3], $Xpinfo[4]



Please help. Thanks in advance...
 
Old 01-15-2009, 09:24 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
When you post a code sample, it's best to tell us what it does (or does not) do.

Why not AWK? It is ideal for extracting specific fields.
 
Old 01-15-2009, 09:31 AM   #3
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
You have to parse that somehow. Since you are obviously just learning, I recommend you switch to perl since it's almost certainly better for this kind of task, is available everywhere, and will probably be more useful knowledge later.
Code:
#!/usr/bin/perl -w 
use strict; 

while (<DATA>) {
	$_=~/^\/dev\/(\w+),\d+,\d+,(\w+),\w+:\w+,([\w+-]+),\d+,(\d+),/;
	print "Device: $1 Port: $2 Type: $3 Serial Number: $4\n";
}

__DATA__
copy the data into here
I think the field numbers you give are fishing, but assuming you wanted the 1st, 4th, 6th, and 8th things seperated by commas, the output is:
Code:
Device: rhdisk5 Port: CL2C Type: OPEN-9-CVS-CM Serial Number: 10346
Device: rhdisk6 Port: CL2C Type: OPEN-9 Serial Number: 10346
Device: rhdisk7 Port: CL4C Type: OPEN-9 Serial Number: 10346
Device: rhdisk8 Port: CL2D Type: OPEN-9 Serial Number: 10346
Device: rhdisk9 Port: CL4D Type: OPEN-9 Serial Number: 10346
Device: rhdisk10 Port: CL2B Type: OPEN-9 Serial Number: 10320
Device: rhdisk11 Port: CL4B Type: OPEN-9 Serial Number: 10320
$1, $2, etc. correspond to substrings (in parantheses) in the =~/match/

I put the data at the end of the script for my own convenience, but you can read it from a file:
Code:
open (FH, "test.txt");

while (<FH>) {
	$_=~/^\/dev\/(\w+),\d+,\d+,(\w+),\w+:\w+,([\w+-]+),\d+,(\d+),/;
	print "Device: $1 Port: $2 Type: $3 Serial Number: $4\n";
}

close(FH);

Last edited by mk27; 01-15-2009 at 09:37 AM.
 
Old 01-15-2009, 09:37 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
I think it would take far less time learning to use awk to solve the problem then learning an entire language. Perl may be better at solving it, but learning to code in perl would take a very long time.
 
Old 01-15-2009, 10:13 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
I agree to the aforementioned usage of awk to parse a file line by line.

Anyway, regarding your ksh script the problems are:
1) when assigning the content of the file to the array (using cat) it will split fields using blank spaces as delimiter (not comma)
2) the syntax to display a single array element is wrong. It should be echo ${Xpinfo[0]}

First consider the following script:
Code:
#!/bin/ksh
IFS=,
Xpinfo=( $(cat xpinfo_maker.out) )
echo ${#Xpinfo[@]}
echo ${Xpinfo[0]}, ${Xpinfo[7]}, ${Xpinfo[5]}, ${Xpinfo[3]}, ${Xpinfo[4]}
This uses comma as Input Field Separator so it assigns the content of the file to the array considering each comma separated field as an element. Look at the total count of elements stored in the array! Then it will print just the 0th, 7th, 5th, 3rd and 4th fields relative to the total content of the file. Conclusion: you have to parse the file line by line.

The following (maybe) is what you're looking for:
Code:
#!/bin/ksh
echo "Device, Serial#, Type, Port, CU:Ldev"

while read line
do
  echo $line | IFS=, read -A Xpinfo
  echo ${Xpinfo[0]}, ${Xpinfo[7]}, ${Xpinfo[5]}, ${Xpinfo[3]}, ${Xpinfo[4]}
done < xpinfo_maker.out
Also it is a good practice to save the original IFS at the beginning of the script - e.g. oldIFS=${IFS} - and then restore it when the modified IFS is not needed anymore - IFS=${oldIFS}.
 
Old 01-15-2009, 10:49 AM   #6
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
Quote:
Originally Posted by jschiwal View Post
I think it would take far less time learning to use awk to solve the problem then learning an entire language. Perl may be better at solving it, but learning to code in perl would take a very long time.
Everytime you say this to yourself you will learn a little patch on something that will add up to a lot more if you just admit to yourself that you would be better off learning some perl. You don't have to learn it all and I guarantee learning to do this in perl will not be any more complicated than learning it in awk (which is also considered a language b/t/w)or with a shell, or whatever.

Anyway, the OP seems intent on learning shell scripting! So it looks like colucix saved the day.
 
Old 01-15-2009, 04:32 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
admit to yourself that you would be better off learning some perl
Not knowing perl has not slowed me down one bit!!! YMMV
 
Old 01-16-2009, 01:33 AM   #8
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
I think it would take far less time learning to use awk to solve the problem then learning an entire language. Perl may be better at solving it, but learning to code in perl would take a very long time.
Thank you for the post. Now, could you please help me solve this using AWK as havent really got the grips of it as much..?
 
Old 01-16-2009, 09:28 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Here's a simple example:

awk -F, '{print $1, $3}' filename

This sets the field separator to "," and then prints the first and third columns

Really good tutorial here;
http://www.grymoire.com/Unix
 
Old 01-19-2009, 02:28 AM   #10
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
Here's a simple example:

awk -F, '{print $1, $3}' filename

This sets the field separator to "," and then prints the first and third columns

Really good tutorial here;
http://www.grymoire.com/Unix
Thanks pixellany. This works perfectly fine and it's fairly short. Thank you again
 
  


Reply


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
Script to extract the fields in the agiml tags akhtar.bhat Linux - Software 1 12-17-2008 07:13 AM
bash shell script find and edit fields in a file hchoonbeng Programming 9 10-29-2008 03:13 AM
script for pasting fields in files bhandu Linux - Newbie 2 06-28-2007 08:20 AM
script to relay the data in the fields to my email address generalachoo Programming 4 08-25-2006 08:54 AM
Two New LQ Profile Fields jeremy LQ Suggestions & Feedback 8 07-13-2005 12:10 AM

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

All times are GMT -5. The time now is 03:32 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration