compare and redirect to new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare and redirect to new file
# 1  
Old 07-09-2008
Bug compare and redirect to new file

hi i have two file

a.txt
123,b,c,d,e
111,c,d,e,f,
456,a,k,j,h

b.txt
123
678
987
321
456

i want to compare these two files(match content of b first coloum with a ) and o/p should be like
123,d,e
456,j,h

pls help.....Smilie
# 2  
Old 07-09-2008
Hi,

Try this,

Code:
grep -f b.txt a.txt | awk -F, '{print $1","$4","$5}'

Regards,
Chella
# 3  
Old 07-09-2008
thanks for reply but grep -f is not supported in my system
# 4  
Old 07-09-2008
Then it can be done using awk,

Code:
awk -F, 'FNR==NR {a[$1];next;}($1 in a) {print $1","$4","$5}' b.txt a.txt

Regards,
Chella
# 5  
Old 07-09-2008
hi its giving error

cat a.txt
123,b,c,d,e
111,c,d,e,f,
456,a,k,j,h

cat b.txt
123
678
987
321
456

awk -F, 'FNR==NR {a[$1];next;}($1 in a) {print $1","$4","$5}' b.txt a.txt
awk: syntax error near line 1
awk: bailing out near line 1
# 6  
Old 07-09-2008
But the awk works fine for me and I got your expected output.

Try this,

Code:
awk -F, 'NR==FNR{a[$1]=$1;next}a[$1]' b.txt a.txt

From the above output you need to print only the first,fourth and fifth fields.

Regards,
Chella

Last edited by chella; 07-09-2008 at 06:40 AM.. Reason: Added an another way for it.
# 7  
Old 07-09-2008
Quote:
Code:
awk -F, 'FNR==NR {a[$1];next;}($1 in a) {print $1","$4","$5}' b.txt a.txt
awk: syntax error near line 1
awk: bailing out near line 1

Use nawk or /usr/xpg4/bin/awk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect to a file

Hi, Is there a way to redirect the output of a set of commands to a file instead of using << at every command in bash shell. For eg (echo hostname echo "Comparing 2 files" comm -3 file1 file2) >>file3 (3 Replies)
Discussion started by: Rossdba
3 Replies

2. Shell Programming and Scripting

Read file from input and redirect to output file

Hi , i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names for eg: file.txt contains file_name1.txt|hai file_name2.txt|bye file_name3.txt|how... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

3. Shell Programming and Scripting

Need script to compare and redirect

Hi, I have a file, sol_servers_global, which is list of all solaris global servers in my environment and another file, solaris_non_global_zones_from_DB. I have a gateway server, from where all servers are contactable via ssh. I have collected name of zones running on these servers with below... (3 Replies)
Discussion started by: solaris_1977
3 Replies

4. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log | awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}' I am creating a health script that has... (4 Replies)
Discussion started by: chipblah84
4 Replies

5. Shell Programming and Scripting

How to redirect stderr to a file as well

Hello everyone, I'm a nooby in Linux, and I need some help. I have a shell script like this: echo "Start of script" > ../My_Log_Dir/Script_Name.log .. cp ../My_DataIn/File.txt ../My_DataOut/ 2>> ../My_Log_Dir/Script_Name.log rc=$? .. echo "End of Script" >>... (5 Replies)
Discussion started by: H.Faria
5 Replies

6. HP-UX

how to redirect the growing contents of log file to another file in unix

how to redirect the growing contents of log file to another file in unix (2 Replies)
Discussion started by: megh
2 Replies

7. HP-UX

How to Redirect the error messages from Syslog file to our own Application Log File

Hello, I am New to Unix. I am Using HP-UX 9000 Series for my Application. I am Currently Facing an Issue that the error messages are being written in the syslog file instead of the Application Log File. The Codes for that Syslog.h is written in Pro*C. I want to know how to Redirect these... (3 Replies)
Discussion started by: balasubramaniam
3 Replies

8. UNIX for Dummies Questions & Answers

redirect input from file?

I need to run a command inside root-environment, and want to use: su - root -c "some command..." Then I am prompted for a password, of course. Is it possible to put this password in a file, and present this files content, to the command above?:confused: (1 Reply)
Discussion started by: bjornrud
1 Replies

9. Shell Programming and Scripting

Search--Compare--Redirect

Hai Guru's, I have 3 files.A,B,C The file A,B is very huge.What i need to do is...I just get each record from file A and compare with File B...if the record presents...redirect the record to File C..otherwise not needed... Kindly give me ur valuable suggestions to resolve the above... (1 Reply)
Discussion started by: satheesh_color
1 Replies

10. Shell Programming and Scripting

redirect output to file?

Hi: I am currently working on a program which requires direct its ouput to a file here is an example ./proram arg_1 arg_2 when program ends all output will be arg_2 file Is that possible I am not a bad programmer, However I am stuck there. Can anyone give a hint? Thanks SW (1 Reply)
Discussion started by: slackware
1 Replies
Login or Register to Ask a Question