Wrong result return from script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wrong result return from script
# 8  
Old 10-09-2013
Quote:
Originally Posted by Akshay Hegde
Whether this is your interest ??
Code:
$ nawk -F"," 'NR==FNR{a[$4]=$3;next}{if ($2 in a) print $0","a[$2]",""Found";else print $0",""----""," "Not Found";}' tmplist srclist
cde,file4,11:42,Found
cde,file5,----,Not Found
def,file6,----,Not Found
def,file7,----,Not Found
def,file8,----,Not Found
abc,file1,11:42,Found
abc,file2,11:42,Found
abc,file3,11:43,Found
acd,file9,----,Not Found
acd,file10,----,Not Found

Thanks for your replay,
the result are pretty close to my expection. I need get the date partion as well
Code:
cde,file4,Oct,9,11:42,Found

one more question, what's the a[$2] mean in below command?
print $0","a[$2] ",""Found"
# 9  
Old 10-09-2013
Quote:
Originally Posted by ken6503
Thanks for your replay,
the result are pretty close to my expection. I need get the date partion as well
Code:
cde,file4,Oct,9,11:42,Found

one more question, what's the a[$2] mean in below command?
print $0","a[$2] ",""Found"
a[$2]contains what you defined in first block in present case
'NR==FNR{a[$4]=$0;next} it contains row ($0) and you are matching with $2 of second file

then use this

Code:
awk -F"," 'NR==FNR{a[$4]=$0;next}{if ($2 in a) print $0","a[$2] ",""Found";else print $0",""---------""," "Not Found";}' tmplist srclist
cde,file4,Oct,9,11:42,file4,Found
cde,file5,---------,Not Found
def,file6,---------,Not Found
def,file7,---------,Not Found
def,file8,---------,Not Found
abc,file1,Oct,9,11:42,file1,Found
abc,file2,Oct,9,11:42,file2,Found
abc,file3,Oct,9,11:43,file3,Found
acd,file9,---------,Not Found
acd,file10,---------,Not Found

This User Gave Thanks to Akshay Hegde For This Post:
# 10  
Old 10-09-2013
Quote:
Originally Posted by Akshay Hegde
a[$2]contains what you defined in first block in present case
'NR==FNR{a[$4]=$0;next} it contains row ($0) and you are matching with $2 of second file

then use this

Code:
awk -F"," 'NR==FNR{a[$4]=$0;next}{if ($2 in a) print $0","a[$2] ",""Found";else print $0",""---------""," "Not Found";}' tmplist srclist
cde,file4,Oct,9,11:42,file4,Found
cde,file5,---------,Not Found
def,file6,---------,Not Found
def,file7,---------,Not Found
def,file8,---------,Not Found
abc,file1,Oct,9,11:42,file1,Found
abc,file2,Oct,9,11:42,file2,Found
abc,file3,Oct,9,11:43,file3,Found
acd,file9,---------,Not Found
acd,file10,---------,Not Found

Thank you very much.
there is one more column in result:
Code:
abc,file1,Oct,9,11:42,file1,Found

I only need date time as below
Code:
abc,file1,Oct,9,11:42,Found

Thanks in advance. you are great
Smilie
# 11  
Old 10-09-2013
use this code

Code:
$ awk -F"," 'NR==FNR{a[$4]=$0;next}{print ($2 in a)?$0 FS sprintf("%s",substr( a[$2],0,length(a[$2])-length($2)-1)) FS "Found" :$0 FS "Not Found"}'  tmplist srclist

Code:
cde,file4,Oct,9,11:42,Found
cde,file5,Not Found
def,file6,Not Found
def,file7,Not Found
def,file8,Not Found
abc,file1,Oct,9,11:42,Found
abc,file2,Oct,9,11:42,Found
abc,file3,Oct,9,11:43,Found
acd,file9,Not Found
acd,file10,Not Found

# 12  
Old 10-09-2013
This may be what you want:
Code:
awk -F"," 'NR==FNR {a[$4]=$1FS$2FS$3;next}{printf "%s,", $0; if (a[$2]) printf "%s ", a[$2]; else printf "not "; print "found"}'  tmpfile srclist
cde,file4,Oct,9,11:42 found
cde,file5,not found
def,file6,not found
def,file7,not found
def,file8,not found
abc,file1,Oct,9,11:42 found
abc,file2,Oct,9,11:42 found
abc,file3,Oct,9,11:43 found
acd,file9,not found
acd,file10,not found

EDIT: or even
Code:
awk -F"," 'NR==FNR {a[$4]=$1FS$2FS$3;next}{printf "%s,%s %s\n", $0, a[$2]?a[$2]:"not", "found"}'  tmplist srclist

