awk - filtering data by if --> into an array

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions awk - filtering data by if --> into an array
# 8  
Old 08-22-2012
You haven't answered my question. What relation does your input have with your output? Does the column it ends up in, depend on the first digit of the triplet? Yes? No? Maybe?

This might do what you want... Maybe...


Code:
awk -F"," -v OFS="\t" '{
        L=$10
        N=substr(L, 1, 1)+1;
        $0=$1"-"$2";
        for(N=2; N<=5; N++) $N="-"
        if(L) $N=L } 1' input > output

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 08-23-2012
Quote:
Originally Posted by Corona688
You haven't answered my question. What relation does your input have with your output? Does the column it ends up in, depend on the first digit of the triplet? Yes? No? Maybe?

This might do what you want... Maybe...

Code:
awk -F"," -v OFS="\t" '{
        L=$10
        N=substr(L, 1, 1)+1;
        $0=$1"-"$2";
        for(N=2; N<=5; N++) $N="-"
        if(L) $N=L } 1' input > output

Hi Corona688!

Thanks for your help. First i will answer the question:
The first triplet-number should be the marker for the coresponding column.
for example:
- triplet 233 according to the timestamp in column 3
- triplet 123 according to the timestamp in column 2
- triplet 433 according to the timestamp in column 5
- triplet 311 according to the timestamp in column 4
Sorry for the slovenly reaktion on your question.
Finally there a two points:
1. I change the code slightly, because i have to change the dateformat
Code:
 awk -F"," -v OFS="\t" '{
                L=$10
                N=substr(L, 1, 1)+1;
                $0=substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9, 2)"-"$2;
                for(N=2; N<=4; N++) $N="-"
                if(L) $N=L } 1' input.dat > output.txt

2. The code is working efficently and fast, but the result is
every triplet is written into the last column ..
Code:
01/01/12-00:50  -       -       -       423
01/01/12-01:00  -       -       -       423
01/01/12-01:10  -       -       -       423
01/01/12-01:20  -       -       -       -
01/01/12-01:30  -       -       -       -
01/01/12-01:40  -       -       -       -
01/01/12-01:50  -       -       -       223
01/01/12-02:00  -       -       -       223
....

I try to solve it but without results - can you please help me.

Thanks in advance!
IMPe
# 10  
Old 08-23-2012
Don't use N in the for loop; it's holding the col no. for your output.
# 11  
Old 08-23-2012
i had made some changes to your awk command
Code:
awk -F"," '{ 
                      if($10 > 400)
                          print  substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9, 2) "-" $2,"-","-","-",$10
	              else if ($10 < 400 && $10 > 300)
                          print  substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9, 2) "-" $2,"-","-",$10,"-"
                      else if ($10 < 300 && $10 > 200)  
                         print  substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9,  2) "-" $2 ,"-",$10,"-","-"
	              else if ($10 < 200 && $10 > 100)
                         print  substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9, 2) "-" $2,"-",$10,"-","-","-"}' OFS=\t  $home/$f > index.txt

This User Gave Thanks to raj_saini20 For This Post:
# 12  
Old 08-23-2012
Fix for the code which probably doesn't do what you want:
Code:
awk -F"," -v OFS="\t" '{
                L=$10
                N=substr(L, 1, 1)+1;
                $0=substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9, 2)"-"$2;
                for(M=2; M<=4; M++) $M="-"
                if(L) $N=L } 1' input.dat > output.txt

Quote:
Originally Posted by IMPe
The first triplet-number should be the marker for the coresponding column.
for example:
- triplet 233 according to the timestamp in column 3
- triplet 123 according to the timestamp in column 2
- triplet 433 according to the timestamp in column 5
- triplet 311 according to the timestamp in column 4
But HOW do you get 5 from 433? Smilie

HOW do you get 4 from 311? Smilie

You have not shown representative input for your output, so we are only guessing.

We will be stuck guessing until you show representative input.

Please show representative input and output -- lines that have anything to do with each other! -- or I will close this thread.
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 08-24-2012
Quote:
Originally Posted by Corona688
Fix for the code which probably doesn't do what you want:
Code:
awk -F"," -v OFS="\t" '{
                L=$10
                N=substr(L, 1, 1)+1;
                $0=substr($1, 4, 2) "/" substr($1, 1, 2) "/" substr($1, 9, 2)"-"$2;
                for(M=2; M<=4; M++) $M="-"
                if(L) $N=L } 1' input.dat > output.txt

But HOW do you get 5 from 433? Smilie

HOW do you get 4 from 311? Smilie

