Visit Our UNIX and Linux User Community


help with awk for file combination


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with awk for file combination
# 1  
Old 04-26-2011
help with awk for file combination

1)file1:
Code:
|  *Local Communication Bandwidths (MB/Sec)  | Memory copy (bcopy) |
|  ^  | mmap_bandwidth |
|  ^  | mmap_read bandwidth |       
|  ^  | memory write bandwidth |
|  Local Communication Latencies |  Pipe Latency |

2)file2

Code:
        
        422.6903
        1948.9000
        627.6130
        1029.1347
        10.6010

3)file3

Code:
        394.1523
        1880.2333
        598.7320
        1122.3873
        10.7973

I want to get file like below
Code:
   
| *Local Communication Bandwidths (MB/Sec)<br>(Bigger is better) | Memory copy (bcopy) | 422.6903 | 394.1523 | native | 6.75% |
| ^ | mmap_bandwidth | 1948.9000 | 1880.2333 | native | 3.52% |
| ^ | mmap_read bandwidth | 627.6130 | 598.7320 | native | 4.60% |
| ^ | memory write bandwidth | 1029.1347 | 1122.3873 | no_native | -9.06% |
| Local Communication Latencies (us)<br>(Smaller is better) | Pipe Latency | 10.6010 | 10.7973 | no_native | -1.85% |

NOTE: it is deived by '|' , I give the rule here, for example the last row formula is (422.6903 - 394.1523)/422.6903 (percent), and if 422.6903 > 394.1523 then then it marked as "native" or else it is marked as "no_native"
# 2  
Old 04-26-2011
nawk -f ya.awk file2.txt file3.txt file1.txt
ya.awk:
Code:
BEGIN {
  OFS=" | "
}
FILENAME == ARGV[1] {
  f1[FNR]=$NF
  next
}
FILENAME == ARGV[2] {
  f2[FNR]=$NF
  next
}
FILENAME == ARGV[3] {
  printf("%s %s%s%s%s%s%s%.2f%\n", $0, f1[FNR],OFS, f2[FNR], OFS,(f1[FNR] > f2[FNR])?"native":"no_native", OFS,((f1[FNR] - f2[FNR])/f1[FNR])*100)
}

# 3  
Old 04-26-2011
Quote:
Originally Posted by vgersh99
nawk -f ya.awk file2.txt file3.txt file1.txt
ya.awk:
Code:
BEGIN {
  OFS=" | "
}
FILENAME == ARGV[1] {
  f1[FNR]=$NF
  next
}
FILENAME == ARGV[2] {
  f2[FNR]=$NF
  next
}
FILENAME == ARGV[3] {
  printf("%s %s%s%s%s%s%s%.2f%\n", $0, f1[FNR],OFS, f2[FNR], OFS,(f1[FNR] > f2[FNR])?"native":"no_native", OFS,((f1[FNR] - f2[FNR])/f1[FNR])*100)
}


Thanks. but it miss the last " |"
Code:
|  *Local Communication Bandwidths (MB/Sec)  | Memory copy (bcopy) | 422.6903 | 394.1523 | native | 6.75%
|  ^  | mmap_bandwidth | 1948.9000 | 1880.2333 | native | 3.52%
|  ^  | mmap_read bandwidth |        627.6130 | 598.7320 | native | 4.60%
|  ^  | memory write bandwidth | 1029.1347 | 1122.3873 | no_native | -9.06%
|  Local Communication Latencies |  Pipe Latency | 10.6010 | 10.7973 | no_native | -1.85%


I want it looks like

Code:
|  *Local Communication Bandwidths (MB/Sec)  | Memory copy (bcopy) | 422.6903 | 394.1523 | native | 6.75% |
|  ^  | mmap_bandwidth | 1948.9000 | 1880.2333 | native | 3.52% |
|  ^  | mmap_read bandwidth |        627.6130 | 598.7320 | native | 4.60% |
|  ^  | memory write bandwidth | 1029.1347 | 1122.3873 | no_native | -9.06% |
|  Local Communication Latencies |  Pipe Latency | 10.6010 | 10.7973 | no_native | -1.85% |

# 4  
Old 04-26-2011
Code:
printf("%s %s%s%s%s%s%s%.2f%%s\n", $0, f1[FNR],OFS, f2[FNR], OFS,(f1[FNR] > f2[FNR])?"native":"no_native", OFS,((f1[FNR] - f2[FNR])/f1[FNR])*100, OFS)

# 5  
Old 04-26-2011
Another approach:
Code:
awk '
FILENAME=="file2"{$1=$1;a[FNR]=$0;next}
FILENAME=="file3"{$1=$1;b[FNR]=$0;next}
{printf("%s %0.4f | %0.4f | %s | %0.2f% |\n", $0, a[++k], b[k],a[k]-b[k]>0?"native":"no_native" ,(a[k]-b[k])/a[k]*100)}
' file2 file3 file1

# 6  
Old 04-26-2011
Quote:
Originally Posted by vgersh99
Code:
printf("%s %s%s%s%s%s%s%.2f%%s\n", $0, f1[FNR],OFS, f2[FNR], OFS,(f1[FNR] > f2[FNR])?"native":"no_native", OFS,((f1[FNR] - f2[FNR])/f1[FNR])*100, OFS)

Thanks again. but I got:

