Find the position of lines matching string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the position of lines matching string
# 8  
Old 01-28-2011
Quote:
Originally Posted by Scrutinizer
@homeboy: How does this: $1 ~ "RMR*"+num work? What does the + signify?
well,i haven't use awk for a long time and i wrote that code without too much thinking,do u mean this kind of usage is seldom to see?SmilieI saw the man pages just now and it seems that ~ can be used as match a pattern either quoted by // or "".I used the "+" to catenate a string and a value,however,i'm not sure it's a right way,but it did gave a correct result here.Smilie

Last edited by homeboy; 01-28-2011 at 04:09 AM..
# 9  
Old 01-28-2011
Remarkable. + is not a concatenation character in awk, but it does something here. But what?
....
I think I figured it out. The + sign forces what follows into numerical context, but also what procedes it. Non-numerical characters turn into 0 (zero) So it becomes equivalent to:
Code:
$1 ~ num


Last edited by Scrutinizer; 01-28-2011 at 05:19 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 10  
Old 01-28-2011
Quote:
Originally Posted by Scrutinizer
Remarkable. + is not a concatenation character in awk, but it does something here. But what?
....
I think I figured it out. The + sign forces what follows in numerical context, but also what procedes it. Other characters turn into 0 (zero) So it becomes equivalent to:
Code:
$1 ~ num

Smilie Thanks,your explanation is compelling.I didn't notice itSmilie
# 11  
Old 01-28-2011
I tried both the snippet and it returns me nothing..
# 12  
Old 01-28-2011
Are you on Solaris? What is you actual command and a sample of your actual input ?
# 13  
Old 01-28-2011
I am using HP-UX.

The command I tried, where bs is the file name and 7203446151111 is teh unique number

HTML Code:
awk '/^GS/{p=NR}  $0~"^RMR\\*"k{f=1} /^GE/&&f{print p,NR;f=0}' k=7203446151111 bs
# 14  
Old 01-28-2011
OK, what does:
Code:
awk '$0~"^RMR\\*"k' k=7203446151111 bs

give or:
Code:
awk '$0~""k' k=7203446151111 bs

Is there an RMR line in the file with that number?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a string and its position in a line from another string

Hello guys, would you please help me with this? this is the line inside a file: first line Something Today YYDDPPSVXIPYYY0XXXOFFS00000000000? I'd like to find the position of string XXX from string PYYY In the example above XXX starts from 6th position from PYYY desired... (4 Replies)
Discussion started by: netrom
4 Replies

2. UNIX for Dummies Questions & Answers

String pattern matching and position

I am not an expert with linux, but following various posts on this forum, I have been trying to write a script to match pattern of charters occurring together in a file. My file has approximately 200 million characters (upper and lower case), with about 50 characters per line. I have merged all... (5 Replies)
Discussion started by: biowizz
5 Replies

3. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

4. UNIX for Dummies Questions & Answers

Find the list of filenames that have the string 31 at 4th and 5th position

Hi, Can anyone let me know the command to know the list of filenames that have string 31 in their 4th and 5th positions inside the file: grep -l "31" main*.txt The above grep lists all the files which have 31 at any position but I want filenames having 31 at position 4 and position 5. (8 Replies)
Discussion started by: okkadu
8 Replies

5. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

6. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies

7. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

8. UNIX for Dummies Questions & Answers

Extracting lines that match string at certain position

I have a fixed length file in the following format <date><product_code><other data> The file size is huge and I have to extract only the lines that match a certain product code which is of 2 bytes length. I cannot use normal grep since that may give undesirable results. When I search for prod... (5 Replies)
Discussion started by: paruthiveeran
5 Replies

9. Shell Programming and Scripting

how to find substring position in a given string

Hello, I have a string like str = "14: Jan 29 13:27:12 : Processor----: : Start of splitting file " from this, i have to find the position or location number starting for "Processor". I have to extract date from this entire string. string which i will give will not have fixed length. ... (2 Replies)
Discussion started by: balareddy
2 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