how to arrange 3 file to one using awk...?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to arrange 3 file to one using awk...?
# 1  
Old 01-08-2008
how to arrange 3 file to one using awk...?

I have 3 file, I want to re-arrange so all data easy to read from one file only:

file1 :
userA 10 20
userB 30 40
userC 50 60
....

file2 :
userA var1 1000
userA var2 2000
userB var2 3000
userB var3 4000
userB var4 5000
userC var1 6000
...

file3 :
userA var3 7000
userA var4 8000
userB var1 9000
userB var3 10000
userC var2 11000
...

the 2nd column in file2 and file3 only consist 4 fix string (var1, var2, var3, var4), and each variable only use one time by each user.

I want arrange those 3 file to one file with format :
C=column
F=file

user F1C2 F1C3 F2C3(if C2=var1) F2C3(if C2=var2) F2C3(if C2=var3) F2C3(if C2=var4) F3C3(if C2=var1) F3C3(if C2=var2) F3C3(if C2=var3) F3C3(if C2=var4)

example :
userA 10 20 1000 0 0 0 0 0 7000 8000
userB 30 40 0 3000 4000 5000 9000 0 10000 0
userC 50 60 6000 0 0 0 0 11000 0 0
...

please help me how to solve this problem using awk?

thanks,

best regards,
dens
# 2  
Old 01-08-2008
This looks easier to do in shell - any reason why it has to be awk?
# 3  
Old 01-08-2008
does awk more easy to solve this problem?
please help me if you have another simple technique using shell...
# 4  
Old 01-08-2008
Code:
#!/bin/sh
cat file1 | while read line
do
  username=`echo $line | awk '{ print $1 }'`
  params=`echo $line | awk '{ print $2,$3 }'`
  for file in file2 file3
  do
    egrep "$username " $file | while read line
    do
      echo "0" > var1.$$
      echo "0" > var2.$$
      echo "0" > var3.$$
      echo "0" > var4.$$
      varval=`echo $line | awk '{ print $3 }'`
      case `echo $line | awk '{ print $2 }'` in
      var1) echo $varval > var1.$$
        ;;
      var2) echo $varval > var2.$$
        ;;
      var3) echo $varval > var3.$$
        ;;
      var4) echo $varval > var4.$$
        ;;
      esac
    done
    params="$params `cat var1.$$` `cat var2.$$` `cat var3.$$` `cat var4.$$`"
  done
  echo "$username $params"
done
rm -f var1.$$ var2.$$ var3.$$ var4.$$

It's a bit clumsy - I always have trouble passing variables back out of while loops (thus the var1.$$ temp files) - but it should do the job. It'll even handle the var# entries being out of order in the files Smilie
# 5  
Old 01-08-2008
awk

Hi guy,

Below is my solution. But not very good.

You can refer to can modify it to address you issue. Actually i do not know how to deal all three files at the same time. so i divided to two step, first deal with the file1 and file2 and generate a temp file, then deal with the temp file and file3. The result is ok.

code:
Code:
nawk '
NR==FNR{user[$1]=$0;userv[$1"1"]=0;userv[$1"2"]=0;userv[$1"3"]=0;userv[$1"4"]=0}
NR!=FNR{
if($2=="var1")
	userv[$1"1"]=$3
else if($2=="var2")
	userv[$1"2"]=$3
else if($2=="var3")
	userv[$1"3"]=$3
else if($2=="var4")
	userv[$1"4"]=$3
}
END{
for(i in user)	
	print user[i]" "userv[i"1"]" "userv[i"2"]" "userv[i"3"]" "userv[i"4"]
}
'  file1 file2 > temp

nawk '
NR==FNR{user[$1]=$0;userv[$1"1"]=0;userv[$1"2"]=0;userv[$1"3"]=0;userv[$1"4"]=0}
NR!=FNR{
if($2=="var1")
	userv[$1"1"]=$3
else if($2=="var2")
	userv[$1"2"]=$3
else if($2=="var3")
	userv[$1"3"]=$3
else if($2=="var4")
	userv[$1"4"]=$3
}
END{
for(i in user)	
	print user[i]" "userv[i"1"]" "userv[i"2"]" "userv[i"3"]" "userv[i"4"]
}
'  temp file3
rm temp

