prints some fields from different files into a line of new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting prints some fields from different files into a line of new file
# 1  
Old 05-20-2012
prints some fields from different files into a line of new file

i have 3 files as below:
Code:
[test1][test2][test3]


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)

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

the output is a file that each lines of that contains the values of each line from source files in septate lines, i want to put the values related to each file into a separate line
# 2  
Old 05-20-2012
This shoudl doe what you ask. From your existing code, the requirement is more complex than what you've asked for her, so I'll explain how this works so you can apply to what you have.

Code:
awk 'BEGIN { print "lat lon depth RMS Gap SecGap MinDis MedDis MaxDis azMaxHorUnc MaxHorAnc MinHorAnc CovvXX CovvYY CovvZZ"}
    FNR<6 { if(FNR!=1) printf " "; printf "%s", $1" "$2" "$5" "$10 }
    FNR==5 { printf "\n" }' test1 test2 test3

Firstly the above code reads all three files in 1 call to awk.
BEGIN executes before the 3 files are read and END after all three.
FNR is the row number in the individual file (NR is the total rows read so far).
printf allows strings withouta <CR> to be output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk prints only last line

data.txt: NEWTEXTS="frq=63,std=-0.00533584,time=Mar-21-(09:15:03)-2016,epoch=1458576903,avg=64.2059,212.698 frq=197,std=0.587585,time=Mar-21-(09:16:02)-2016,epoch=1458576962,avg=64.2059,483.756 frq=178,std=0.503514,time=Mar-21-(09:46:02)-2016,epoch=1458578762,avg=64.2059,500... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

6. Shell Programming and Scripting

Compare fields in two files line by line

I am new to awk scripting. I want to do a field by word (field) comparison of two files File1.txt and File2.txt. The files contain a list of | (pipe) separated field. **File 1: ------------------- aaa|bbb|ccc|eee|fff lll|mmm|nnn|ooo|ppp rrr|sss|ttt|uuu|vvv** File 2: ... (7 Replies)
Discussion started by: dhruvmohan
7 Replies

7. Shell Programming and Scripting

Script to compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

8. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

9. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

10. Shell Programming and Scripting

How to create a CSV File by reading fields from separate files

SHELL SCRIPT Hi, I have 3 separate files within a folder. Every File contains data in a single column like File1 contains data mayank sushant dheeraj File2 contains DSA_AT MG_AT FLAT_09 File3 contains data 123123 232323 (2 Replies)
Discussion started by: mayanksargoch
2 Replies
Login or Register to Ask a Question