Search Key in same file but at different positions

Hi All,

I want some help in unix.
Current code is

for DOC in `InputFileName | awk '{ if ( substr($0, 392, 1)=="C") { $0 = (substr($0, 28, 15) "" substr($0, 386, 3)) }  else { ($0 = substr($0, 28, 18))}; print }' | sort | uniq`
do
    grep ${DOC} InputFileName > DOCKET_${DOC}.txt
done

Here it is searching for key record type C and then making a key DOC used to write in different file. And other records in same file.

But my problem here is as grep is searching for DOC as same sequence in file it is not able to find the DOC key we made for the record type C and this record is not getting written in the output file.

How can i make this grep to search both type of keys?
Thanks!

---------- Post updated at 04:56 PM ---------- Previous update was at 03:06 PM ----------

I have tried grep but i am not able to implement it.

I have an idea
i think in loop
I can put another grep which will search for the records matching the key
and append to file.

But my problem is i am not able to do multiple search in grep

Please post inputfile sample, and the required output for this sample;

Jean-Pierre.

Example input file
Its an fixed width file
1            12367                A       1100
2            12367                B       2200
3            12367                C       3300
C            123                   D       4467
1            12467                A       1100
 2            12467                B       2200
 3            12467                C       3300
 C            124                   D       4467

So here for C record we are picking first 3 and adding the "67" value to make 5 digit key.

To generate the output files as

Filename : DOCKET_12367

1            12367                A       1100
 2            12367                B       2200
 3            12367                C       3300
 C            123                   D       4467

Filename : DOCKET_12467

1            12467                A       1100
  2            12467                B       2200
  3            12467                C       3300
  C            124                   D       4467

---------- Post updated at 05:20 PM ---------- Previous update was at 05:16 PM ----------

This are smale files and not the actual file which i am using for my code as i have pasted my code in 1st post

you can try using flags. see below this might be helpful to you.

var=1
for DOC in `InputFileName | awk '{ if ( substr($0, 392, 1)=="C") { $0 = (substr($0, 28, 15) "" substr($0, 386, 3)) }  else { ($0 = substr($0, 28, 18))}; print }' | sort | uniq`
do
if [[ ##check if var is odd ]]
then
    grep ${DOC} InputFileName > DOCKET_${DOC}.txt
else 
UR_CODE for Second key
fi
var++
done

Hi Thanks for your reply.
I dont have any second key i am generating different files by unique DOC numbers generated in for loop.

And then for each file i am putting the record for same DOC in file for same DOC number.

But problem is for record type 'C' i am generating the DOC number from two different fixed positioned columns.

And I don't know how to search a single line same time so the same DOC number an be matched at that position.

"^ur_sequence"

^ - use for the first citing of whatever sequence you put after that.....

awk '{if ($1~/C/) {print > "DOCKEY_" t;next}} {t=$2;print > "DOCKEY_" $2}' urfile

A solution is to do all the work in one awk program (not tested):

awk '
{
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3)
   else
      DOC = substr($0, 28, 18);
   print $0 > "DOCKET_" DOC ".txt"
}
' InputFileName

The awk program may fail if the number of output files is important (all the files will be opened and not closed until the end of program).

Another approch (not tested):

awk '
{
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3)
   else
      DOC = substr($0, 28, 18);
   print DOC SUBSEP $0;
}
' InputFileName | \
awk '
BEGIN { FS = SUBSEP }
{
   DOC = $1;
   if (DOC != prvDOC && out) close(out);
   out = "DOCKET_" DOC ".txt";
   print $2 > out
}
'

Jean-Pierre.

Hi aigles,

I i tried your script.
But i am getting only one output row as output and that is also the last row of the file.

And also in send awk i am not able to understand where we are doing the column comparison for it.

To genrate different doc

---------- Post updated at 06:36 PM ---------- Previous update was at 06:29 PM ----------

Hi My level is two small to
understand this so difficult code.

But thanks for you efforts.

I tried executing it But it is generating 3 different files for 3 input record.
And contain one record per file

---------- Post updated at 06:42 PM ---------- Previous update was at 06:36 PM ----------

And my problem is not generating the DOC key value my code is running fine for it.
My problem is to search the same DOC key value splitting it and match with different columns for record type C.
And if it matches to be written in the same DOC key file i am writing for.

