Need help in searching a file by range


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help in searching a file by range
# 1  
Old 03-29-2011
Data Need help in searching a file by range

hi,

i need to search a file by range, the file (f3.txt) contains:

Code:
x1=0123318739
x2=0120123456
x3=0120453576
x4=0110445654
x5=0120432343
x6=0129423
x7=0104323433
x8=01232132134
x9=0122344242
x10=012006196
x11=012016546
x12=012091235
x13=0121064598
x14=012194562
x15=0122028556
x16=0122946532
x17=01230894565
x18=0123954858
x19=0120013655

the range is from 01201 to 0122
i used this command

Code:
$ grep 012[01-2] f3.txt

output :

Code:
x2=0120123456
x3=0120453576
x5=0120432343
x9=0122344242
x10=012006196
x11=012016546
x12=012091235
x13=0121064598
x14=012194562
x15=0122028556
x16=0122946532
x19=0120013655


i noticed that x10 and x19 is out of range but it's included
Code:

x10=012006196
x19=0120013655



i don't know why 00 is in the output range while my start range suppose to be from 01201xxxxx to 0122xxxxxx

i'm new in unix so i know it's a stupid question but i need a solution for this problem if you please.>>


Last edited by radoulov; 03-29-2011 at 06:03 PM.. Reason: Code tags, please!
# 2  
Old 03-29-2011
Code:
grep -E '012(0[1-9]|1[0-9]|2)' infile

---------- Post updated at 10:48 PM ---------- Previous update was at 10:45 PM ----------

Code:
$ cat tst2
x1=0123318739
x2=0120123456
x3=0120453576
x4=0110445654
x5=0120432343
x6=0129423
x7=0104323433
x8=01232132134
x9=0122344242
x10=012006196
x11=012016546
x12=012091235
x13=0121064598
x14=012194562
x15=0122028556
x16=0122946532
x17=01230894565
x18=0123954858
x19=0120013655
$ grep -E '012(0[1-9]|1[0-9]|2)' tst2
x2=0120123456
x3=0120453576
x5=0120432343
x9=0122344242
x11=012016546
x12=012091235
x13=0121064598
x14=012194562
x15=0122028556
x16=0122946532

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-29-2011
thanks alot, you'r the best. Smilie

but could you please explain in simple words what is the -E option? (i'm eager to know)
# 4  
Old 03-29-2011
But i would not recommand to use grep for that :
consider the following :

x20=0099990122
x21=0120012120
The pattern 0122 occurs in it so it will be grepped as "being in the range" whereas it is NOT !
just like x21 would match the string 0121 whereas it is NOT in the range ...

I would suggest something like

Code:
awk -F= '{x=0+substr($2,1,5)}(x>1200)&&(x<1230)'

---------- Post updated at 11:05 PM ---------- Previous update was at 11:03 PM ----------

i meant :

Code:
[ctsgnb@shell ~/sand]$ cat tst2
x1=0123318739
x2=0120123456
x3=0120453576
x4=0110445654
x5=0120432343
x6=0129423
x7=0104323433
x8=01232132134
x9=0122344242
x10=012006196
x11=012016546
x12=012091235
x13=0121064598
x14=012194562
x15=0122028556
x16=0122946532
x17=01230894565
x18=0123954858
x19=0120013655
[ctsgnb@shell ~/sand]$ awk -F= '{x=0+substr($2,1,5)}(x>1200)&&(x<1230)' tst2
x2=0120123456
x3=0120453576
x5=0120432343
x9=0122344242
x11=012016546
x12=012091235
x13=0121064598
x14=012194562
x15=0122028556
x16=0122946532
[ctsgnb@shell ~/sand]$

---------- Post updated at 11:12 PM ---------- Previous update was at 11:05 PM ----------

To have more info about the option of grep , just type

Code:
man grep

the -E option makes grep able to understand extended character set for regular expression (this may have minor various behaviour depending on your plateform)

---------- Post updated at 11:43 PM ---------- Previous update was at 11:12 PM ----------

You can also find usefull sed & awk, Second Edition
especially the Appendix A and B by following the links you can then go to the command summary of sed and/or awk.
You can also refer to "mastering the regular expession" http://docstore.mik.ua/orelly/perl4/mre/mre.pdf
This User Gave Thanks to ctsgnb For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to lookup section of file in a range of another file

In the below, I am trying to lookup $1 and $2 from file1, in a range search using $1 $2 $3 of file2. If the search key from file1 is found in file2, then the word low is printed in the last field of that line in the updated file1. Only the last section of file1 needs to be searched, but I am not... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Searching range of filenames witn and without numbers on the end

So I'm grepping for the following right now: ls -la /somedirectory/*.log* | awk '{print $9}' The problem with this is that I get the following output: /somedirectory/errors_1_foo.log /somedirectory/errors_1_foo.log.1 /somedirectory/errors_1_foo.log.2... (4 Replies)
Discussion started by: LinuxRacr
4 Replies

3. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. UNIX for Dummies Questions & Answers

Searching by date range from filenames

Hello all, i have tons of files in folder named like this (yyyymmdd): bookcollection20100729 bookcollection20100730 bookcollection20100731 bookcollection20100801 bookcollection20100802 etc. I need to find files with date range in there names lets say from 2010.07.30 - 2010.08.02 ... (10 Replies)
Discussion started by: Whit3H0rse
10 Replies

8. Shell Programming and Scripting

Searching the lines within a range of time period in a text file

Dear All, Please advice me, I have a text file with one field date and time like below given. I need to find out the lines whchi content the time stamp between Wed May 26 11:03:11 2010 and Wed May 26 11:03:52 2010 both can be included, using awk command which could be an interactive so that I... (6 Replies)
Discussion started by: chinmayadalai
6 Replies

9. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question