starting with AWK - awk
This is a discussion on starting with AWK - awk ; Hi there. I just discover awk and I want to use it for my experimetal
data. I did some experiments and the machine gave me a *.txt file, like
this:
A1, A2, A3, A4, ..., An
B1, B2, B3, B4, ...
-
starting with AWK
Hi there. I just discover awk and I want to use it for my experimetal
data. I did some experiments and the machine gave me a *.txt file, like
this:
A1, A2, A3, A4, ..., An
B1, B2, B3, B4, ..., Bn
C1, C2, C3, C4, ..., Cn
......................................
M1, M2, M3, M4, ..., Mn
N1, N2, N3, N4, ..., Nn
I'm wondering if I could do some operations like:
cap=A3*(B2-B1)+B3*(C2-C1)+...+M3*(N2-N1)
Or, how can I delete let's say first 1000 lines from a txt file or how
can I save a part of the file, for exmple from line 1000 to line 2000.
It's possible with AWK this kind of operations?
Thanx
-
Re: starting with AWK
horatiu wrote:
> Hi there. I just discover awk and I want to use it for my experimetal
> data. I did some experiments and the machine gave me a *.txt file, like
> this:
> A1, A2, A3, A4, ..., An
> B1, B2, B3, B4, ..., Bn
> C1, C2, C3, C4, ..., Cn
> .....................................
> M1, M2, M3, M4, ..., Mn
> N1, N2, N3, N4, ..., Nn
>
> I'm wondering if I could do some operations like:
> cap=A3*(B2-B1)+B3*(C2-C1)+...+M3*(N2-N1)
>
If it's not to big, you could read the whole file into a 2-D array then
do operations in the END section, e.g.:
awk -F', ' '
{for (i=1;i<=NF;i++) a[NR,i] = $i}
END {
# A = a[1,<N>], B = a[2,<N>] so A3 = a[1,3], B2 = a[2,3], etc.
cap=a[1,3] * (a[2,2] - a[2,1]) + a[2,3] * ....
print cap
}
> Or, how can I delete let's say first 1000 lines from a txt file
Assuming UNIX:
awk 'NR>1000' file > tmp && mv tmp file
or how
> can I save a part of the file, for exmple from line 1000 to line 2000.
awk 'NR>=1000 && NR<=2000' file > newfile
> It's possible with AWK this kind of operations?
Yes.
Ed.
-
Re: starting with AWK
>
> awk 'NR>1000' file > tmp && mv tmp file
Thank you.
It works like that:
awk 'NR>1858' 5M-SM1_16-2ppm-02mVps.Txt > tmp && mv tmp
5M-SM1_16-200ppm-02mVps.txt
but I have 18 files like that so I tried to make a AWK program:
NR>1858 5M-SM1_16-2ppm-02mVps.Txt > tmp && mv tmp
5M-SM1_16-200ppm-02mVps.txt
NR>1858 5M-SM1_16-20ppm-02mVps.Txt > tmp && mv tmp
5M-SM1_16-200ppm-02mVps.txt
NR>1858 5M-SM1_16-200ppm-02mVps.Txt > tmp && mv tmp
5M-SM1_16-200ppm-02mVps.txt
but it doesn't work.
My files looks like:
, 6,-1.29026666666667, 0, 0
, 6.5,-1.28926666666667, 0, 0
, 7,-1.2882, 0, 0
, 7.5,-1.2872, 0, 0
, 8,-1.2862, 0, 0
, 8.5,-1.2852, 6.66666666666667E-02,-19278
, 9,-1.2842, 0, 0
, 9.5,-1.2832, 0, 0
, 10,-1.2822, 6.66666666666667E-02,-19233
, 10.5,-1.2812, 0, 0
, 11,-1.2802, 0, 0
, 11.5,-1.2792, 0, 0
, 12,-1.2782, 0, 0
, 12.5,-1.2772, 0, 0
, 13,-1.2762, 0, 0
, 13.5,-1.2752, 0, 0
, 14,-1.2742, 6.66666666666667E-02,-19113
, 14.5,-1.2732, 6.66666666666667E-02,-19098
, 15,-1.2722, 0, 0
First I want to find the line witch contain the value -1.2802
I did something like this:
awk '/-1.2802/{print $0}' 5M-SM1_16-2ppm-02mVps.Txt
but that did't gave the line number just print the lines that contain
that value
I don't know how to find the line number.
let's say that the value -1.2802 in in the 1858 line
how can I calculate the sum of the third column, from line 1858 to line
2018 and save it in a file?
maybe I'm not so clear ... briefly: how can I calculate the sum of o
column and put in program?
-
Re: starting with AWK
horatiu wrote:
> awk '/-1.2802/{print $0}' 5M-SM1_16-2ppm-02mVps.Txt
> but that did't gave the line number just print the lines that contain
> that value
>
> I don't know how to find the line number.
print NR
>
> let's say that the value -1.2802 in in the 1858 line
>
> how can I calculate the sum of the third column, from line 1858 to line
> 2018 and save it in a file?
NR==1858, NR==2018 { sum += $3}
END { print sum }
-
Re: starting with AWK
horatiu wrote:
>>awk 'NR>1000' file > tmp && mv tmp file
>
>
> Thank you.
>
> It works like that:
> awk 'NR>1858' 5M-SM1_16-2ppm-02mVps.Txt > tmp && mv tmp
> 5M-SM1_16-200ppm-02mVps.txt
>
> but I have 18 files like that so I tried to make a AWK program:
> NR>1858 5M-SM1_16-2ppm-02mVps.Txt > tmp && mv tmp
> 5M-SM1_16-200ppm-02mVps.txt
> NR>1858 5M-SM1_16-20ppm-02mVps.Txt > tmp && mv tmp
> 5M-SM1_16-200ppm-02mVps.txt
> NR>1858 5M-SM1_16-200ppm-02mVps.Txt > tmp && mv tmp
> 5M-SM1_16-200ppm-02mVps.txt
>
> but it doesn't work.
The above is not not an awk script. Is that really what you tried to
execute? I assume not, so post the real script and tell us in what way
it doesn't work.
> My files looks like:
> , 6,-1.29026666666667, 0, 0
> , 6.5,-1.28926666666667, 0, 0
> , 7,-1.2882, 0, 0
> , 7.5,-1.2872, 0, 0
> , 8,-1.2862, 0, 0
> , 8.5,-1.2852, 6.66666666666667E-02,-19278
> , 9,-1.2842, 0, 0
> , 9.5,-1.2832, 0, 0
> , 10,-1.2822, 6.66666666666667E-02,-19233
> , 10.5,-1.2812, 0, 0
> , 11,-1.2802, 0, 0
> , 11.5,-1.2792, 0, 0
> , 12,-1.2782, 0, 0
> , 12.5,-1.2772, 0, 0
> , 13,-1.2762, 0, 0
> , 13.5,-1.2752, 0, 0
> , 14,-1.2742, 6.66666666666667E-02,-19113
> , 14.5,-1.2732, 6.66666666666667E-02,-19098
> , 15,-1.2722, 0, 0
>
> First I want to find the line witch contain the value -1.2802
> I did something like this:
> awk '/-1.2802/{print $0}' 5M-SM1_16-2ppm-02mVps.Txt
> but that did't gave the line number just print the lines that contain
> that value
Yes, it would. You could have just written:
awk '/-1.2802/' 5M-SM1_16-2ppm-02mVps.Txt
with the same result since thd deafault action is to print the record.
Note that that would also match "-1.28029999" and other strings. If you
want to match exactly that string anywhere in the record you'd use:
awk '/^-1.2802$/' 5M-SM1_16-2ppm-02mVps.Txt
If you want to match exactly that string only in field 2 in the record
you'd use:
awk -F, '$3 ~ /^-1.2802$/' 5M-SM1_16-2ppm-02mVps.Txt
or
awk -F, '$3 == "-1.2802"' 5M-SM1_16-2ppm-02mVps.Txt
> I don't know how to find the line number.
awk -F, '$3 ~ /^-1.2802$/{ print NR, $0 }' 5M-SM1_16-2ppm-02mVps.Txt
will print the record number followed by the matching record.
> let's say that the value -1.2802 in in the 1858 line
>
> how can I calculate the sum of the third column, from line 1858 to line
> 2018 and save it in a file?
awk -F, 'NR>=1858{sum += $3} NR==2018{print sum; exit}' file > newfile
> maybe I'm not so clear ... briefly: how can I calculate the sum of o
> column and put in program?
>
I thought it was clear up until that last sentence. What do you mean by
"calculate the sum of o column and put in program"? Do you want a
program to calculate the sum, or do you want to calculate the sum and
use that value in a program?
Ed.
-
Re: starting with AWK
In article <qeKdnTCb27hKA5fYnZ2dnUVZ_s-dnZ2d@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>horatiu wrote:
>
>>>awk 'NR>1000' file > tmp && mv tmp file
>>
>>
>> Thank you.
>>
>> It works like that:
>> awk 'NR>1858' 5M-SM1_16-2ppm-02mVps.Txt > tmp && mv tmp
>> 5M-SM1_16-200ppm-02mVps.txt
>>
>> but I have 18 files like that so I tried to make a AWK program:
>> NR>1858 5M-SM1_16-2ppm-02mVps.Txt > tmp && mv tmp
>> 5M-SM1_16-200ppm-02mVps.txt
OP might find it easier to just do (<OT>):
#!/bin/sh
# Untested - and you *will* have to play with it to get the bugs out
for i in (*.Txt);do
echo "1,1858d|x" | ex - "$i"
done
-
Re: starting with AWK
>
> awk -F, 'NR>=1858{sum += $3} NR==2018{print sum; exit}' file > newfile
Sorry, something it's not clear for me yet.
My file "test.txt" is like this:
, 8.02,-.902266666666667, 28.2666666666667,-31.9198113207547
, 8.04,-.901266666666667, 27.4,-32.8929440389294
, 8.06,-.900333333333333, 26.4666666666667,-34.0176322418136
, 8.08,-.899333333333333, 25.2,-35.6878306878307
, 8.1,-.898333333333333, 24.0666666666667,-37.3268698060942
, 8.12,-.8972, 22.7333333333333,-39.466275659824
, 8.14,-.896266666666667, 20.8666666666667,-42.9520766773163
, 8.16,-.8952, 19.0666666666667,-46.9510489510489
, 8.18,-.8942, 17.0666666666667,-52.39453125
, 8.2,-.893266666666667, 14.8,-60.3558558558559
, 8.22,-.892266666666667, 12.7333333333333,-70.0732984293194
, 8.24,-.891333333333333, 10.9333333333333,-81.5243902439024
Now, if I do:
awk '{print $3}' test.txt
28.2666666666667,-31.9198113207547
27.4,-32.8929440389294
26.4666666666667,-34.0176322418136
25.2,-35.6878306878307
24.0666666666667,-37.3268698060942
22.7333333333333,-39.466275659824
20.8666666666667,-42.9520766773163
19.0666666666667,-46.9510489510489
17.0666666666667,-52.39453125
14.8,-60.3558558558559
12.7333333333333,-70.0732984293194
10.9333333333333,-81.5243902439024
but this is not the column 3 that I need. For this I replace all
"," with " " and I have the file "test-spaces.txt" like this:
8.02 -.902266666666667 28.2666666666667 -31.9198113207547
8.04 -.901266666666667 27.4 -32.8929440389294
8.06 -.900333333333333 26.4666666666667 -34.0176322418136
8.08 -.899333333333333 25.2 -35.6878306878307
8.1 -.898333333333333 24.0666666666667 -37.3268698060942
8.12 -.8972 22.7333333333333 -39.466275659824
8.14 -.896266666666667 20.8666666666667 -42.9520766773163
8.16 -.8952 19.0666666666667 -46.9510489510489
8.18 -.8942 17.0666666666667 -52.39453125
8.2 -.893266666666667 14.8 -60.3558558558559
8.22 -.892266666666667 12.7333333333333 -70.0732984293194
8.24 -.891333333333333 10.9333333333333 -81.5243902439024
Now here if I do
awk '{print $3}' test-spaces.txt
28.2666666666667
27.4
26.4666666666667
25.2
24.0666666666667
22.7333333333333
20.8666666666667
19.0666666666667
17.0666666666667
14.8
12.7333333333333
10.9333333333333
This is the column that I want to add all values, so if I use:
awk -F, 'NR>=3{sum += $3} NR==7{print sum; exit}' test-spaces.txt >
new.txt
the result is 0
-
Re: starting with AWK
Kenny McCormack wrote:
> OP might find it easier to just do (<OT>):
>
> #!/bin/sh
> # Untested - and you *will* have to play with it to get the bugs out
> for i in (*.Txt);do
> echo "1,1858d|x" | ex - "$i"
> done
sorry but I'm not so good at BASH and I save the script in a file
"test2" and when I tried ./test2 the result was
../test2: line 3: syntax error near unexpected token `('
../test2: line 3: `for i in (*.Txt);do'
Similar Threads
-
By Application Development in forum c++
Replies: 3
Last Post: 08-08-2007, 02:18 AM
-
By Application Development in forum Java
Replies: 2
Last Post: 04-06-2007, 07:32 AM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 04-10-2006, 10:16 PM
-
By Application Development in forum J2EE
Replies: 1
Last Post: 03-10-2006, 06:02 AM
-
By Application Development in forum Microsoft Exchange
Replies: 0
Last Post: 09-24-2003, 07:07 AM