There was type errors in the code of my previous post, they have been edited.

An example with your last sample datas (kam.txt):

1            12367                A       1100
2            12367                B       2200
3            12367                C       3300
C            123                  D       4467
1            12467                A       1100
2            12467                B       2200
3            12467                C       3300
C            124                  D       4467

The code (kam.sh)

awk '
{
   if ( substr($0, 1, 1)=="C" )
      DOC = substr($0, 14, 3)  substr($0, 45, 2)
   else
      DOC = substr($0, 14, 5);
   print $0 > "kam_" DOC ".txt"
}
' kam.txt

Result:

$ head kam_*.txt
==> kam_12367.txt <==
1            12367                A       1100
2            12367                B       2200
3            12367                C       3300
C            123                  D       4467

==> kam_12467.txt <==
1            12467                A       1100
2            12467                B       2200
3            12467                C       3300
C            124                  D       4467
$

Jean-Pierre.

Scipt is working fine
but when i am calling from script am getting following error

awk: Field  is not correct.
 The input line number is 1.
 The source line number is 6.

i modified the the script like this

cat $INPUT/$param_InputFileName | awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3);
   else
      DOC = substr($0, 28, 18);
   print $0 > WOR/"WORK_" DOC ".txt"
}'

I have to change it because when i was using the one you give i was getting error file not found.

---------- Post updated at 09:24 PM ---------- Previous update was at 09:20 PM ----------

Same code is working fine when i dont put in script
what is the problem

Try

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3);
   else
      DOC = substr($0, 28, 18);
   print $0 > "WOR/WORK_" DOC ".txt"
}' $INPUT/$param_InputFileName

Jean-Pierre.

Sorry
I think i might be have done some mistake from my side while putting the code in the script below is the code:

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3);
   else
      DOC = substr($0, 28, 18);
   print $0 > $WOR/WORK_${DOC}.txt
}' $INPUT/$param_InputFileName

But i am getting this error

syntax error The source line is 6.
 The error context is
                   print $0 > >>>  $WOR/WORK_${ <<<
 awk: The statement cannot be correctly parsed.
 The source line is 6.
 awk: Quitting
 The source line is 6.

The syntax

${DOC}

is valid for shell variables.
Here you are inside an awk program that does'nt accept this syntax.

Modify your script:

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3);
   else
      DOC = substr($0, 28, 18);
   print $0 > "$WOR/WORK_" DOC ".txt"
}' $INPUT/$param_InputFileName

Jean-Pierre.

Hi,

awk: Cannot find or open file $C

if i give the way you told i am getting the error as

awk: Cannot find or open file $WOR_/Work_TIR453220100721533.txt

My modified code

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3)
   else
      DOC = substr($0, 28, 18)
   print $0 > "$WOR/Work_" DOC ".txt"
}' $INPUT/$param_InputFileName

Sorry, I forgot a modification (Assume that WOR is'nt a shell variable):

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3);
   else
      DOC = substr($0, 28, 18);
   print $0 > "WOR/WORK_" DOC ".txt"
}' $INPUT/$param_InputFileName

If WOR is a shell variable :

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3);
   else
      DOC = substr($0, 28, 18);
   print $0 > Dir "/WORK_" DOC ".txt"
}' Dir=$WOR $INPUT/$param_InputFileName

Jean-Pierre.

Hi I tried with this option i got the below error

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3)
   else
      DOC = substr($0, 28, 18)

   print $0 > $WOR "/WORK_" DOC ".txt"
}' $INPUT/$param_InputFileName

Error for above code:

awk: Field  is not correct.
 The input line number is 1. The file is inputfilename.
 The source line number is 6.

Them i read i have to put the awk in single quotes i tried to put this below way

awk '{ 
   if ( substr($0, 392, 1)=="C" )
      DOC = substr($0, 28, 15)  substr($0, 386, 3)
   else
      DOC = substr($0, 28, 18)

   print $0 > '"$WOR"' "/WORK_" DOC ".txt"
}' $INPUT/$param_InputFileName

ERROR For above code:

awk: Cannot divide by zero.

 The input line number is 1. The file is input filename.
 The source line number is 7.

Why don't you try the code of my last post ?

Jean-Pierre.

Thanks for all help.
It worked fine.