This User Gave Thanks to RudiC For This Post:
# 13  
Old 10-09-2013
Quote:
Originally Posted by RudiC
This may be what you want:
Code:
awk -F"," 'NR==FNR {a[$4]=$1FS$2FS$3;next}{printf "%s,", $0; if (a[$2]) printf "%s ", a[$2]; else printf "not "; print "found"}'  tmpfile srclist
cde,file4,Oct,9,11:42 found
cde,file5,not found
def,file6,not found
def,file7,not found
def,file8,not found
abc,file1,Oct,9,11:42 found
abc,file2,Oct,9,11:42 found
abc,file3,Oct,9,11:43 found
acd,file9,not found
acd,file10,not found

EDIT: or even
Code:
awk -F"," 'NR==FNR {a[$4]=$1FS$2FS$3;next}{printf "%s,%s %s\n", $0, a[$2]?a[$2]:"not", "found"}'  tmplist srclist

Thanks for both of you to add such valuable input for me.
One more question:
I need to get the group which all files are found in this group. the group identified by first column (cde, abc). in above result I want to get result as below:
Code:
abc,file1,Oct,9,11:42 found
abc,file2,Oct,9,11:42 found
abc,file3,Oct,9,11:43 found

for group "cde", there is one file missing, so I don't want get it.

thanks in advance.
Smilie

---------- Post updated at 05:18 PM ---------- Previous update was at 04:08 PM ----------

Quote:
Originally Posted by Akshay Hegde
use this code

Code:
$ awk -F"," 'NR==FNR{a[$4]=$0;next}{print ($2 in a)?$0 FS sprintf("%s",substr( a[$2],0,length(a[$2])-length($2)-1)) FS "Found" :$0 FS "Not Found"}'  tmplist srclist

Code:
cde,file4,Oct,9,11:42,Found
cde,file5,Not Found
def,file6,Not Found
def,file7,Not Found
def,file8,Not Found
abc,file1,Oct,9,11:42,Found
abc,file2,Oct,9,11:42,Found
abc,file3,Oct,9,11:43,Found
acd,file9,Not Found
acd,file10,Not Found

I found below code in this forum, I think this will help to achieve my goal. I have some question for this code:
Code:
awk -F , '! a[$1 FS $2] {b[$1]++;a[$1 FS $2]++}END {for (i in b) print i","b[i]}' infile

1. what' "! a[$1 FS $2] " mean?
2. if I want to print out the entire record, what I should change?
Code:
abc,1
cde,2

I want to get;
Code:
abc,Oct,9,11:42, 1
cde,Oct,9,11:42, 2

Thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Shell Programming and Scripting

Python - Function print vs return - whats wrong

All, I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this #!/usr/bin/python def div4and6(s,e): for i in range(s,e+1): if... (5 Replies)
Discussion started by: oky
5 Replies

3. UNIX for Advanced & Expert Users

File command return wrong filetype while file holds group separator char.

hi, I am trying to get the FileType using the File command. I have one file, which holds Group separator along with ASCII character. It's a Text file. But when I ran the File command the FileType is coming as "data". It should be "ASCII, Text file". Is the latest version of File... (6 Replies)
Discussion started by: Arpitak29
6 Replies

4. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

5. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

6. Shell Programming and Scripting

Result of Catching Return Value from Sub_script.sh to Main_script.sh is not as Expected

Main_script.sh #! /bin/sh ./Sub_script.sh rc=$? echo "Return code from Sub_script.sh : $rc" if ; then echo "$rc = 991" echo "" exit 1 elif ; then echo "$rc = 992" echo "" exit 1 elif ; then echo "$rc = 0" echo "" exit 1 fi (2 Replies)
Discussion started by: duddukuri
2 Replies

7. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

8. Solaris

df command on Sol8 machine doesn't return a result

I have a sun4u system running Solaris 8. I tried running the df command but it returns a blank result. Also I'm unable to collect an explorer from this system as the OS complains that the disk is full. What could be going on here? (10 Replies)
Discussion started by: dperry1973
10 Replies

9. UNIX for Dummies Questions & Answers

Any way to grep a string in directories and return the result with diskusage aswell?

What Im basically trying to do is this: I have a small script that can grep any parameter entered into a search string, then print to the screen the name of each file the parameter appears in as well as the file path, ie the directory. The code Im using just for this is.... Directory... (3 Replies)
Discussion started by: Eddeh
3 Replies

10. Shell Programming and Scripting

Pick up the return code for every iteration and display the result only once in loop.

Hi All, I amlearning UNIX scripting. I have a small query. I would be thankful if any one helps me out. I have a below piece of code which delets the files. If file dosent have the permissions to delete a particular file I have used 2>>operator to track the error code. But my objective is... (1 Reply)
Discussion started by: manas6
1 Replies
Login or Register to Ask a Question