LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2013, 01:44 PM   #16
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976

You can slightly modify the code I suggested above: since it prints out only the averaged line, you can add a print statement before getline and a print statement at the end. Moreover you have to change the value of the first field to put the correct sequence of line numbers:
Code:
#!/usr/bin/awk -f
BEGIN { OFS = "\t" }
NR % 2 {
  for (i=3;i<=NF;i++)
    _[i]=$i
  $1=++c
  print
  getline
  printf "%d\t%d", ++c, $2
  for (i=3;i<=NF;i++)
    printf "\t%.2f", ($i+_[i])/2
  print ""
  $1=++c
  print
}
Actually from a file of 100 lines you will get 150 lines, since you compute the average every two lines (if I understand well). Hope this helps.
 
Old 10-15-2013, 03:04 PM   #17
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
This is a brute-force solution. The output is not pretty.

With this InFile ...
Code:
1       42      0.0     0.4     0.3     0.8     0.7
2       42      0.3     0.1     0.2     0.4     0.1
3       42      0.0     0.1     0.0     0.2     0.4
4       42      0.7     0.1     0.0     0.0     0.8
5       42      0.3     0.2     0.3     0.8     0.1
... this code ...
Code:
awk '{for (i=1;i<=NF;i++) arr[i]=arr[i]" "$i} 
  END {i=1; while (i in arr) {print arr[i]; i++;}}' $InFile \
|awk '{for (j=1;j<=NF-1;j++) {k=j+1;printf $j" "($j+$k)/2" "$k" "}; print "\n"}'  \
|awk '{for (i=1;i<=NF;i++) arr[i]=arr[i]" "$i} 
  END {i=1; while (i in arr) {print arr[i]; i++;}}'  \
|awk '!a[$1]++' >$OutFile
... produced this OutFile ...
Code:
 1 42 0.0 0.4 0.3 0.8 0.7
 1.5 42 0.15 0.25 0.25 0.6 0.4
 2 42 0.3 0.1 0.2 0.4 0.1
 2.5 42 0.15 0.1 0.1 0.3 0.25
 3 42 0.0 0.1 0.0 0.2 0.4
 3.5 42 0.35 0.1 0 0.1 0.6
 4 42 0.7 0.1 0.0 0.0 0.8
 4.5 42 0.5 0.15 0.15 0.4 0.45
 5 42 0.3 0.2 0.3 0.8 0.1
Daniel B. Martin
 
Old 10-15-2013, 03:31 PM   #18
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by colucix View Post
Actually from a file of 100 lines you will get 150 lines, since you compute the average every two lines (if I understand well). Hope this helps.
oh yea 150 not 198, that's embarrassing
 
Old 10-15-2013, 04:07 PM   #19
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Quote:
Originally Posted by tabbygirl1990 View Post
oh yea 150 not 198, that's embarrassing
My solution (post #17) averages values in adjacent lines. Hence an input file with 100 lines results in an output file with 199 lines.

Daniel B. Martin
 
Old 10-15-2013, 04:14 PM   #20
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by danielbmartin View Post
My solution (post #17) averages values in adjacent lines. Hence an input file with 100 lines results in an output file with 199 lines.

Daniel B. Martin
Maybe you're right, Daniel. I was confused from the requirement in post #6 where the lines were averaged in pairs and the requirement explained in post #15 where it seems that adjacent lines should be averaged in sequence.

What exactly is the output you expect from the example in post #15, tabby?
 
Old 10-15-2013, 04:25 PM   #21
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Here is another solution to average every two adjacent lines (which gives 199 lines, as Daniel clarified):
Code:
BEGIN { OFS = "\t"}

NR == 1 {
  $1=++c
  line=$0
}

NR > 1 {
  print line
  split(line,_)
  printf "%d\t%d", ++c, $2
  for (i=3;i<=NF;i++)
    printf "\t%.2f", ($i+_[i])/2
  print ""
  $1=++c
  line=$0
}

END {
  print line
}
 
1 members found this post helpful.
Old 10-15-2013, 04:48 PM   #22
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
starting to get messy now

Code:
#!/bin/bash
Input="$1"
tick=1
LineNo=1
while read Line;do
    case $tick in
        1)
           X=($Line)
           tick=2
        ;;
        2)
           Y=($Line)
           tick=1
           printf "%s\t%d" ${X[0]} ${X[1]}
           for i in {2..6};do
               awk '{printf "\t%.11f",$1}' <<< "${X[i]}"
           done
           printf '\n'
           printf "%s\t%d" ${Y[0]} ${Y[1]}
           for i in {2..6};do
               awk '{printf "\t%.11f",$1}' <<< "${Y[i]}"
           done
           printf '\n'
           printf "%s" "Avg. of ${X[0]} & ${Y[0]}"
           for i in {2..6};do
               awk '{printf "\t%.11f",($1 + $2)/2}' <<< "${X[i]} ${Y[i]}"
           done
           printf '\n\n'
           LineNo=$(($LineNo+1))
        ;;
    esac
