The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help in sed command (adding a blank line btw each block generated by pattern) frozensmilz Shell Programming and Scripting 2 01-08-2009 12:12 AM
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 06:40 PM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 02:37 AM
Printing out pattern in line FK_Daemon Shell Programming and Scripting 3 11-23-2007 04:27 PM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 07:24 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 01-05-2009
frozensmilz's Avatar
frozensmilz frozensmilz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 28
Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends,

Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming.

The below script "strMatch" is the one of them which i use to search for a pattern. Once the pattern is found it will display the pattern & the line number or the line number alone as shown below.

>Input
strMatch Sample.cpp "PATTERN STRING"
>Output
5: PATTERN STRING

>Input
strMatch Sample.cpp "PATTERN STRING" 2
>Output
5

Many of my other script has started using this script and the dependency to this script has almost became complex such that the new scripts that uses strMatch directly or indirectly for various purposes is slow in execution. I have noticed that the following sed command can be used to tweak strMatch to increase the search speed...

sed '/PATTERN STRING/p' Sample.cpp

But i need to get the line number also like my previous strMatch....Is there any way to print the output like below

Output 1 [Line number & pattern]
5 PATTERN STRING

Output 2 [Line number where pattern was found]
5

Because some of my scripts uses only these line numbers..... Kindly help in making strMatch script fast....the purpose of strMatch is to search for a pattern and return the pattern location or the pattern along with the location.....Below is actual strMatch script...

there is a special case for handling "include" keyword.....I am not able to find any alternative for "exec" command to pass the file content to a variable as such (with correct alignment).....and somehow whenever i search for include keyword...it will exec on all the files in the folder.......I believe with sed command i wont be requiring "exec",

Thanks in advance...........
-------------------------------------------------------------------------

filter(){
pattern=$1
line=$2
for x in $line; do
flag=`echo $x | grep -c $pattern`

if [ $flag -eq 1 ]; then
return $flag
else
return 0
fi
done
}

#filterKeywords(){

#}


pattern=$2
exec<$1
#fileCont=`rdfil $1`
linCnt=1

exitCnt=`lincnt $1`
cnt=0
count=0
mode=$3
#rdfil $1
while read line; do

if [ "$2" == "include" ]; then
quitCon=`echo $line | grep -c "//-------------------------"`
if [ $quitCon -eq 1 ]; then
count=$(($count + 1))
if [ $count -eq 2 ]; then
break
fi
fi
fi

status=`echo $line | grep -c "$2"`
if [ $status -eq 1 ]; then
fflag=`filter $pattern $line`
wcnt=`echo $fflag| wc -l`
if [ $wcnt == 1 ]; then
if [ $exitCnt -ge $cnt ]; then
if [ "$mode" != "2" ]; then
case $mode in
"NUM") # LINE NUMBER
echo $linCnt
;;

"FON") # FIRST OCCURENCE LINE NUMBER
echo $linCnt
exit
;;

"FOLN") # FIRST OCCURENCE LINE
echo $line
exit
;;

"LN") # ONLY LINES
echo $line
;;

*) # DEFAULT
echo $linCnt: $line
;;
esac
#echo "Hi"
else
echo $linCnt
fi
#filterKeywords $line
fi
fi
fi
linCnt=$(($linCnt + 1))

cnt=$(($cnt + 1))
#echo "---------------------------------------$cnt $exitCnt"
done

#echo "Bye!"
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0