File Join with different format using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Join with different format using awk
# 1  
Old 06-01-2009
File Join with different format using awk

File Join with different format using awk

file1
Quote:
set1
set2
set3
set5
file2
Quote:
ab=set1
item1
item2
ab=set2
item3
item4
ab=set3
ab=set4
item5
item6
ab=set5
item7
item8

Code:
nawk -F"=" 'NR==FNR{a[$1]++;next} a[$2]{ print a[$2],


output

Quote:
set1 item1
set1 item2
set2 item3
set2 item4
set5 item7
set5 item8

Help is appreciated
# 2  
Old 06-01-2009
where is the rest of your nawk code?
# 3  
Old 06-01-2009
Quote:
Originally Posted by ghostdog74
where is the rest of your nawk code?
That all he has

Last edited by pinnacle; 06-01-2009 at 11:01 PM..
# 4  
Old 06-01-2009
if that's all he has, then i bet he's not even testing his code.
# 5  
Old 06-01-2009
hope below perl can help you some

Code:
my %hash=(set1=>1,set2=>1,set3=>1,set5=>1);
my $name;
while(<DATA>){
if(/.*=(.*)/){
  if (exists($hash{$1})){
    $name=$1;
    next;
  }
  else{
    undef $name;
    next;
  }
}
else{
  if($name){
    print $name," ",$_;
  }
}
}
__DATA__
ab=set1
item1
item2
ab=set2
item3
item4
ab=set3
ab=set4
item5
item6
ab=set5
item7
item8

awk:
Code:
nawk -F"=" '{
if(NR==FNR)
	a[$1]=1
else{
	if(NF==2){
		if(a[$2]==1)
			tmp=$2
		else
			tmp=""
	}
	else{
		if(tmp!="")
			printf("%s %s\n",tmp, $1)
	}
}
}' file1 file2


Last edited by summer_cherry; 06-04-2009 at 12:39 AM..
# 6  
Old 06-03-2009
Quote:
Originally Posted by summer_cherry
hope below perl can help you some

Code:
my %hash=(set1=>1,set2=>1,set3=>1,set5=>1);
my $name;
while(<DATA>){
if(/.*=(.*)/){
  if (exists($hash{$1})){
    $name=$1;
    next;
  }
  else{
    undef $name;
    next;
  }
}
else{
  if($name){
    print $name," ",$_;
  }
}
}
__DATA__
ab=set1
item1
item2
ab=set2
item3
item4
ab=set3
ab=set4
item5
item6
ab=set5
item7
item8

I want to implement using ksh(awk or sed) and would like to avoid perl

The output i am looking for is:
Quote:
output
set1 item1
set1 item2
set2 item3
set2 item4
set5 item7
set5 item8
The code i wrote:
Code:
nawk -F"=" 'NR==FNR{a[$1]++;next} {if(a[$2]) $2,getline,getline}' file1 file2


Help is appreciated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. Shell Programming and Scripting

Join file with awk

I have a problem of joining 2 files with different position of key column file1 INDIA|LEFT|123 USA|RIGHT|345 CHINA|LEFT|234 file2 123|HIGH 345|LOW expected output INDIA|LEFT|123|HIGH USA|RIGHT|345|LOW CHINA|LEFT|234| based on column 3 in file 1 and column 2 in file 2, I want to... (2 Replies)
Discussion started by: radius
2 Replies

3. Shell Programming and Scripting

Format a file using awk

I have a file as below empty May7 noread May8 How can process this file and print as below. neat and clean. Filename Date Created empty May7 noread May8 I tried something with printf before creating the file itself... (3 Replies)
Discussion started by: Tuxidow
3 Replies

4. Shell Programming and Scripting

Changing format of file with awk

Hi all, I have a file that looks like this: Closest words to: manifesto >>>> Closest words to: passport >>>> and I want to reformat this with awk with the following desired result: manifesto 0.99999999999999978, 'manifesto' 0.72008211381623111, 'communiqu\xe9'... (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Need help on awk script to format a file

Hi everyone, Looking for some help in awk script to modify the format of a file. I seem to be struggling with this: Input: ***************************************************** ,,,,TOTAL,2014/1-,2014/2- 0,TOTAL,BLANK,"Description",Total 1,Total 2,Total 3 1, MFG Name 1,,"Description",MFG... (4 Replies)
Discussion started by: yogesh_jain
4 Replies

6. Shell Programming and Scripting

awk to format file

Hello, I shall like using the function awk to modify the contents of the following file: /tmp/conf-1 -sec=sys,rw=lpar1:lpar2:lpar3,access=lpar1:lpar2:lpar3 /tmp/conf-2 -vers=4,sec=sys,rw=lpar4:lpar5:lpar6,access=lpar4:lpar5:lpar6 I need to have the result below towards another file ... (5 Replies)
Discussion started by: khalidou13
5 Replies

7. Shell Programming and Scripting

Need awk/sed to format a file

My content of source file is as below scr1 a1 scr2 a2 b2 scr3 a3 b3 c3 I need a awk/sed command (to be used in C shell)to format it to something like below scr1 $a1 >file1 scr2 $a2 $b2 >file2 scr3 $a3 $b3 $c3 >file3 (12 Replies)
Discussion started by: animesharma
12 Replies

8. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

9. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question