Unable to solve this
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Unable to solve this




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Unable to solve this  
pankaj_wolfhunter@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

Greetings,

I have a file as:

name1|value1|value2|N
name1|value3|value4|N
name1|value5|value6|N
name1|value7|value8|Y
name2|value1|value2|N
name2|value3|value4|N
name2|value5|value6|Y
.....................
.....................

I want to start looping the file and for each name(name1, name2) and
for each name
I want to check the value in 4 column.
If the column value is N then I want to move to the next line for the
same
name. Whenever I find the fourth column value as Y, I want to put
only 2nd and 3rd column value for this name into one line along with
its name.

Req Output

 name1|value1|value2|value3|value4|value5
|value6|value7|value8
 name2|value1|value2|value3|value4|value5
|value6.

The prob here is, In between a name, a second name may appear.
like
name1|value1|value2|N
name3|value1|value2|N
name1|value3|value4|N
name1|value5|value6|Y

So if the script starts with one name then it should go on with that
name
until the fourth column value becomes Y and then for the second name
and so on.

Can someone please help me build with this logic or some script
which would guide me in right direction.

Any help would be appreciated

TIA






[ Post a follow-up to this message ]



    Re: Unable to solve this  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

pankaj_wolfhunter@yahoo.co.in wrote:
> Greetings,
>
> I have a file as:
>
> name1|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|N
> name1|value7|value8|Y
> name2|value1|value2|N
> name2|value3|value4|N
> name2|value5|value6|Y
> .....................
> .....................
>
> I want to start looping the file and for each name(name1, name2) and
> for each name
> I want to check the value in 4 column.
> If the column value is N then I want to move to the next line for the
> same
> name. Whenever I find the fourth column value as Y, I want to put
> only 2nd and 3rd column value for this name into one line along with
> its name.
>
> Req Output
>
>  name1|value1|value2|value3|value4|value5
|value6|value7|value8
>  name2|value1|value2|value3|value4|value5
|value6.
>
> The prob here is, In between a name, a second name may appear.
> like
> name1|value1|value2|N
> name3|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|Y
>
> So if the script starts with one name then it should go on with that
> name
> until the fourth column value becomes Y and then for the second name
> and so on.
>
> Can someone please help me build with this logic or some script
> which would guide me in right direction.
>
> Any help would be appreciated
>
> TIA
>

Here is the program...

awk -F"|" '
{ n[$1] = n[$1] "|" $2 "|" $3 }
$4 == "Y" { print $1 "|" n[$1] ; delete n[$1] }
'


Janis





[ Post a follow-up to this message ]



    Re: Unable to solve this  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

Janis Papanagnou wrote:
> pankaj_wolfhunter@yahoo.co.in wrote:
> 
>
> Here is the program...
>
>   awk -F"|" '
>       { n[$1] = n[$1] "|" $2 "|" $3 }
>       $4 == "Y" { print $1 "|" n[$1] ; delete n[$1] }

Sorry, this line should have been...

$4 == "Y" { print $1 n[$1] ; delete n[$1] }

>     '

Janis





[ Post a follow-up to this message ]



    Re: Unable to solve this  
Loki Harfagr


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

Le Sat, 18 Mar 2006 07:38:32 -0800, pankaj_wolfhunter a écrit_:

> Greetings,
>
> I have a file as:
>
> name1|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|N
> name1|value7|value8|Y
> name2|value1|value2|N
> name2|value3|value4|N
> name2|value5|value6|Y
> .....................
> .....................
>
> I want to start looping the file and for each name(name1, name2) and
> for each name
> I want to check the value in 4 column.
> If the column value is N then I want to move to the next line for the
> same
> name. Whenever I find the fourth column value as Y, I want to put
> only 2nd and 3rd column value for this name into one line along with
> its name.
>
> Req Output
>
>  name1|value1|value2|value3|value4|value5
|value6|value7|value8
>  name2|value1|value2|value3|value4|value5
|value6.
>
> The prob here is, In between a name, a second name may appear.
> like
> name1|value1|value2|N
> name3|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|Y
>
> So if the script starts with one name then it should go on with that
> name
> until the fourth column value becomes Y and then for the second name
> and so on.
>
> Can someone please help me build with this logic or some script
> which would guide me in right direction.
>
> Any help would be appreciated
>
> TIA


Apart the good awk solution Janis Papanagnou posted which I would
use in your case, but as you didn't post what exactly was the process
you used, I just wondered  if sorting your file before processing it
thru your filter wouldn't be just what you'd want ?

As your 'triggers' are on sorted names and "N" then "Y"
it's already a sorted index, then given your sample :

$ cat MISCFILES/sorteed.txt
name1|value1|value2|N
name3|value1|value2|N
name1|value3|value4|N
name1|value5|value6|N
name1|value7|value8|Y
name2|value1|value2|N
name2|value3|value4|N
name2|value5|value6|Y

Using this would bring you in good shape for your filtering ?

$ sort MISCFILES/sorteed.txt

name1|value1|value2|N
name1|value3|value4|N
name1|value5|value6|N
name1|value7|value8|Y
name2|value1|value2|N
name2|value3|value4|N
name2|value5|value6|Y
name3|value1|value2|N

Please, if this is wrong, comment and give sample for
failure of this, as I'm not sure I've understood all what
you wanted to nor what you already had !?-D)





[ Post a follow-up to this message ]



    Re: Unable to solve this  
John W. Krahn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

pankaj_wolfhunter@yahoo.co.in wrote:
>
> I have a file as:
>
> name1|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|N
> name1|value7|value8|Y
> name2|value1|value2|N
> name2|value3|value4|N
> name2|value5|value6|Y
> .....................
> .....................
>
> I want to start looping the file and for each name(name1, name2) and
> for each name I want to check the value in 4 column.
> If the column value is N then I want to move to the next line for the
> same name. Whenever I find the fourth column value as Y, I want to
> put only 2nd and 3rd column value for this name into one line along
> with its name.
>
> Req Output
>
>  name1|value1|value2|value3|value4|value5
|value6|value7|value8
>  name2|value1|value2|value3|value4|value5
|value6.
>
> The prob here is, In between a name, a second name may appear.
> like
> name1|value1|value2|N
> name3|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|Y
>
> So if the script starts with one name then it should go on with that
> name until the fourth column value becomes Y and then for the second
> name and so on.
>
> Can someone please help me build with this logic or some script
> which would guide me in right direction.

$ PERL -F'\|' -lane'
( $name, $val ) = ( shift @F, pop @F );
push @{ $data{ $name } }, @F;
print join "|", $name, splice @{ $data{ $name } } if $val eq "Y";
' yourfile


John
--
use Perl;
program
fulfillment





[ Post a follow-up to this message ]



    Re: Unable to solve this  
William James


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-19-06 05:02 PM

John W. Krahn wrote:
> pankaj_wolfhunter@yahoo.co.in wrote: 
>
> $ PERL -F'\|' -lane'
> ( $name, $val ) = ( shift @F, pop @F );
> push @{ $data{ $name } }, @F;
> print join "|", $name, splice @{ $data{ $name } } if $val eq "Y"
;
> ' yourfile

Ruby:

h = Hash.new { "" }
while gets =~ /(.*?)(\|.*)\|(.*)/
h[$1] += $2
puts $1 + h[$1]   if "Y" == $3
end






[ Post a follow-up to this message ]



    Re: Unable to solve this  
Xicheng


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-21-06 08:17 AM

pankaj_wolfhunter@yahoo.co.in wrote:
> Greetings,
>
> I have a file as:
>
> name1|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|N
> name1|value7|value8|Y
> name2|value1|value2|N
> name2|value3|value4|N
> name2|value5|value6|Y
> .....................
> .....................
>
> I want to start looping the file and for each name(name1, name2) and
> for each name
> I want to check the value in 4 column.
> If the column value is N then I want to move to the next line for the
> same
> name. Whenever I find the fourth column value as Y, I want to put
> only 2nd and 3rd column value for this name into one line along with
> its name.
>
> Req Output
>
>  name1|value1|value2|value3|value4|value5
|value6|value7|value8
>  name2|value1|value2|value3|value4|value5
|value6.
>

perl -F'\|' -lane ' $h{ $F[0] } .= "|$F[1]|$F[2]";
print "$F[0]$h{$F[0]}" if $F[3] eq "Y" '
myfile.txt

Xicheng

> The prob here is, In between a name, a second name may appear.
> like
> name1|value1|value2|N
> name3|value1|value2|N
> name1|value3|value4|N
> name1|value5|value6|Y
>
> So if the script starts with one name then it should go on with that
> name
> until the fourth column value becomes Y and then for the second name
> and so on.
>
> Can someone please help me build with this logic or some script
> which would guide me in right direction.
>
> Any help would be appreciated
>
> TIA






[ Post a follow-up to this message ]



    Re: Unable to solve this  
pankaj_wolfhunter@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-21-06 08:17 AM

Thanks a ton people. I am really thankful to all of you.
I got what a wanted. Thanks again.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:19 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register