Search in specific position and print the whole line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search in specific position and print the whole line
# 1  
Old 03-11-2010
Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows)
for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line.

if its present the output needs to go to bde.dat

Example:

contents abc.dat
Code:
aaaa
bbbb
cccc
dddd

contents sant.dat
Code:
this is a new messagexxxx  aaaa
this is not a new message  bbbb
this is not a new message  xxxx


Output
Code:
this is a new messagexxxx  aaaa
this is not a new message  bbbb


Last edited by Yogesh Sawant; 03-11-2010 at 05:36 AM.. Reason: added code tags
# 2  
Old 03-11-2010
Actually, it should be 27,4 in your example...
Code:
awk 'NR==FNR{_[substr($0,1,4)];next}{for(i in _){if(i==substr($0,27,4)){ print $0}}}' abc.dat sant.dat

# 3  
Old 03-11-2010
i am getting the below error...

Code:
$ awk 'NR==FNR{_[substr($0,1,4)];next}{for(i in _){if(i==substr($0,27,4)){ print $0}}}' abc.dat sant.dat
awk: _ is not an array
 record number 1


Last edited by pludi; 03-11-2010 at 06:30 AM.. Reason: code tags, please...
# 4  
Old 03-11-2010
Try with nawk instead of awk
# 5  
Old 03-11-2010
works fine now...Thanks a lot for all the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

3. Shell Programming and Scripting

Print in a specific position

HI My input is 12345678901234567890123456789012345678 pay 234 56789.23 7788934.5 2456 calcul 123 456 678 98 i want to print the line starting with "pay" as $2 should commence from 7 irresptive of its length and $3 to start from 14 and $4 to start from 24 and $5 from 36.... (4 Replies)
Discussion started by: Indra2011
4 Replies

4. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

5. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

6. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

7. Shell Programming and Scripting

Search for multiple strings in specific position

Hi, I need to search for some strings in specific positions in a file. If the strings: "foo1", "foo2" or "foo3" is on position 266 or position 288 in a file i want the whole line printed. Any idea how to do it? (5 Replies)
Discussion started by: HugoH
5 Replies

8. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

9. Shell Programming and Scripting

How to print at a specific position of the display

Hi, How can I write a string in a specific position of the screen in a bourne shell? Regards, Elio (1 Reply)
Discussion started by: efernandes
1 Replies

10. Shell Programming and Scripting

Print lines with search string at specific position

Hi Folks, I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas? ... (4 Replies)
Discussion started by: HealthyGuy
4 Replies
Login or Register to Ask a Question