Help me Out in unix scripting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help me Out in unix scripting
# 8  
Old 02-19-2009
With more then 2 file you should use some temporary files with join. Another way is to use awk:

Code:
awk -F, '
FILENAME=="file1"{a[$1]=$0}
FILENAME=="file2" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file2" && !(a[$1]){a[$1]=$0}
FILENAME=="file3" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file3" && !(a[$1]){a[$1]=$0}
END{for(i in a)print a[i]}
' file1 file2 file3

This is the output I get:

Code:
$ cat file1
001,AA,43,rrrr
002,FF,44,gg
$
$ cat file2
001,43,66
003,43,77
$
$ cat file3
001,55,555
003,44,444
$
$ awk -F, '
FILENAME=="file1"{a[$1]=$0}
FILENAME=="file2" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file2" && !(a[$1]){a[$1]=$0}
FILENAME=="file3" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file3" && !(a[$1]){a[$1]=$0}
END{for(i in a)print a[i]}
' file1 file2 file3
001,AA,43,rrrr,43,66,55,555
002,FF,44,gg
003,43,77,44,444
$
$

Regards
# 9  
Old 02-20-2009
Hi Franklin52,

I applied your given code, but it gave different output :

cat file1
ID, Name, Address, remark
001,AA,43,rrrr
002,FF,44,gg

cat file2
ID,marks,age
001,43,66
003,43,77

cat file3
ID, amount,balance
001,55,555
003,44,444

awk -F, '
> FILENAME=="file1"{a[$1]=$0}
> FILENAME=="file2" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
> FILENAME=="file2" && !(a[$1]){a[$1]=$0}
> FILENAME=="file3" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
> FILENAME=="file3" && !(a[$1]){a[$1]=$0}
> END{for(i in a)print a[i]}
> ' file1 file2 file3
OUTPUT is below:
002,FF,44,gg
003,43,77,44,444
ID, Name, Address, remark ,marks,age, amount,balance
001,AA,43,rrrr,43,66,55,555

Desired output should be like that:
ID, Name, Address, remark , marks, age, amount, balance
001, AA, 43, rrrr, 43, 66, 55, 555
002, FF, 44, gg
003, 43, 77, 44, 444
# 10  
Old 02-20-2009
With the given solution you should be able to fit the code, anyway:

Code:
awk -F, 'BEGIN{print "ID, Name, Address, remark , marks, age, amount, balance"}
FILENAME=="file1"{a[$1]=$0}
FILENAME=="file2" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file2" && !(a[$1]){a[$1]=$0}
FILENAME=="file3" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file3" && !(a[$1]){a[$1]=$0}
END{for(i in a)print a[i]|"sort"}
' file1 file2 file3

Regards
# 11  
Old 02-20-2009
Hi,
using give code, output is

ID, Name, Address, remark , marks, age, amount, balance
001,AA,43,rrrr,43,66,55,555
002,FF,44,gg
003,43,77,44,444
ID, Name, Address, remark ,marks,age, amount,balance
which is not required.


i want to remove last line to repeat. Also i want output in single file rather than result display on screen.

Thanks
# 12  
Old 02-20-2009
Quote:
Originally Posted by kaprus
Hi,
using give code, output is

ID, Name, Address, remark , marks, age, amount, balance
001,AA,43,rrrr,43,66,55,555
002,FF,44,gg
003,43,77,44,444
ID, Name, Address, remark ,marks,age, amount,balance
which is not required.


i want to remove last line to repeat. Also i want output in single file rather than result display on screen.

Thanks
Sorry, I forgot something, try this:

Code:
awk -F, 'BEGIN{print "ID, Name, Address, remark , marks, age, amount, balance"}
FNR==1{next}
FILENAME=="file1"{a[$1]=$0}
FILENAME=="file2" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file2" && !(a[$1]){a[$1]=$0}
FILENAME=="file3" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="file3" && !(a[$1]){a[$1]=$0}
END{for(i in a)print a[i]|"sort"}
' file1 file2 file3 > newfile

Regards
# 13  
Old 02-24-2009
Hi,

I am trying to do something like that