done < $Input
Code:
./Script.sh /path/to/InputFile
Original 'input' ( post 10 ) , gives
Code:
1	42	0.19796486000	0.36209083500	0.35434490900	0.85658287700	0.73567178900
2	42	0.02501695100	0.12691389000	0.21023592500	0.41777332100	0.09168590200
Avg. of 1 & 2	0.11149090550	0.24450236250	0.28229041700	0.63717809900	0.41367884550

3	42	0.61008503800	0.44505031100	0.75656573300	0.18000768500	0.21662871100
4	42	0.45826483200	0.35942381100	0.48894996300	0.07380080200	0.09190244700
Avg. of 3 & 4	0.53417493500	0.40223706100	0.62275784800	0.12690424350	0.15426557900

5	42	0.26852244300	0.64834488900	0.98388615800	0.43634909500	0.94903523500
6	42	0.17626450100	0.05980607500	0.86050950200	0.48814615800	0.24050986100
Avg. of 5 & 6	0.22239347200	0.35407548200	0.92219783000	0.46224762650	0.59477254800

7	42	0.89882842000	0.00434019800	0.95988506100	0.08370775500	0.63690777500
8	42	0.17540739600	0.75294634100	0.03749785800	0.73802708800	0.59901326000
Avg. of 7 & 8	0.53711790800	0.37864326950	0.49869145950	0.41086742150	0.61796051750

9	42	0.92989348600	0.11003698700	0.10994534600	0.78832930300	0.30393201100
10	42	0.78835974200	0.35680380500	0.95455837400	0.93942156000	0.47472270400
Avg. of 9 & 10	0.85912661400	0.23342039600	0.53225186000	0.86387543150	0.38932735750
NewInput
Code:
1	42	0.00000000000	0.40000000000	0.30000000000	0.80000000000	0.70000000000
2	42	0.30000000000	0.10000000000	0.20000000000	0.40000000000	0.10000000000
Avg. of 1 & 2	0.15000000000	0.25000000000	0.25000000000	0.60000000000	0.40000000000

3	42	0.00000000000	0.10000000000	0.00000000000	0.20000000000	0.40000000000
4	42	0.70000000000	0.10000000000	0.00000000000	0.00000000000	0.80000000000
Avg. of 3 & 4	0.35000000000	0.10000000000	0.00000000000	0.10000000000	0.60000000000
change the %.11f to %.2f
Code:
1	42	0.00	0.40	0.30	0.80	0.70
2	42	0.30	0.10	0.20	0.40	0.10
Avg. of 1 & 2	0.15	0.25	0.25	0.60	0.40

3	42	0.00	0.10	0.00	0.20	0.40
4	42	0.70	0.10	0.00	0.00	0.80
Avg. of 3 & 4	0.35	0.10	0.00	0.10	0.60
 
Old 10-15-2013, 07:17 PM   #23
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
This solution is shorter and cleaner than my previous effort.

With this InFile ...
Code:
1       42      0.0     0.4     0.3     0.8     0.7
2       42      0.3     0.1     0.2     0.4     0.1
3       42      0.0     0.1     0.0     0.2     0.4
4       42      0.7     0.1     0.0     0.0     0.8
5       42      0.3     0.2     0.3     0.8     0.1
... this code ...
Code:
awk 'BEGIN{getline;
   for (i=1;i<=NF;i++) {printf "%8.2f",$i; a[i]=$i} print ""};
  {for (j=1;j<=NF;j++) {printf "%8.2f",(a[j]+$j)/2; a[j]=$j}; print "";
  {for (k=1;k<=NF;k++)  printf "%8.2f",a[k]; print ""}}' $InFile >$OutFile
... produced this OutFile ...
Code:
    1.00   42.00    0.00    0.40    0.30    0.80    0.70
    1.50   42.00    0.15    0.25    0.25    0.60    0.40
    2.00   42.00    0.30    0.10    0.20    0.40    0.10
    2.50   42.00    0.15    0.10    0.10    0.30    0.25
    3.00   42.00    0.00    0.10    0.00    0.20    0.40
    3.50   42.00    0.35    0.10    0.00    0.10    0.60
    4.00   42.00    0.70    0.10    0.00    0.00    0.80
    4.50   42.00    0.50    0.15    0.15    0.40    0.45
    5.00   42.00    0.30    0.20    0.30    0.80    0.10
