LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   put data from some files into a field (https://www.linuxquestions.org/questions/programming-9/put-data-from-some-files-into-a-field-945980/)

oreka18 05-20-2012 04:30 AM

put data from some files into a field
 
i have 3 files as below:
Code:

[file1][file2][file3]
i want to print 1st,2nd,5th and 10th filed of 1st to 5th lines from each files into a line of an output file, so the result would be:
[output]:
Code:

{line1}(field 1 of line 1 from file 1)(field 2 of line 1 from file 1)(field 5 of line 1 from file 1)(field 10 of line 1 from file 1)...(field 10 of line 5 from file 1)
{line2}(field 1 of line 1 from file 2)(field 2 of line 1 from file 2)(field 5 of line 1 from file 2)(field 10 of line 1 from file 2)...(field 10 of line 5 from file 2)
{line3}(field 1 of line 1 from file 3)(field 2 of line 1 from file 3)(field 5 of line 1 from file 3)(field 10 of line 1 from file 3)...(field 10 of line 5 from file 3)


pan64 05-20-2012 06:43 AM

in perl you can easily open 3 files, split lines into fields and construct a line you need. What have you tried until now?

oreka18 05-20-2012 10:43 AM

Quote:

Originally Posted by pan64 (Post 4683113)
in perl you can easily open 3 files, split lines into fields and construct a line you need. What have you tried until now?

i wrote this code, but the output is not as the same as i wish.
Code:

#!/bin/bash
clear
#put each relocations result[input] to a sprate line in [output].
output=./results/test
awk 'BEGIN {print "lat lon depth RMS Gap SecGap MinDis MedDis MaxDis azMaxHorUnc MaxHorAnc MinHorAnc CovvXX CovvYY CovvZZ "}' >> $output
for i in {1..3}
  do
input=loc/test${i}.*.*.*.*.hyp
awk -F" " '{if($1~"GEOGRAPHIC") print $10,$12,$14; else if($1~"QUALITY") print $9; else if($1~"QML_OriginQuality") print $15,$17,$21,$25,$23; else if($1~"QML_OriginUncertainty") print $9,$7,$5; else if($1~"STATISTICS") print $9,$15,$19}' $input >> $output
 done

[output]:
Code:

lat lon depth RMS Gap SecGap MinDis MedDis MaxDis azMaxHorUnc MaxHorAnc MinHorAnc CovvXX CovvYY CovvZZ
34.5044 47.3051 16.0312
0.0650051
23.7003 1.52776 15.0473
102.148 160.055 41.1729 55.6071 86.7106
100.879 7.51443 1.24819
34.5031 47.3034 2.3125
0.0766044
12.1645 18.5632 61.0134
102.528 160.323 41.1128 55.6446 86.596
36.9632 7.86856 2.95961
34.5003 47.2999 0.125
0.0894073
32.613 44.6653 98.348
103.292 160.857 40.9959 55.7222 86.3679
38.1985 12.1573 5.47167


Nominal Animal 05-20-2012 02:43 PM

Assuming you mean {line1} .. {line3} literally, then
Code:

awk '(FNR==1) { if (files > 0) printf("\n");
                printf("{line%d}", ++files)  }
    (FNR<=5) { printf(" %s %s %s %s", $1, $2, $5, $10) }
    END      { if (files > 0) printf("\n") }
    ' file1 file2 file3 > output-file

but if you only want the fields, then
Code:

awk '(FNR==1) { if (files++>0) SP="\n" }
    (FNR<=5) { printf("%s%s %s %s %s", SP, $1, $2, $5, $10); SP=" " }
    END      { if (files > 0) printf("\n") }
    ' file1 file2 file3 > output-file

FNR restarts from one for each new input file. The (FNR==1) rule is only applied to first line of each input file, and it starts the output for a new output line. (In the latter awk snippet, SP will be by default empty for the first line of the first file, but a newline for the first line of any other files.)

The (FNR<=5) rule is applied to first five lines of each input file. It outputs the first, second, fifth and tenth fields of the input line, but without a newline. (The latter one prepends no separator for the first line of first input file, a newline for the first line of any other input files, and a space for the second to fifth input lines of all input files.)

Since the last output line does not get a newline, we add one in the END rule, but only if there have been input lines.

Note that you can specify any number of files. If you don't specify any, then standard input is used (as the only file).


All times are GMT -5. The time now is 05:48 PM.