Complex structure from Specific Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex structure from Specific Column
# 8  
Old 01-30-2012
It like your code, Skrynesaver. It just little edit.

Code:
perl -Mstrict -e '
sub round{
   my $num=shift;
   return int($num + 0.5);
}
my $rec_id=undef;

while(<>){
   my ($A,$B,$dat)=split(/\s+/,$_);
   if($A != $rec_id){
      print "\n                   ( ", $rec_id=round($A),") = A", round($B),"B ",round($dat),",\n";
   }
   else {
      print "A ", round($B), "B ", round ($dat),",";
   }
}
print "\n";' input.dat> output.dat.

Expected output has not been obtained.
# 9  
Old 01-30-2012
"? : is known as the ternary operator and can be used to set a value conditionally

Code:
 perl -Mstrict -e '
sub round{
   my $num=shift;
   return int($num + 0.5);
}
my $rec_id=undef;
my $rec_count;
while(<>){
   my ($A,$B,$dat)=split(/\s+/,$_);
   if($A != $rec_id){
      print "\n\t\t\t\t ( ", $rec_id=round($A)," ) = A ", round($B),"B ",round($dat),",\n\t\t";
      $rec_count=0;
   }
   else {
      print "A ", round($B), "B ", round ($dat),(($rec_count > 1 ) && ($rec_count %5) == 0) ?",\n\t\t":",";
      $rec_count++;

   }
}
print "\n";' tmp/tmp.dat

# 10  
Old 01-31-2012
Quote:
Originally Posted by attila
Dear all, I have many files like this:

input :

Code:
 1083677.0     162.9      1969.2      
 1083677.0     481.7      2236.2      
 1083677.0     754.9      2515.4      
 1083677.0    1041.8      2733.5      
 1083677.0    1310.5      2952.6      
 1083677.0    1588.3      3204.2      
 1083677.0    2421.7      3673.6      
 1083677.0    3988.2      4027.6      
 1083677.0    5062.9      4172.9      
 1083699.0     135.6      1993.2      
 1083699.0     358.8      2164.3      
 1083699.0     691.2      2448.0      
 1083699.0    1028.2      2760.0      
 1083699.0    1938.9      3475.8      
 1083699.0    2804.2      3979.2      
 1083699.0    3633.0      4153.5

Output that I want:

output:
Code:
                ( 1083677)=A  163B 1969,                 
      A  482B 2236,A  755B 2515,A 1042B 2734,A 1311B 2953,A 1588B 3204,
      A 2422B 3674,A 3988B 4028,A 5063B 4173,   
                ( 1083699)=A  136B 1993,    
      A  359B 2164,A  691B 2448,A 1028B 2760,A 1939B 3476,A 2804B 3979,
      A 3633B 4154,

Note: important structure : output data must be not contain decimal.

Many thanks in advance.

-Attila
Code:
# awk '{a[x++]=$0;b[$1]++}
function calrnum(ar,i){
$R=ar[i];f=gensub(".*\\.","\\1","R")
if(f>=5){ar[i]+=1};if(i%2!=1){printf " %4d%s",ar[i],"B";}else{printf " %4d%s",ar[i],",A";}}
function last(ar,i){
$R=ar[i];f=gensub(".*\\.","\\1","R")
if(f>=5){ar[i]+=1};printf " %d",ar[i];}
END{for(i in b){n[xx++]=b[i]*(NF-1)-(NF-1);};nx=0;;split(a[0],aa);split(a[1],ab);fNF=NF;
{for(c=1;c<=fNF;c++)if(c==1){printf "%27s","( "int(aa[c])")=A"}else{if(c==fNF)last(aa,c);
else{calrnum(aa,c);}}printf "%s\n%6s",",","A"};cc++
for(c=2;c<=fNF;c++)calrnum(ab,c);cc++;for(i=1;i<x;i++){split(a[i],aa);split(a[i+1],ab)
if(aa[1]==ab[1]){for(j=2;j<=length(ab);j++){cc++;ix++;ncc=0;if(n[nx]-(fNF-1)==ix){last(ab,j);ncc=cc};
if(cc!=10&&ncc!=cc)calrnum(ab,j);if(cc==10&&ncc!=cc){last(ab,j);printf "%s\n%6s",",","A";cc=0}}}
else{cc=0;printf "%s\n",",";for(c=1;c<fNF;c++){if(ab[c])if(c==1){printf "%27s","( "int(ab[c])")=A"}
else{nx++;n[nx]=n[nx]+n[nx-1];calrnum(ab,c);;printf " %d,\n%6s",ab[fNF],"A"}}}}}' infile
               ( 1083677)=A  163B 1969,
     A  482B 2236,A  755B 2515,A 1042B 2734,A 1311B 2953,A 1588B 3204,
     A 2422B 3674,A 3988B 4028,A 5063B 4173,
               ( 1083699)=A  136B 1993,
     A  359B 2164,A  691B 2448,A 1028B 2760,A 1939B 3476,A 2804B 3979,
     A 3633B 4154,

regards
ygemici
This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

Search the specific content from the complex file

Hi, I have a file with complex data without delimiter, have requirement to fetch the specific record based on some charcters. here is my file data ... (12 Replies)
Discussion started by: Riverstone
12 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

4. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

5. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

6. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

7. Shell Programming and Scripting

Script to search specific folders dates /mm/dd/ structure

Hi, I have a script that handles a huge amount of log files from many machines and copies it into a SAN location with the following directory structure: /SAN/machinenames/yyyy/m/d so for example /SAN/hosta/2011/3/12/files* Now I am writing a bash script to search for files between to date... (4 Replies)
Discussion started by: GermanJulian
4 Replies

8. UNIX for Dummies Questions & Answers

Help with copying specific parts of a file structure

Hello. I need help with copying part of a file structure to another directory while still keeping the structure. For example look below: ../folder1/sub1/txt.txt ../folder1/sub2/pic.png ../folder2/sub1/pic.png ../folder2/sub2/txt.txt So in this I would like to copy only the directories and... (3 Replies)
Discussion started by: the
3 Replies

9. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question