Daniel B. Martin
 
Old 10-15-2013, 07:20 PM   #24
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
with the exception of his 1st column daniel's script gives the needed results, well actually the 2nd column has to be an integer, it's a key to another code read in as a "string" if that makes sense

Code:
1	42	0.0	0.4	0.3	0.8	0.7
2	42	0.15	0.25	0.25	0.6	0.4
3	42	0.3	0.1	0.2	0.4	0.1
4	42	0.15	0.1	0.1	0.3	0.25
5	42	0.0	0.1	0.0	0.2	0.4
6	42	0.35	0.1	0.0	0.1	0.6
7	42	0.7	0.1	0.0	0.0	0.8
8	42	0.5	0.15	0.15	0.4	0.45
9	42	0.3	0.2	0.3	0.8	0.1
thanks daniel

colucix, i can try out your script tomorrow when i get to my linux box, i'm at home on my mac

thanks so much guys!

tabby

Last edited by tabbygirl1990; 10-15-2013 at 07:24 PM.
 
Old 10-15-2013, 07:41 PM   #25
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Polishing the apple...

With this InFile ...
Code:
1       42      0.0     0.4     0.3     0.8     0.7
2       42      0.3     0.1     0.2     0.4     0.1
3       42      0.0     0.1     0.0     0.2     0.4
4       42      0.7     0.1     0.0     0.0     0.8
5       42      0.3     0.2     0.3     0.8     0.1
... this code ...
Code:
awk 'BEGIN{getline;
   for (i=2;i<=NF;i++) {printf "%8.2f",$i; a[i]=$i} print ""};
  {for (j=2;j<=NF;j++) {printf "%8.2f",(a[j]+$j)/2; a[j]=$j}; print "";
  {for (k=2;k<=NF;k++)  printf "%8.2f",a[k]; print ""}}' $InFile  \
|nl |sed 's/.00//' >$OutFile
... produced this OutFile ...
Code:
     1	   42    0.00    0.40    0.30    0.80    0.70
     2	   42    0.15    0.25    0.25    0.60    0.40
     3	   42    0.30    0.10    0.20    0.40    0.10
     4	   42    0.15    0.10    0.10    0.30    0.25
     5	   42    0.00    0.10    0.00    0.20    0.40
     6	   42    0.35    0.10    0.00    0.10    0.60
     7	   42    0.70    0.10    0.00    0.00    0.80
     8	   42    0.50    0.15    0.15    0.40    0.45
     9	   42    0.30    0.20    0.30    0.80    0.10
Daniel B. Martin
 
1 members found this post helpful.
Old 10-15-2013, 07:48 PM   #26
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
that's it daniel, muah

i'll sit down and study it over tomorrow,

tabby
 
Old 10-15-2013, 07:54 PM   #27
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
how do i send a pm to say thank you?
 
Old 10-15-2013, 10:02 PM   #28
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Quote:
Originally Posted by tabbygirl1990 View Post
how do i send a pm to say thank you?
The customary way to express thanks is to click on the YES button just to the right of the legend "Did you find this post helpful?"

If you have more questions of this nature, best to post them on the Programming forum rather than the Newbie forum.

Daniel B. Martin

Last edited by danielbmartin; 10-15-2013 at 10:11 PM.
 
1 members found this post helpful.
Old 10-16-2013, 09:57 AM   #29
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,484

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
The mathematical operation requested by OP is called interpolation.
I mention this here so that future LQ searches on this word will be directed to this thread.

See: http://whatis.techtarget.com/definit...-interpolation

Daniel B. Martin
 
Old 10-16-2013, 12:54 PM   #30
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Original Poster
Rep: Reputation: 1
hi daniel,

i must be somehow running your script wrong? i've tried several things like

Code:
awk -f daniels_script.awk inputfile > outputfile
or specifiying the inputfile and outputfile names directly in the script as you did for InFile and OutFile
buti keep getting back
Code:
awk: daniels_script.awk:1: awk 'BEGIN{getline;
awk: daniels_script.awk:1:     ^invalid char ''' in expression
if i copy and past the script directly into a terminal and execute i get back ambiguous redirect

thanks for your help!

tabby

Last edited by tabbygirl1990; 10-16-2013 at 01:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [Bash] Totalling & Averaging in one go blenderfox Programming 19 07-07-2013 06:32 AM
Averaging columns from multiple files carlr Programming 3 03-18-2012 02:24 AM
alsa - averaging stereo to mono for speaker output bdjnk Linux - General 0 08-18-2009 03:38 PM


All times are GMT -5. The time now is 11:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration