Grep of a SubString


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep of a SubString
# 1  
Old 11-09-2010
MySQL Grep of a SubString

Dear Folks,
I am having a requirement to grep a word which could be a part of a substring of a file. Please help me with the command for korn shell.

Let me give you a example
I am having a file in which the data is based on positions.
Lets say
1-5 is Emp ID 6-10 is Emp Name 11-15 is Salary

Code:
10110RAMAS90890
10111GURUS88990
10112SHEEP10110
10113SRIKA10110

Now my requirement is when I search for Emp Id 10110 in the file using grep command It shud NOT return me the third and fourth lines. Because those two lines salary value is same as the value of Emp Id of first line.
Please help me with this. It is a urgent requirement for me.
# 2  
Old 11-09-2010
Code:
[ctsgnb@shell ~]$ cat infile
10110RAMAS90890
10111GURUS88990
10112SHEEP10110
10113SRIKA10110
[ctsgnb@shell ~]$ empid=10110
[ctsgnb@shell ~]$ grep ^$empid infile
10110RAMAS90890
[ctsgnb@shell ~]$ grep -E ^.{10}$empid infile
10112SHEEP10110
10113SRIKA10110
[ctsgnb@shell ~]$

# 3  
Old 11-09-2010
Thanks for the reply

Hi ctsgnb,
Thanks for your reply. Can you please tell me what is the use of ^. I am new to UNIX. Also, if the emp ID does not start in the position 1 how do we need to modify the command. Please help me.

Example Data

Position 1-3 Country
4-8 Emp ID
9-13 Name
14-18 Salary
Code:
IND10110RAMAS90890
AUS10111GURUS88990
SRI10112SHEEP10110
ENG10113SRIKA10110

# 4  
Old 11-10-2010
Not an elegant Solution but a solution anyways.. I am sure there will be better ways of doing this

following is a shell script that searches for the string in a given field.
Code:
#!/bin/bash
if  [ $# -lt 3 ]
then
echo "Usage: ./empListQuery -FieldNo SearchString FileName"
else
field=$1
string=$2
file=$3
count=1
lines=`wc -l $file | cut -d ' ' -f 1`
while [ $count -le $lines ]
do
case $field in
    1)
        head -$count $file | tail -1 | cut -c 1-3 | grep  -q $string 
    if [ $? -eq 0 ]
then
     head -$count $file | tail -1
fi ;;
    2)
        head -$count $file | tail -1 | cut -c 4-8 | grep  -q $string 
    if [ $? -eq 0 ]
then
         head -$count $file | tail -1 
fi ;;
    3)
        head -$count $file | tail -1 | cut -c 9-13 | grep  -q $string 
    if [ $? -eq 0 ]
then
         head -$count $file | tail -1 
fi ;;

    4)
        head -$count $file | tail -1 | cut -c 14-18 | grep  -q $string 
    if [ $? -eq 0 ]
then
         head -$count $file | tail -1 
fi ;;

    *)
        echo "Please enter a number within 4 for firstArgument"
  esac
count=`expr $count + 1`
done
fi


Last edited by Scott; 11-10-2010 at 01:57 PM.. Reason: Please use code tags
# 5  
Old 11-10-2010
the "^" means "the beginning of the line"
the dot "." match any single character

Code:
[ctsgnb@shell ~]$ grep $empid infile
10110RAMAS90890
10112SHEEP10110
10113SRIKA10110
[ctsgnb@shell ~]$


Last edited by ctsgnb; 11-10-2010 at 03:31 PM..
# 6  
Old 11-15-2010
Bug Thanks..

Thanks for your replies..I have used the following code to find the exact match..

Code:
cat <filename> | cut -c <start position>-<end-position> | grep -n "required data"

From the above I will get the exact line number of my required data matched in the specific position. Then to extract the line from the data.I have used

Code:
Head -<linenumber> filename | tail -1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

2. Shell Programming and Scripting

Substring

Hi All, In ksh, am trying to get a substring stuff done. Not sure where the problem is.. can you guys guide me on this... for instance, var1=41, and var2=4175894567, then i want to know whether var2 starts with var1.. var1 and var2 can be of any length.. VAR1=41 VAR2=419068567777... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

3. UNIX for Dummies Questions & Answers

grep exact string/ avoid substring search

Hi All, I have 2 programs running by the following names: a_testloop.sh testloop.sh I read these programs names from a file and store each of them into a variable called $program. On the completion of the above programs i should send an email. When i use grep with ps to see if any of... (3 Replies)
Discussion started by: albertashish
3 Replies

4. Shell Programming and Scripting

How to grep substring from a line ?

Hi all, I am stuck in a tricky situation where i want to search a substring from a line through shell script.What i meant by that is given in the example shown below: Line :: This is the TEST(Message(with some tricky word)) from which i want to search. Result substring should be :... (8 Replies)
Discussion started by: titanicstarin
8 Replies

5. Shell Programming and Scripting

Substring HELP!

Hi, I am trying to do something which I thought was very simple but still being a beginner, has proved not to be. Input: val1 val2 val3 val4 val5 val6 . . . etc Desired Output: Every row in which value of val6 is a number starting with 0.0 or contains a capital E. The input... (2 Replies)
Discussion started by: awknerd
2 Replies

6. Shell Programming and Scripting

substring

Dear All, i have a file that contains, FROM_DATE: 06-08-2007 00:00:00 TO_DATE: 06-08-2007 23:59:59 Total number of lines: 6874154 in another file,the contain is, FROM_DATE: 06-08-2007 00:00:00 Total number of lines: 874154 alltime i want to find the particular string... (4 Replies)
Discussion started by: panknil
4 Replies

7. Shell Programming and Scripting

How do I Substring ??

Hello everyone. I'm writing a script in UNIX. The purpose is to get the second character from a variable that stores the system year. This is the code: unix_year_yy=`date "+%g"` This will return "07" in variable unix_year_yy. How can I get the second character (7)?? (6 Replies)
Discussion started by: Rigger
6 Replies

8. UNIX for Dummies Questions & Answers

substring

Hi, I have a value of a filepath in a variable DATAFILE with value as "customtop/gpsore37/gepspo/1.0/bin/ashoka.csv ". Now i want the value of last 4 charcters in to another variable. That is EXTENSION = .csv How can i do this in Shell scripting Thanks in advance Alla Kishore (8 Replies)
Discussion started by: alla.kishore
8 Replies

9. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies

10. Shell Programming and Scripting

Getting a substring

This is probably pretty simple butI'm not sure how to best go about it. If I have FILE="myBigLongFileName_1.xls" FILE_PREFIX=`echo $FILE| cut -d"." -f1` # that gives "myBigLongFileName_1" All i want to do now is chop the "_1" from the end of $FILE_PREFIX Any ideas anyone? (3 Replies)
Discussion started by: djkane
3 Replies
Login or Register to Ask a Question