Sponsored Content
Full Discussion: Grep of a SubString
Top Forums UNIX for Dummies Questions & Answers Grep of a SubString Post 302470575 by sreeharshasn on Wednesday 10th of November 2010 11:43:04 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ZGREP(1)                                                      General Commands Manual                                                     ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 04:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy