[awk] split file1 and save it as var from file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] split file1 and save it as var from file2
# 1  
Old 07-24-2012
[awk] split file1 and save it as var from file2

I have 2 files:

file_1:
Quote:
Head;track1;track2;track3;
date1;vol2;vol4;vol9;
date2;vol5;vol0;vol1;
date3;vol7;vol2;vol5;
file_2:
Quote:
Head1
artV1 track1
artV2 track2
artV3 track3
expected result:
name file:
"artV1"
Quote:
Head;track1;
date1;vol2;
date2;vol5;
date3;vol7;
"artV2"
Quote:
Head;track2;
date1;vol4;
date2;vol0;
date3;vol2;
etc.

I have:
Quote:
FS=";";
ORS=";\n";
OFS = ";";
k=0;

while (getline < file_1 == 1) {

if (k==0) {
split($0,tmpp);
hm=NF-1;
}
k++;

for (j=2; j <= hm; j++) {

while (getline z < file_2 == 1) {
split(z,tmpq," ");

if (tmpp[j]==tmpq[2]) {
out=tmpq[1];
}
}
print $1,$j > out;
}
}
but why don;t work save to file 'out'??


when use this, it's look great but file name is wrong; is track1 not artV1 Smilie
Quote:
FS=";";
ORS=";\n";
OFS = ";";
k=0;

while (getline < file_1 == 1) {

if (k==0) {
split($0,tmpp);
hm=NF-1;
}
k++;

for (j=2; j <= hm; j++) {
print $1,$j > tmpp[j];
}
}
# 2  
Old 07-24-2012
Try this:
Code:
awk 'FNR==NR{if(FNR>1) fil[$2]=$1;next} 
{
 for(i=2;i<NF;i++)
 {
  if(FNR==1){filepos[i]=fil[$i]}
  print $1 FS $i FS > filepos[i]
 }
}' file2 FS=";" file1

I've assumed that the maximum number of files required to be generated is within the max. open files limit for your awk implementation (should not be a big concern!).

I've also assumed that file2 has all the possible values occurring in record 1 of file1.

Last edited by elixir_sinari; 07-24-2012 at 11:13 AM..
# 3  
Old 07-24-2012
Ok it's work, but how it us in awk file:
this don't work:
Quote:
BEGIN{
}

{
file2="/tmp/file2";
file1="/tmp/file1";

while((getline < file2) == 1){
if (FNR==NR) {
if(FNR>1) {
fil[$2]=$1;
}
}
}

FS=";";
while((getline < file2) == 1){
for(i=2; i<NF; i++) {
if(FNR==1) {
filepos[i]=fil[$i];
}
print $1 FS $i FS > filepos[i];
}

}
}

END{
}
# 4  
Old 07-25-2012
It's a bit tedious job to do it the way you want it done. Smilie Can't use FNR/NR in this case because getline will not set these variables.
Code:
cat split.awk

BEGIN {
while(getline < "fil2")
 if(NF==2)
  fil[$2]=$1
close("fil2")
FS=";"
while(getline < "fil1")
{
 if($0 ~ /^Head/)
  for(i=2;i<NF;i++)
   filepos[i]=fil[$i]
 if($0 ~ /^date/)
  for(i=2;i<NF;i++)
   print $1 FS $i FS > filepos[i]
}
}


Last edited by elixir_sinari; 07-25-2012 at 04:16 AM..
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Reading and appending a row from file1 to file2 using awk or sed

Hi, I wanted to add each row of file2.txt to entire length of file1.txt given the sample data below and save it as new file. Any idea how to efficiently do it. Thank you for any help. input file file1.txt file2.txt 140 30 200006 141 32 140 32 200006 142 33 140 35 200006 142... (5 Replies)
Discussion started by: ida1215
5 Replies

5. Shell Programming and Scripting

Looking for lines, which is present in file1 but not in file2 using UNIX and awk

I have 2 files with 7 fields and i want to print the lines which is present in file1 but not in file2 based on field1 and field2. Logic: I want to print all the lines, where there is a particular column1 and column2. And we do not find the set of column1 and column2 in file2. Example: "sc2/10... (3 Replies)
Discussion started by: NamS
3 Replies

6. Shell Programming and Scripting

How to get unique of file1 from file2 and save the output?

Please help, file1.txt 1 2 3 4 5 file2.txt 3 4 5 6 7 All I need is this result.txt 1 2 (17 Replies)
Discussion started by: richmac
17 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

9. Shell Programming and Scripting

Awk Compare File1 File2 on f2

I'm trying to compare two files using AWK, where if field2 of both files match, replace field1 of file1 with field1 of file2 and if there is no match just print the line of file1. file1.txt (has empty first field) :ABBATOM:B:H:1992 :ABBA TROJAN:B:H:1993 :ABBES FIRST HOPE:B:M:1997 :ABBEYS... (4 Replies)
Discussion started by: RacerX
4 Replies

10. Shell Programming and Scripting

Awk Compare f1,f2,f3 of File1 with f1 of File2

I have an Awk string-compare problem and have searched the internet and forums for a solution i could use but cannot find a solution i understand to make work with my particular problem: I need to compare (field1 field2 field3 of File1) against (field1 of File2) and if they match print out... (6 Replies)
Discussion started by: RacerX
6 Replies
Login or Register to Ask a Question