LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2012, 04:30 AM   #1
oreka18
LQ Newbie
 
Registered: May 2012
Posts: 7

Rep: Reputation: Disabled
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)

Last edited by oreka18; 05-20-2012 at 04:36 AM.
 
Old 05-20-2012, 06:43 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
in perl you can easily open 3 files, split lines into fields and construct a line you need. What have you tried until now?
 
Old 05-20-2012, 10:43 AM   #3
oreka18
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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
 
Old 05-20-2012, 02:43 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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).
 
1 members found this post helpful.
  


Reply



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
Can I execute a shell command and put the result in command field? fran4tw Linux - General 9 08-08-2011 08:27 PM
bash script to sort data by field lothario Linux - Newbie 4 08-26-2009 02:23 AM
Label merge prints field names, not data RetiredInMaine Linux - Desktop 2 09-30-2006 12:21 PM
What is the data type field definition to save RTF file? Linux4BC Linux - General 3 06-02-2004 04:19 AM
Reading data from file (field organizzation) eiem Programming 1 03-29-2004 05:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 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
Open Source Consulting | Domain Registration