Search multiple columns in each row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search multiple columns in each row
# 1  
Old 06-14-2010
Search multiple columns in each row

Hi All,

I have to search in multiple columns for multiple values, if the match is found then print the values as below.

Eg:

cat t1
Code:
Z|VLD_AB_P|VLD_CD_P|VLD_EF_F|VLD_GH_F|100
Y|VLD_AB_F|VLD_CD_F|VLD_EF_P|VLD_GH_P|101

Code:
if [ $2 == "VLD_AB_F" ] then print "Invalid AB in $6"
if [ $3 == "VLD_CD_F" ] then print "Invalid CD in $6"

---I want to print "Invalid AB, CD in $6"

--I started with this> a
Code:
wk -F"|" ' { if($2 == "VLD_AB_F") print $6; if($3 == "VLD_CD_F") print "Invalid BU";}' t1

Is there a way I can write a one-liner(in ksh) to search in each row/column for a value and if match is found then print the value

Last edited by vgersh99; 06-14-2010 at 06:09 PM.. Reason: code tags, please!
# 2  
Old 06-14-2010
Can you show expected output?
# 3  
Old 06-14-2010
if [ $2 == "VLD_AB_F" ] then print "Invalid AB in $6"
if [ $3 == "VLD_CD_F" ] then print "Invalid CD in $6"

---for the above I want to print "Invalid AB, CD in $6"
[for all the matches the corresponding value]

Basically, _F is failure. So I will check in few columns for this and echo the corresponding error message
# 4  
Old 06-14-2010
Code:
nawk -F'|' '
{
   str=""
   for(i=1;i<=NF;i++) {
       n=split($i,a,"_")
       if(a[n]=="F") str=(str)?str","a[n-1]:a[n-1]
   } 
   if (str) print "Invalid " str " in $6"
}' myFile

# 5  
Old 06-23-2010
First of all, I want to thank you all for your wonderful support here.

The requirement changed a little bit and I could not tweak the logic, can someone assist me here.

The requirement is:

Following is the input data

Code:
 
Z|VLD_AB_P|VLD_CD_P|VLD_EF_F|VLD_GH_F|100|UUU|0000-01-01
Y|VLD_AB_F|VLD_CD_F|VLD_EF_P|VLD_GH_P|101|ZZZ|2010-06-23

I have to check for

Code:
 
If [ `grep ^Z inpfile` ]
 then
       if [ `awk -F"|" '{print $2} line` -eq "VLD_AB_F" ]
          then
                 echo " Check failed in Z record, line number is .. ,keys are $6"
       fi
 
      if [ `awk -F"|" '{print $3} line` -eq "VLD_EF_F" ]
          then
                 echo " Check failed in Z record, line number is .??. ,keys are $5"
       fi
   ###similarly I have to check for all the fields and report the errors##
elif  [ `grep ^Y inpfile` ]
 then
       if [ `awk -F"|" '{print $2} line` -eq "VLD_AB_F" ]
          then
                 echo " Check failed in Y record, line number is ??.. ,keys are $6"
         fi
###similarly I have to check for all the fields and report the errors##

I thought of scanning each record and the field in the file by nested if. Wanted to know if we can achieve this thru awk single liners.
# 6  
Old 06-23-2010
Code:
 
###similarly I have to check for all the fields and report the errors##

Based on What ???

Please post a sample case , which shows the input and the expected output.

Regards
Ravi
# 7  
Old 06-23-2010
I apologize for the confusion. In Field 2,3,4,5 I will check for the following and If I find the match I will echo the message.

Code:
 
ErrCodes: VLD_AB_F|VLD_CD_F|VLD_EF_F|VLD_GH_F
 
Z|VLD_AB_P|VLD_CD_P|VLD_EF_F|VLD_GH_F|100|UUU|0000-01-01
Y|VLD_AB_F|VLD_CD_F|VLD_EF_P|VLD_GH_P|101|ZZZ|2010-06-23
 
If field -1 = Z && field2 (delim - 2) = VLD_AB_F Then print"Check failed in Z record, line number is .. ,keys are $6"
If field -1 = Z && field3 (delim - 3) = VLD_CD_F Then print"Check failed in Z record, line number is .. ,keys are $7"
If field -1 = Z && field4 = VLD_EF_F Then print"Check failed in Z record, line number is .. ,keys are $8"
If field -1 = Z && field5  = VLD_GH_F Then print"Check failed in Z record, line number is .. ,keys are $6"
 
###similarly for Y record
If field -1 = Y && field2 (delim - 2) = VLD_AB_F Then print"Check failed in Y record, line number is .. ,keys are $6"
###---so on for other fields in Y record
###--There will be multiple Z/Y records in the file and I have to scan all the records##

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

3. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

4. UNIX for Dummies Questions & Answers

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 (0 Replies)
Discussion started by: khaled1989kh
0 Replies

5. Shell Programming and Scripting

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 give mee the code .... (0 Replies)
Discussion started by: khaled1989kh
0 Replies

6. Shell Programming and Scripting

Splitting data from one row as multiple columns

Hi I have a file containing some data as follows: 11-17-2010:13:26 64 4 516414 1392258 11-17-2010:13:26 128 4 586868 695603 11-17-2010:13:26 256 4 474937 1642294 11-17-2010:13:32 64 4 378715 1357066 11-17-2010:13:32 128 4 597981 1684006 ... (17 Replies)
Discussion started by: annazpereira
17 Replies

7. Shell Programming and Scripting

How to convert 2 column data into multiple columns based on a keyword in a row??

Hi Friends I have the following input data in 2 columns. SNo 1 I1 Value I2 Value I3 Value SNo 2 I4 Value I5 Value I6 Value I7 Value SNo 3 I8 Value I9 Value ............... ................ SNo N (1 Reply)
Discussion started by: ks_reddy
1 Replies

8. Shell Programming and Scripting

Row to Columns question

Hi, I have a question for Row to Columns by script. e.g. i have a file Start 1 2 3 4 End Start 1 2 3 3 4 4 End (6 Replies)
Discussion started by: bleach8578
6 Replies

9. Shell Programming and Scripting

Transpose - Multiple row to Multiple columns

I have seen many posts to transpose rows into columns, but not for multiple rows to columns..so please help me how to do this.. I have around 1000 rows and 1000 columns, i want to transpose it.. A1 A2 A3 B1 B2 B3 C1 C2 C3 D1 D2 D3 I want output to be like A1 B1 C1 D1 A2 B2 C2 D2... (5 Replies)
Discussion started by: ganeshss
5 Replies

10. UNIX for Dummies Questions & Answers

Row to Columns

Hi, I have a file like this. 1,1,1,0,0,0 1,1,2,1,0,0 1,1,3,0,0,0 1,1,4,0,0,0 ........... ........... 1,1,24,0,0,0 1,1,25,0,0,0 1,1,26,1,0,0 1,1,27,0,0,0 1,2,1,0,0,0 1,2,2,0,0,0 1,2,3,0,0,0 1,2,4,0,0,0 1,2,5,1,0,0 1,2,6,1,0,0 (4 Replies)
Discussion started by: vskr72
4 Replies
Login or Register to Ask a Question