FILE1=$1
FILE2=$2
FILE3=$3
awk -F, 'BEGIN{print "ID, Name, Address, remark , marks, age, amount, balance"}
FNR==1{next}
FILENAME=="$FILE1"{a[$1]=$0}
FILENAME=="$FILE2" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="$FILE2" && !(a[$1]){a[$1]=$0}
FILENAME=="$FILE3" && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME=="$FILE3" && !(a[$1]){a[$1]=$0}
END{for(i in a)print a[i]|"sort"}
' $FILE1 $FILE2 $FILE3 > newfile

using
./script.sh file1 file2 file3


But its not working.
can u assist me??
# 14  
Old 02-24-2009
The variables aren't used properly, try this:

Code:
awk -F, -v f1=$1 -v f2=$2 -v f3=$3 '
BEGIN{print "ID, Name, Address, remark , marks, age, amount, balance"}
FNR==1{next}
FILENAME==f1{a[$1]=$0}
FILENAME==f2 && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME==f2 && !(a[$1]){a[$1]=$0}
FILENAME==f3 && a[$1] {for(i=2;i<=NF;i++){a[$1]=a[$1] FS $i};next}
FILENAME==f3 && !(a[$1]){a[$1]=$0}
END{for(i in a)print a[i]|"sort"}
' $1 $2 $3 > newfile

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX scripting

LIST=/home/xxxxxxx/ABC/xeeno_scrpts/temp/tempfile ERRORDIR=/home/xxxxxxx/ABC/error_directory EMAILFILE=/home/xxxxxxx/ABC/xeeno_scrpts/temp/emailfile echo "There were errors in the following report file for Xeenos:" > $EMAILFILE echo >> $EMAILFILE echo "This files have been moved to... (9 Replies)
Discussion started by: bcarosi
9 Replies

2. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

3. UNIX for Advanced & Expert Users

Unix scripting

i need help with this problem this is the problem: Write a script that logs how many users login on/off the system over a 5 minute period. It can run in the foreground, and run 4 times a minute. Set a trap that will not allow a CNTRL-C command, and if a CNTRL-C is excuted store the time and date... (1 Reply)
Discussion started by: sportsmansixty6
1 Replies

4. Shell Programming and Scripting

Unix scripting

how to check if a unix script gets executed without errors across all unix platforms. incase if a script gets executed without errors only one platform say AIX, what needs to be done to that script such that it will run all unix platforms like linux, hp, sun etc (2 Replies)
Discussion started by: rmann
2 Replies

5. Shell Programming and Scripting

Unix Scripting

Hi Gurus, I am a system admin in solaris field and ive planned to study unix scripting.ive planned to start reading Mastering Unix scripting by randal.Scripts in that are based on Korn shell(ksh).my question is whether the same scripts can be applied to other shells like bash etc..And... (4 Replies)
Discussion started by: madanmeer
4 Replies

6. UNIX for Dummies Questions & Answers

Unix Scripting

Hi Gurus, I am a system admin in solaris field and ive planned to study unix scripting.ive planned to start reading Mastering Unix scripting by randal.Scripts in that are based on Korn shell(ksh).my question is whether the same scripts can be applied to other shells like bash etc..And... (1 Reply)
Discussion started by: madanmeer
1 Replies

7. UNIX for Dummies Questions & Answers

Unix scripting pl help

Hi All, I am new to Unix Scripting. I have below scenario. I need to write a Unix function with the following. 1. I have table. From this table I need to write a query. SELECT Col1(File_nm),Col2(From_Loc),Col3(To_Loc) FROM A WHERE CONDITION For... (1 Reply)
Discussion started by: sree11
1 Replies

8. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

9. UNIX for Dummies Questions & Answers

Help With Unix Scripting.

Can anybody tell me the best way to learn unix scripting.can you recommend a good book.Please help! (1 Reply)
Discussion started by: hella
1 Replies

10. UNIX for Dummies Questions & Answers

UNIX Scripting

:confused: I need to find a place or places on the Internet where I can find UNIX scripts to view and to modify to make life easy on the UNIX environment. Can someone help me on this. Thanks (7 Replies)
Discussion started by: wolf
7 Replies
Login or Register to Ask a Question