Check for rows with particular string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for rows with particular string
# 1  
Old 07-29-2013
Check for rows with particular string

Hi. The data file is as below:

2000922111111100232091212098324....
2123011230912832094820943684896....
3435983453409583405938453049583....
.
.
.

I need to get only the rows that match my criteria. For example: those at characters 5-10 should equal to "922111" (thus getting only the 1st row above).

As my script processes a lot of these files, I only need to check for this string first before I run succeeding codes to minimize run time. An "if-cut" condition should work but I don't want to do this per line (again, due to run time considerations).

Hope you can help me with an alternative solution.

Thanks!!!
# 2  
Old 07-29-2013
Code:
awk 'substr($0,5,6)=="922111"' file

# 3  
Old 07-29-2013
Quote:
Originally Posted by Yoda
Code:
awk 'substr($0,5,6)=="922111"' file

Sorry, but I forgot to mention that I need to filter a number range for that string. Example, I only need to show the files/rows that have chars 5-10 withiin the range of 922000 to 9229999.

Hope you can help.
# 4  
Old 07-29-2013
Code:
awk 'substr($0,5,6)>="922000" && substr($0,5,6)<="9229999"' file

# 5  
Old 07-29-2013
Quote:
Originally Posted by krishmaths
Code:
awk 'substr($0,5,6)>="922000" && substr($0,5,6)<="9229999"' file

I knew it! Thanks Krishmaths! Smilie
# 6  
Old 07-29-2013
awk 'substr($0,5,6)>="922000" && substr($0,5,6)<="9229999"' file
This will not work, since you are testing 6 characters substr($0,5,6) against a number of 7 digit number 9229999
Not sure if you like 6 or 7 digit numbers 922000 vs 9229999

Another solution:
Code:
awk 'substr($0,5,7)~/922[0-9][0-9][0-9][0-9]/' file

or:
Code:
awk --posix 'substr($0,5,7)~/922[0-9]{4}/' file

# 7  
Old 07-29-2013
Quote:
Originally Posted by Jotne
awk 'substr($0,5,6)>="922000" && substr($0,5,6)<="9229999"' file
This will not work, since you are testing 6 characters substr($0,5,6) against a number of 7 digit number 9229999
Not sure if you like 6 or 7 digit numbers 922000 vs 9229999

Another solution:
Code:
awk 'substr($0,5,7)~/922[0-9][0-9][0-9][0-9]/' file

or:
Code:
awk --posix 'substr($0,5,7)~/922[0-9]{4}/' file

Sorry for the typo, but my earlier msg should've read 922000 to 922999 (fixed length).

It's working now. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting delimited string into rows

Hi, I have a requirement that has 50-60 million records that we need to split a delimited string (Delimeter is newline) into rows. Source Date: SerialID UnidID GENRE 100 A11 AAAchar(10)BBB 200 B11 CCCchar(10)DDD(10)ZZZZ Field 'GENRE' is a string with new line as delimeter and not sure... (5 Replies)
Discussion started by: techmoris
5 Replies

2. Shell Programming and Scripting

Need help in splitting the string to diff rows

Hi, I have file with values as below 1~ab~456~ac:bd:de:ef~yyyy-mm-dd 2~cd~458~af:fg:ty:er:ty:uj:io:~yyyy-mm-dd I want the o/p as for frist row 1~ab~456~ac~yyyy-mm-dd 1~ab~456~bd~yyyy-mm-dd 1~ab~456~de~yyyy-mm-dd 1~ab~456~ef~yyyy-mm-dd and for the second row 2~cd~458~af~yyyy-mm-dd... (4 Replies)
Discussion started by: rithushri
4 Replies

3. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

4. Shell Programming and Scripting

How to cut the particular string and increment the rows?

Hi am using unix aix I have tried using awk but am getting only output = x ,its not incrementing next output set -A var1 vv qa za ct=0 i=3 while do var1=`echo ${var1}` count=`awk ' NR==$i++ {print;exit}' ${.txt} | cut -c5 ` echo $count let ct=ct+1 done (6 Replies)
Discussion started by: Venkatesh1
6 Replies

5. Shell Programming and Scripting

How to check count of rows in excel sheet in Unix

Hi, Would anyone be able to tell me how to check the number of rows in an excel sheet on unix box, please? Cheers, Girish. (2 Replies)
Discussion started by: girish1428
2 Replies

6. UNIX Desktop Questions & Answers

Need to ftp some files, and check the number of rows in the transferd file

1)I need to write a script which ftps 3 files to a unix box, 2)once the files are ftped i need to check the number of rows in each file and compare it with the data (no of rows) coming in a manifest file, if the number of rows in each file matches the data coming in manifest file, then i need to... (3 Replies)
Discussion started by: imran_affu
3 Replies

7. Shell Programming and Scripting

Parsing the string into several rows

I'm getting the input row in this format it contains 2 alphabets followed by numbers between 1 and 7 and again 2 alphabets followed by numbers between 1 and 7. Now I need to parse this input into several output rows in this format 2 alphabets followed by each number occurrence suppose... (5 Replies)
Discussion started by: abhijith321
5 Replies

8. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

10. Shell Programming and Scripting

Append string in all rows

I would like to append a string at the end of each row of a .dat file. please give me some advice? Thanks. e.g. #content in abc.dat aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... #after running the script/command,... (7 Replies)
Discussion started by: Rock
7 Replies
Login or Register to Ask a Question