Conversion of line via awk or etc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conversion of line via awk or etc
# 1  
Old 12-22-2012
Conversion of line via awk or etc

Hello friends, could you help me about problem with my data lines. I suppose a simple awk code may help me.

I have following data lines: (first line including 3 numbers and then a matrices of 4x10)

Code:
           500  40  9
 1  A  B   4   5  6   7   8   9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 Z P R



How can I convert this format to

first line sum of 3 numbers at the first line ---> 549
second line is converted to 1x40 ----> 1 A B 4 5 6 7 8 9 ..... Z P R



Could anyone help ?

thanks
# 2  
Old 12-22-2012
Code:
awk ' BEGIN { sum=0; }
 NR==1 { for(i=1;i<=NF;i++) { sum+=$i; } print sum; }
 NR>1 { for(i=1;i<=NF;i++) printf "%s ", $i }
 END { printf "\n";
} ' filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-22-2012
thank you very much bipinajith ,

what if I have multiple as follows, is it possible to do each of them seperately

Code:
            5  2  3
 -12.108 -11.789  -5.196  -4.370  -3.344  -2.229  -1.465  -0.347  -0.325   2.456
   2.855   4.105   4.569   5.902   7.791   7.950   8.750   9.383  10.041  10.160
  10.755  11.441  11.827  12.302  12.847  13.816  15.849  15.904  16.213  16.383
  17.158  17.705  18.715  19.221  19.330  19.411  19.696  20.122  20.142  20.417
            7  4  8
 -12.118 -11.796  -5.181  -4.356  -3.345  -2.225  -1.463  -0.351  -0.334   2.454
   2.842   4.093   4.566   5.937   7.785   7.924   8.741   9.360  10.046  10.129
  10.845  11.461  11.850  12.233  12.942  13.811  15.853  15.860  16.200  16.399
  17.192  17.707  18.708  19.149  19.302  19.381  19.664  20.017  20.089  20.375


I mean

Code:
            11
 -12.108 -11.789  -5.196  -4.370  -3.344  -2.229  -1.465  -0.347  -0.325   2.456  2.855   ....
            19
 -12.118 -11.796  -5.181  -4.356  -3.345  -2.225  -1.463  -0.351  -0.334   2.454  2.842  ...

is it possible ?

Last edited by Scott; 12-22-2012 at 11:34 AM.. Reason: Code tags
# 4  
Old 12-22-2012
Assuming every new set starts with 3 numbers:-
Code:
awk ' BEGIN { sum=0; }
 NF==3 { for(i=1;i<=NF;i++) { sum+=$i; } if(NR==1) printf "%s\n", sum; else printf "\n%s\n", sum; sum=0;}
 NF>3 { for(i=1;i<=NF;i++)  { printf "%s ", $i; } ; }
 END { printf "\n";
} ' filename

This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-22-2012
Thank you very much... It works perfect.

One last question. If you don"t have time forget it

instead of simple sum of 3 numbers i.e. a+b+c, is it possible to get Sqrt(axa+bxb+cxc) with this code ?

which part should I change ?


thank you for your time
# 6  
Old 12-22-2012
You have to change 2nd line to:
Code:
 NF==3 { for(i=1;i<=NF;i++) { sum+=($i*$i); } if(NR==1) printf "%s\n", sqrt(sum); else printf "\n%s\n", sqrt(sum); sum=0;}

This User Gave Thanks to Yoda For This Post:
# 7  
Old 12-22-2012
thanks bipinajith
It works perfect...

thank you for your time
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Base64 conversion in awk overlaps

hi, problem: output is not consistent as expected using external command in AWK description: I'm trying to convert $2 into a base64 string for later decoding, and for this when I use awk , I'm getting overlapped results , or say it results are not 100% correct. my code is: gawk... (9 Replies)
Discussion started by: busyboy
9 Replies

2. Shell Programming and Scripting

Numeral conversion in awk

I am running into conversion of string to numbers in awk that I can't understand and don't know how to deal with properly My script checks for numeric only field, I use this approach to do that: $1 + 0 == $1 It works most of the time, but in some cases it does not behave how I expect it to... (5 Replies)
Discussion started by: migurus
5 Replies

3. Shell Programming and Scripting

awk string to number conversion

Can someone explain whats happening here: $ awk 'BEGIN {print (2.5 - 1)}' 1,5 2.5 - 1 is correctly calculated to 1,5 (using european locale) $ echo "2.5" | awk '{temp = $1 - 1; print temp}' 1 If i now pipe the string 2.5 through awk it seems at it truncates 2.5 to 2? What's the... (4 Replies)
Discussion started by: beow
4 Replies

4. Shell Programming and Scripting

Wiki conversion with Awk or Sed

I have the words in twiki format that I want to convert to mediawiki format like below : %BLUE%some words1%ENDCOLOR% bla bla blab labdad sdadasd adsasdads oerdkfj kdfjs %PINK%some wordks2 123.4.5.6/26%ENDCOLOR%, ksdjak dkasjd kjfrjkfgjdkfgjdfkgjdgdfgdgf %PURPLE%1.2.3.4/28%ENDCOLOR%, dskd... (3 Replies)
Discussion started by: rk4k
3 Replies

5. AIX

function trace back and address to line number conversion

Hi, I am new to AIX and I am developing a small tool for our product which helps debug memory leaks etc. Q1)Is there a way in which i can get a function trace back as to the call (lets say malloc() )has been made in which file--> in which function. I tried using the #pragma options (... (0 Replies)
Discussion started by: Wkdunreal
0 Replies

6. Shell Programming and Scripting

HPUX to Linux awk conversion

I have the following script to edit ^'s and newlines out of files to be entered into a database. This script has been around since the dawn of time (way before me). #!/bin/bash # Remove all ^ and \n from the source file, except do not remove ^^^\n cat myfile.hold | awk ' BEGIN {FS="|";... (1 Reply)
Discussion started by: insania
1 Replies

7. Shell Programming and Scripting

File conversion and awk

Hi Everyone, I am confused with the output of the input file and I am using below command in script to get the expected output. Also I want to add another condition using logical AND (&&) in place of $2=="L"{$4=0-$4} as $2=="L" && $3=="L" {$4=0-$4} but I am getting some awk error. Can someone... (6 Replies)
Discussion started by: gehlnar
6 Replies

8. Shell Programming and Scripting

AWK Currency Conversion

How can I use awk command to convert values to currency. For example I have a database like follows John:200 smith:300 kim:405 and want it to out put like this John $200.00 (3 Replies)
Discussion started by: 3junior
3 Replies

9. Shell Programming and Scripting

awk script for date conversion

hi awk script for dd/mm/yyyy to yyyymmdd awk script for dd-mon-yyyy to yyyymmdd awk script for dd-mm-yyyy to yyyymmdd formate ..............urgent............. Thanks in advanced (2 Replies)
Discussion started by: charandevu
2 Replies

10. Shell Programming and Scripting

String Conversion in awk

I am porting a awk script from Windows to unix I_SALE_MEDIA=$67 if ((I_VOID_FLAG == "Y") && (I_SALE_MEDIA == 0)) NOW consider the case where I_SALE_MEDIA i.e $67 is "000" The above comparison works fine in Windows , but to make it work in Unix , I had to change the above as follows : ... (3 Replies)
Discussion started by: rohanrege
3 Replies
Login or Register to Ask a Question