|
Home > Archive > Unix Shell > March 2006 > Unable to solve this
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Unable to solve this
|
|
| pankaj_wolfhunter@yahoo.co.in 2006-03-19, 12: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
| |
| Janis Papanagnou 2006-03-19, 12: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
| |
| Janis Papanagnou 2006-03-19, 12: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
| |
| Loki Harfagr 2006-03-19, 12: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)
| |
| John W. Krahn 2006-03-19, 12: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
| |
| William James 2006-03-19, 12: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
| |
| Xicheng 2006-03-21, 3: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
| |
| pankaj_wolfhunter@yahoo.co.in 2006-03-21, 3:17 am |
| Thanks a ton people. I am really thankful to all of you.
I got what a wanted. Thanks again.
|
|
|
|
|