Code:
|  *Local Communication Bandwidths (MB/Sec)  | Memory copy (bcopy) | 422.6903 | 394.1523 | native | 6.75%s
|  ^  | mmap_bandwidth | 1948.9000 | 1880.2333 | native | 3.52%s
|  ^  | mmap_read bandwidth |        627.6130 | 598.7320 | native | 4.60%s
|  ^  | memory write bandwidth | 1029.1347 | 1122.3873 | no_native | -9.06%s
|  Local Communication Latencies |  Pipe Latency | 10.6010 | 10.7973 | no_native | -1.85%s

if I want to add a row for file4 eg:

file4:
Code:
abc fef
ded blash
....
qq
fdfdfdsfsfsdf fdfds

to get
Code:
 
| *Local Communication Bandwidths (MB/Sec) | Memory copy (bcopy) | 422.6903 | 394.1523 | native | 6.75% | abc fef |
| ^ | mmap_bandwidth | 1948.9000 | 1880.2333 | native | 3.52% | ded blash |
| ^ | mmap_read bandwidth | 627.6130 | 598.7320 | native | 4.60% | .... |
| ^ | memory write bandwidth | 1029.1347 | 1122.3873 | no_native | -9.06 | qq |
| Local Communication Latencies | Pipe Latency | 10.6010 | 10.7973 | no_native | -1.85 | fdfdfdsfsfsdf fdfds |

how could do it?
# 7  
Old 04-26-2011
sorry again.....
Code:
BEGIN {
  OFS=" | "
  pct=sprintf("%c", 037)
}
FILENAME == ARGV[1] {
  f1[FNR]=$NF
  next
}
FILENAME == ARGV[2] {
  f2[FNR]=$NF
  next
}
FILENAME == ARGV[3] {
  printf("%s %s%s%s%s%s%s%.2f%c%s\n", $0, f1[FNR],OFS, f2[FNR], OFS,(f1[FNR] > f2[FNR])?"native":"no_native", OFS,((f1[FNR] - f2[FNR])/f1[FNR])*100, pct, OFS)
}


Previous Thread | Next Thread
Test Your Knowledge in Computers #535
Difficulty: Medium
In C, the Boolean type false is associated with zero while any non-zero value is associated with true.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added and the total placed in a new-header section. Also the total should should be rounded or cut to a two decimal anynumber.XX format with the AB string added on the end. For example: The numerical lines from headers 2 and 3 are... (3 Replies)
Discussion started by: jessandr
3 Replies

2. UNIX for Dummies Questions & Answers

Display latest record from file based on multiple columns combination

I have requirement to print latest record from file based on multiple columns combination. EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/16/2015 100004 03/16/2015 100005 001 EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/18/2015 140003 03/18/2015 140004 001 EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/18/2015 220006... (1 Reply)
Discussion started by: tmalik79
1 Replies

3. Shell Programming and Scripting

Help with parsing file with combination of pattern

I have a file1 like prt1|als28.1 prt3|als53.1 prt2|als550.1 prt1|bls9.2 prt2|als7.2 prt2|bls0.2 prt2|als872.1 prt1|bls871.1 prt2|als6.2 prt4|als22.1 prt2|bls43.2 I want to create a file2 from this file by comparing all the possible combinations of patterns (prt) assuming prt1... (3 Replies)
Discussion started by: sammy777
3 Replies

4. Shell Programming and Scripting

Considerable trouble with for loop in combination with awk

I have the text file where each line has the format: chr10 101418889 101418904 0.816327 Right now the interval between column 2 and 3 is 15. I only want the two consecutive positions starting at position 1, write it to a file, then move up one position write to file etc. So that: ... (1 Reply)
Discussion started by: jfern
1 Replies

5. Linux

Berkeley version bdf: combination of inode and file system usage

hello, i need a command like "bdf" of HP-UX. ( report number of free disk blocks (Berkeley version) ). it should report inode and file system usage in one line like bdf -i manpage : Man Page for bdf (all Section 1m) - The UNIX and Linux Forums HP-UX command "" report inode and file... (2 Replies)
Discussion started by: bora99
2 Replies

6. Shell Programming and Scripting

Combination awk and rename

Hello, i wondering if anybody can help me with the following: doing a search for the words "Sample ID:" in a file called test.txt and I need to have the string after this (Postmatch) Then I want that the string is used to rename an other file called test.pdf into .... the string.pdf I... (8 Replies)
Discussion started by: pcdok
8 Replies

7. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

8. Shell Programming and Scripting

Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead. SO, here is my desired outcome. I have some Cisco... (5 Replies)
Discussion started by: abbzer0
5 Replies

9. UNIX for Dummies Questions & Answers

shell help - file combination

Dear all, I have a question about merging multiple files to one. For example, I have 4 files, named file_1, file_2, file_3 and file_4, they all have same line number, and only one word in each line. I want to combine these four files to one file, file_1 becomes the first column of the new... (4 Replies)
Discussion started by: ting123
4 Replies

10. UNIX for Dummies Questions & Answers

awk and file combination

Hi there, I have 3 files and i want to take different fields from each file and combine them in one. I would like to ask if somebody tell me how can I refer to each field of the different files to write an awk command. I mean can I do sth like awk '........... print $1.file1 $3.file2}'... (1 Reply)
Discussion started by: sickboy
1 Replies

Featured Tech Videos