Operating System - HP-UX
1833800 Members
2319 Online
110063 Solutions
New Discussion

scripting question: Filtering certain fields and data. ?

 
SOLVED
Go to solution
rveri
Super Advisor

scripting question: Filtering certain fields and data. ?

Hi Experts,

Here is the data file:
---------------------------------------------
0769 021E c4t3d3/14A c9t3d3/08B c11t3d3/09A c17t3d3/03B
0769 0256 c4t4d2/14A c9t4d2/08B c11t4d2/09A c17t4d2/03B
0769 0296 c4t5d2/14A c9t5d2/08B c11t5d2/09A c17t5d2/03B
0769 0326 c4t7d4/14A c9t7d4/08B c11t7d4/09A c17t7d4/03B
0769 0356 c4t8d2/14A c9t8d2/08B c11t8d2/09A c17t8d2/03B
0769 03DE c4t10d3/14A c9t10d3/08B c11t10d3/09A c17t10d3/03B
0769 042E c4t11d5/14A c9t11d5/08B c11t11d5/09A c17t11d5/03B
0769 048E c4t13d1/14A c9t13d1/08B c11t13d1/09A c17t13d1/03B
0769 04D6 c4t14d2/14A c9t14d2/08B c11t14d2/09A c17t14d2/03B
0769 05A6 c19t1d0/09A c20t1d0/08B c21t1d0/14A c22t1d0/03B
0769 05C6 c19t1d4/09A c20t1d4/08B c21t1d4/14A c22t1d4/03B
0769 0656 c19t3d6/09A c20t3d6/08B c21t3d6/14A c22t3d6/03B
0769 06DE c19t5d7/09A c20t5d7/08B c21t5d7/14A c22t5d7/03B
0769 06EE c19t6d1/09A c20t6d1/08B c21t6d1/14A c22t6d1/03B
0769 071E c19t6d7/09A c20t6d7/08B c21t6d7/14A c22t6d7/03B
0769 086E c19t12d1/09A c20t12d1/08B c21t12d1/14A c22t12d1/03B
0769 087E c19t12d3/09A c20t12d3/08B c21t12d3/14A c22t12d3/03B
0769 08AE c19t13d1/09A c20t13d1/08B c21t13d1/14A c22t13d1/03B
0769 0926 c24t14d6/14A c25t14d6/03B c26t14d6/08B c27t14d6/09A
0769 093E c24t15d1/14A c25t15d1/03B c26t15d1/08B c27t15d1/09A
0769 0A06 c24t2d0/14A c25t2d0/03B c26t2d0/08B c27t2d0/09A
0769 0A66 c24t3d4/14A c25t3d4/03B c26t3d4/08B c27t3d4/09A
0769 0A86 c24t4d0/14A c25t4d0/03B c26t4d0/08B c27t4d0/09A
0769 0AAE c24t4d5/14A c25t4d5/03B c26t4d5/08B c27t4d5/09A
0769 0AC6 c24t5d0/14A c25t5d0/03B c26t5d0/08B c27t5d0/09A
0769 0AD6 c24t5d2/14A c25t5d2/03B c26t5d2/08B c27t5d2/09A
0769 0C9E c24t12d3/14A c25t12d3/03B c26t12d3/08B c27t12d3/09A
0769 140E c41t1d1/08B c42t1d1/09A c43t1d1/14A c44t1d1/03B
0769 141E c41t1d2/08B c42t1d2/09A c43t1d2/14A c44t1d2/03B
0769 1426 c41t1d3/08B c42t1d3/09A c43t1d3/14A c44t1d3/03B
0769 1436 c41t1d4/08B c42t1d4/09A c43t1d4/14A c44t1d4/03B
----------------------------------------------


I need to filter all the devices ending with 'B' ( example: c9t3d3/08B ) and need to produce a single file with all the 'B' devices one after another in a column format.

The file attached to view in the proper format:


The output should be look like this:
c41t1d2/08B
c41t1d4/08B
c25t12d3/03B
c25t5d2/03B
...



Please suggest how to do it with the help of a script,

Thanks,

10 REPLIES 10
rajdev
Valued Contributor

Re: scripting question: Filtering certain fields and data. ?

Hi,

Since your starting character is 'c' and ending is 'B' , you can do the following to get as you wanted.

cat filename |tr ' ' '\n' |grep '^c'|grep 'B$'

Regards,
RD
Dennis Handly
Acclaimed Contributor

Re: scripting question: Filtering certain fields and data. ?

This awk script scans field 3, 4, 5 and 6 and prints that field if it ends in "B".
awk '
BEGIN { PAT = "B$" }
$3 ~ PAT { print $3 }
$4 ~ PAT { print $4 }
$5 ~ PAT { print $5 }
$6 ~ PAT { print $6 }
' filename

Is there any ordering requirements?
rajdev
Valued Contributor

Re: scripting question: Filtering certain fields and data. ?

Hi,

note: there is a single space in the tr ...

cat filename |tr ' ' '\n' |grep '^c'|grep 'B$' > filename.out


Regards,
RD
Dennis Handly
Acclaimed Contributor

Re: scripting question: Filtering certain fields and data. ?

>RD: cat filename |tr ' ' '\n' |grep '^c'|grep 'B$'

This can be optimized to remove useless cats:
tr ' ' '\n' < filename | grep '^c.*B$'
Hein van den Heuvel
Honored Contributor

Re: scripting question: Filtering certain fields and data. ?

and one of many obligatory perl alternatives....

$ perl -ne 'while (/\b(c\S+B)\b/) {print qq($1\n);$_=$'"'}" filename

This is a double loop, the -n looping over the records.
The a loop looking for:
\b = a word boundary
( = start remembering
c
\S+ a series of 1 or more non-whitespace
B
) = stop remembering
\b a word boundary

If matched, print the remembered part and make the default variable $_ become the 'post-match' remainder of the line.
The is expressed with $'
Of course the ' fights with the initial '
One way to solve that is a terminate the 'program' string and open a new "rest" string with the '. Ugly.
There are alternatives, and this problem does not exist for a program in a script. It is a command line thing.

Hein.

James R. Ferguson
Acclaimed Contributor

Re: scripting question: Filtering certain fields and data. ?

Hi Rveri:

Here's another (TMTOWTDI) Perl:

# perl -nle '{@a=split;print for /c\S+B/g}' file

...This splits each line on whitespace (spaces, tabs, newlines) and looks for strings beginning with a "c" and ending with "B" with at least one non-whitespace (\S+) character between. For every matching occurance on each line, the matched token is printed.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: scripting question: Filtering certain fields and data. ?

Hi (again) Rveri:

Ooops, ignore my first post. I posted a composite of two approaches without sufficient refinement. Instead, use:

# perl -nle '{print for /\bc\S+B\b/g}' file

Regards!

...JRF...
Sandman!
Honored Contributor
Solution

Re: scripting question: Filtering certain fields and data. ?

# awk '{for(i=1;i<=NF;++i) if($i~"B$") print $i}' file
rveri
Super Advisor

Re: scripting question: Filtering certain fields and data. ?

Awesome scripts! . Thanks.
I like all the scripts given here : tr, perl , awk and all worked fine. Though one perl script found to be long enough. And the best one found the oneliner one. Thanks all (again!).
Dennis Handly
Acclaimed Contributor

Re: scripting question: Filtering certain fields and data. ?

>And the best one found the oneliner one.

The best one may be the one you can understand and perhaps modify. (That's why I had the 4 fields.)