You have not shown representative input for your output, so we are only guessing.
We will be stuck guessing until you show representative input.
Please show representative input and output -- lines that have anything to do with each other! -- or I will close this thread.
Pardon me, but i'm afraid i didnt understand you right - sorry for the confusion.
i will try to concentrate my specification.
1. a snippet of the original input data [csv] i posted in #1
there i've forgotten to mention, that the triplet number in column $10 varies from
111 ... 433 [ #3 ]
2. the awk-script should basically
- read the input data [$1], time [$2] and the triplet-number [ $10 ]
- change the dateformat and accomplish it with the time-value to one timestamp.
3. the output >
in column $1 write the timestamp and depending from the first tripletncipher the related tripletnumber in the aproppriate column - for example
Code:
$1                      $2    $3     $4     $5
...
02/04/12-06:10  131   -       -       -
02/04/12-06:20  -       213   -       -
02/04/12-06:30  -       -       311   -
02/04/12-06:40  -       -       -       421
02/04/12-06:50  -       -       331    -
...

.. so that a triblet-no. with first cipher 4 should be written in column $5
or a triplet-no. with first cipher 1 should be written in column $2
or a triplet-no. with first cipher 3 should be written in column $4
and a triplet-no. with first cipher 2 should be written in column $3
and the occurred, TAB-separated blank spaces should be filled up with "-"

But with the last changes, the script is very efficently and exactly generating output as desired.

Thank you very much!
IMPe
This User Gave Thanks to IMPe For This Post:
# 14  
Old 08-25-2012
A simple 'yes' pages ago would have been sufficient since my original guess, by pure luck, turned out to be correct.

Thank you.
This User Gave Thanks to Corona688 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

Need help Filtering Data from an API

Hi Everyone, I need help on figuring out a way to filter some data that I get back from an API. Im able to get all the data that Im looking for but I would like to know a way for me to filter it better. The data that Im getting back is basically 2 rows of data as seen here. Row 1 ... (25 Replies)
Discussion started by: TheStruggle
25 Replies

2. Shell Programming and Scripting

Filtering out the data with dates

Hi, I have some data like seen below. format : apple(hhmm mm/dd).fruit apple(2345 03/25).fruit apple(2345 05/06).fruit orange(0443 05/02).fruit orange(0345 05/05).fruit orange(2134 05/04).fruit grape(0930 04/24).fruit grape(2330 03/30).fruit I need to get the data which are... (1 Reply)
Discussion started by: jayadanabalan
1 Replies

3. Shell Programming and Scripting

awk - filtering data by if --> into an array

Hi my data is in csv-format ... ... 13/08/2012,16:30,303.30,5.10,3,2,2,1,9360.0,322 13/08/2012,16:40,305.50,5.00,3,2,2,1,12360.0,322 13/08/2012,16:50,319.90,3.80,3,2,1,0,2280.0 13/08/2012,17:00,326.10,3.50,3,2,1,1,4380.0,321 13/08/2012,17:10,333.00,3.80,3,3,1,0,2280.0... (1 Reply)
Discussion started by: IMPe
1 Replies

4. Shell Programming and Scripting

awk data filtering

I am trying to filter out some data with awk. If someone could help me that would be great. Below is my input file. Date: 10-JUN-12 12:00:00 B 0: 00 00 00 00 10 00 16 28 B 120: 00 00 00 39 53 32 86 29 Date: 10-JUN-12 12:00:10 B 0: 00 00 00 00 10 01 11 22 B 120: 00 00 00 29 23 32 16 29... (5 Replies)
Discussion started by: thibodc
5 Replies

5. Shell Programming and Scripting

Filtering data using AWK

Hi , i have file with delimiter as "|" and data in Double codes for all fields. how to filter data in a column like awk -F"|" '$1="asdf" {print $0}' test. ex : "asdf"|"zxcv" Thanks, Soma (1 Reply)
Discussion started by: challamsomu
1 Replies

6. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

7. Shell Programming and Scripting

help need in filtering data

Hello Gurus, Please help me out of the problem. I ve a input file as below input clock; input a; //reset all input b; //input comment output c; output d; output e; input f; //output comment I need the output as follows: \\Inputs (1 Reply)
Discussion started by: user_prady
1 Replies

8. UNIX for Dummies Questions & Answers

Filtering Data

file1 contain: (this just a small sample of data it may have thousand of lines) 1 aaa 1/01/1975 delhi 2 bbb 2/03/1977 mumbai 3 ccc 1/01/1975 mumbai 4 ddd 2/03/1977 chennai 5 aaa 1/01/1975 kolkatta 6 bbb 2/03/1977 bangalore program: nawk '{ idx= $2 SUBSEP $3 arr = (idx in arr) ?... (2 Replies)
Discussion started by: bobo
2 Replies

9. Shell Programming and Scripting

Filtering Data

Hi All, I have the below input and expected ouput. I need a code which can scan through this input file and if the number in column1 is more than 1 , it will print out the whole line, else it will output "No Re-occurrence". Can anybody help ? Input: 1 vvvvv 20 7 7 23 0 64 6 zzzzzz 11 5... (7 Replies)
Discussion started by: Raynon
7 Replies

10. UNIX for Dummies Questions & Answers

Filtering out data ...

I have following command which tells me File size in GBs which are greater than 0.01GBs recursively in a dir structure. ls -l -R | awk '{ if ($5/1073741824 >= 0.01) print $9, $5/1073741824 }' But there are some files whom I dont have enough permissions, after executing this script gives me... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question