# 6  
Old 01-08-2008
Quote:
Originally Posted by Smiling Dragon
[CODE]#!/bin/sh
cat file1 | while read line
...
not that it matters unless there's a performance issue for large files, you can do away with the cat. use file indirection to the while loop instead. cuts down one process Smilie
# 7  
Old 01-09-2008
amazing!! both of your script solving my problem.
thank you summer_cherry, your script execution faster. Smilie

thank you all.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Arrange file by modified date

Hi, Am performing a find based on filename and result can contain multiple files being found Let's say my find command is find /Archive -f -name 12345.pdf and result of find command is /Archive/Folder A/12345.pdf /Archive/Folder B/12345.pdf please note white space in folder names I... (2 Replies)
Discussion started by: gigagigosu
2 Replies

2. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

3. Shell Programming and Scripting

script to arrange file in specific format

Hi All, I am new to forum, I am looking to arrange a file in specific format but unable to get the formula to do it, already googled for the same, but didnt find the answer :(. hope to get help here :o:o:o:o:o I have to files : $ cat Dev_List2 0685 0686 0687 0688 0689 068A 068B 068C... (2 Replies)
Discussion started by: prasan_Aix
2 Replies

4. Shell Programming and Scripting

awk to search similar strings and arrange in a specified pattern

Hi, I'm running a DB query which returns names of people and writes it in a text file as shown below: Carey, Jim; Cena, John Cena, John Sen, Tim; Burt, Terrence Lock, Jessey; Carey, Jim Norris, Chuck; Lee, Bruce Rock, Dwayne; Lee, Bruce I want to use awk and get all the names... (9 Replies)
Discussion started by: prashu_g
9 Replies

5. Shell Programming and Scripting

Arrange / format data using awk

Input 217:fngadi4osa:fngadi4osa:M 217:415744:N/A 227:fngadi4osa:fngadi4osa: M 227:51200:N/A 228:fngadi4osa:fngadi4osa: M 228:102400:N/A 65:sapgt04:sapgt04: M 65:104448:N/A 228:fngadi4osa:fngadi4oma: M 228:102400:N/A Output 217:fngadi4osa:fngadi4osa:M 217:415744:N/A... (3 Replies)
Discussion started by: greycells
3 Replies

6. Shell Programming and Scripting

Re-arrange column

10.142.7.155 - - www.abc.com 404 - I have many columns which is tab delimited file, I have to re-arrange this to a particular column and also add "-" to 3rd column and 6th column. 10.142.7.155 - - - www.abc.com - 404 - (4 Replies)
Discussion started by: sandy1028
4 Replies

7. Shell Programming and Scripting

Arrange log files with AWK

Hello friends, I have too many log files to arrange. I use a simple script to create log files with below format and i forgot to create daily directory for them at the beginning. Because of this i should move all daily logs into a directory that i need to create. a part of "ls -l" output:... (1 Reply)
Discussion started by: EAGL€
1 Replies

8. Shell Programming and Scripting

how to arrange all lines in a file to a single line

Hi Gurus, I am a starter with shell script. Help me to achieve this. I have a file with lines given below. line1 line2 line3 . . etc How can I arrange the file which should look like line1,line2,line3,..,..,etc Any help on this is appreciated. (9 Replies)
Discussion started by: smv
9 Replies

9. Shell Programming and Scripting

re arrange data

Any idea in awk or sed? $cat file a b c 2 4 5 6 output: a b c 2 4 5 6 (3 Replies)
Discussion started by: kenshinhimura
3 Replies

10. UNIX for Dummies Questions & Answers

Reverse Arrange File

I've got hundreds of lines in a file that looks like this: Line1 CCR CCH Line2 ICVM FBO GSC Line3 MKF The result should be like the one below so that I can insert them on our database. Line1 CCR Line1 CCH Line2 ICVM Line2 FBO Line2 GSC Line3 MKF Thanks in advance! (4 Replies)
Discussion started by: The One
4 Replies
Login or Register to Ask a Question