Search Key in same file but at different positions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search Key in same file but at different positions
# 8  
Old 07-27-2010
A solution is to do all the work in one awk program (not tested):
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 > "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):
Code:
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.

Last edited by aigles; 07-27-2010 at 10:12 AM.. Reason: extra ( removed in DOC assignment inside first script and " added in print statement
This User Gave Thanks to aigles For This Post:
# 9  
Old 07-27-2010
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 ----------

Quote:
Originally Posted by rdcwayx
Code:
awk '{if ($1~/C/) {print > "DOCKEY_" t;next}} {t=$2;print > "DOCKEY_" $2}' urfile

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.
# 10  
Old 07-27-2010
There was type errors in the code of my previous post, they have been edited.

An example with your last sample datas (kam.txt):
Code:
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)
Code:
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:
Code:
$ 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.
This User Gave Thanks to aigles For This Post:
# 11  
Old 07-27-2010
Scipt is working fine
but when i am calling from script am getting following error
Code:
awk: Field  is not correct.
 The input line number is 1.
 The source line number is 6.

i modified the the script like this
Code:
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
# 12  
Old 07-27-2010
Quote:
Originally Posted by kam786sim
Scipt is working fine
but when i am calling from script am getting following error
Code:
awk: Field  is not correct.
 The input line number is 1.
 The source line number is 6.

i modified the the script like this
Code:
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
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

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 13  
Old 07-27-2010
Sorry
I think i might be have done some mistake from my side while putting the code in the script below is the code:
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
Code:
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.

# 14  
Old 07-27-2010
The syntax
Code:
${DOC}

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

Modify your script:
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

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for specific key in a file and print it

Hi All, I have a file abc.txt Database unique name: NEPAL Database name: NEPAL Services: NEPAL_COB,NEPAL_PROD output i need is it should search "Services" and it that search "COB" word so output i need is like NEPAL_COB (3 Replies)
Discussion started by: amar1208
3 Replies

2. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 Replies

3. Shell Programming and Scripting

Search the key from a file to another file

Hi, I need help in solving the below need in UNIX. I have a file datafile as below pg1,dvn1,scls1,cls1,itp1,sku1 pg2,dvn2,scls2,cls2,itp2,sku2 and keyfile as below scls1,1000,a,b,c,d,p1 scls2,1001,a,b,c,d,p1 scls1,1000,a1,b,c,d,p1 scls2,2000,a,b,c,d,p1 (4 Replies)
Discussion started by: bhaski2012
4 Replies

4. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

5. Shell Programming and Scripting

Perl - start search by using search button or by pressing the enter key

#Build label and text box $main->Label( -text => "Input string below:" )->pack(); $main->Entry( -textvariable => \$text456 )->pack(); $main->Button( -text => "Search", -command => sub { errchk ($text456) ... (4 Replies)
Discussion started by: popeye
4 Replies

6. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

7. Shell Programming and Scripting

Help adding a key word search to my script

Hello: I need help adding a key word search to my bash script. I have the following script. My boss whats the user to be able to add a search word e.g. unknown failures for the script to search the logs through and find the instances. I had originally done it so it grepped for unknown... (8 Replies)
Discussion started by: taekwondo
8 Replies

8. Shell Programming and Scripting

search for key word and execute

Hi, I am writing a shell (after 6-7 months)that has to receive text from another shell, check if the first line in the text has a key word and then execute different shell.I could come up with the below program structure, please suggest me if there is a better way to do it or please help me with... (14 Replies)
Discussion started by: rider29
14 Replies
Login or Register to Ask a Question