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
# 1  
Old 07-27-2010
Search Key in same file but at different positions

Hi All,

I want some help in unix.
Current code is
Code:
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
# 2  
Old 07-27-2010
Please post inputfile sample, and the required output for this sample;

Jean-Pierre.
# 3  
Old 07-27-2010
Code:
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
Code:
Filename : DOCKET_12367

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


Code:
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

Last edited by kam786sim; 07-27-2010 at 08:49 AM.. Reason: 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
# 4  
Old 07-27-2010
you can try using flags. see below this might be helpful to you.

Code:
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


Last edited by dazdseg; 07-27-2010 at 09:20 AM.. Reason: forgot to add the var increment condition.
# 5  
Old 07-27-2010
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.
# 6  
Old 07-27-2010
Code:
"^ur_sequence"

^ - use for the first citing of whatever sequence you put after that.....
# 7  
Old 07-27-2010
Code:
awk '{if ($1~/C/) {print > "DOCKEY_" t;next}} {t=$2;print > "DOCKEY_" $2}' urfile

This User Gave Thanks to rdcwayx 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