1846455 Members
2561 Online
110256 Solutions
New Discussion

script help

 
SOLVED
Go to solution
sheevm
Regular Advisor

script help

Hi All,

I have the following lines as input:


10:27:01 0 0 0 0 100
1 0 1 0 99
2 3 0 0 97
3 6 1 0 92



I want to format the ouput as:

Ex:
10:27:01-0-1-2-3-0-0-3-6-0-1-0-1-0-0-0-0-100-99-97-92


Thanks!!
Rajim
be good and do good
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: script help

Hi Rajim:

# perl -nale 'push @a,join "-", @F;END{print join "-",@a}' file

Regards!

...JRF...
sheevm
Regular Advisor

Re: script help

Hi James,

Thanks. I just executed your command it kind of worked. but not exactly. Pardon me, I do not know perl.

10:27:01 0 0 0 0 100
1 0 1 0 99
2 3 0 0 97
3 6 1 0 92

I want (2nd row through 4th row coulmn data)

10:27:01-0-1-2-3-0-0-3-6-0-1-0-1-0-0-0-01-100-99-97-92

Your command gives

10:27:01-0-0-0-0-100-1-0-1-0-99-2-3-0-0-97-3-6-1-0-92

Thanks.

Can you suggest me a good book on perl for dummies

be good and do good
Sandman!
Honored Contributor
Solution

Re: script help

Try the awk script below...


awk '{
if(NR==1) {
tmp=$1
for (i=2;i<=NF;++i) {
if (x[i])
x[i]=x[i]"-"$i
else
x[i]=$i
}
}
else
for (i=1;i<=NF;++i)
x[i+1]=x[i+1]"-"$i
}END{
for (i=1;i<=NF+1;++i) {
if (z)
z=z"-"x[i]
else
z=tmp
}
print z
}' infile
Hein van den Heuvel
Honored Contributor

Re: script help

Rajim,

That was a horrible problem specification.
>> I want (2nd row through 4th row coulmn data)

Your data suggests you wnat 1st thru 5th unless there is a timestamp, in which case you want 2 - 6

That appears to be a horrible output format.
What if you ever get an other line of data?

Anyway, here is a chunk of perl with can read multiple blocks of your data pattern:

---- input ---
10:27:01 0 0 0 0 100
1 0 1 0 99
2 3 0 0 97
3 6 1 0 92
10:27:23 0 a 0 0 80
1 b 1 0 81
2 c 0 0 82
3 d 1 0 83

--------------- output --------

10:27:01-0-1-2-3-0-0-3-6-0-1-0-1-0-0-0-0-100-99-97-92
10:27:23-0-1-2-3-a-b-c-d-0-1-0-1-0-0-0-0-80-81-82-83

----------- perl ------
while (<>) {
@words = split;
if (/^\d*:\d+:/) {
&myprint;
undef @data;
$line = 0;
$time = shift @words;
}
$col = 0;
while (defined ($x = shift @words)) {
$data[$line][$col++] = $x;
}
$line++;
}
&myprint;

sub myprint {
if ($time) {
print "$time";
for $c (0..4) {
for $l (0..$line-1) {
print '-',$data[$l][$c];
}
}
print "\n";
}
}

Peter Nikitka
Honored Contributor

Re: script help

Hi,

this seems to me a simple awk solution:

awk -v col=5 'function out () {
printf dtm;for (j=1;jNF == (col+1) {if(found) out();dtm=$1; for (i=2;i<=NF;i++) a[i-1]=$i;found++}
NF == col {for (i=1;i<=NF;i++) a[i]=a[i]"-"$i}
END {if(found) out()}' output-file

For customization, set
awk -v col=5 ...
to the appropriate value. Multiple blocks (starting with an additional date field) are processed and reported.
Lines not containing col or (col+1) columns are ignored.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
sheevm
Regular Advisor

Re: script help

Thanks for all of your responses.

I will be submitting points soon.
be good and do good