Need help in sed command [ printing a pattern + its line no or line no alone ]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in sed command [ printing a pattern + its line no or line no alone ]
# 1  
Old 01-05-2009
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...........SmilieSmilie
-------------------------------------------------------------------------

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!"
# 2  
Old 01-05-2009
Have you tried grep? This gives the number of the line and the line delimited by a colon:

Code:
grep -n <pattern> <file>

To get the number you can do something like:

Code:
grep -n <pattern> <file>| cut -d: -f1

Check the man page of grep and cut for the used options.

Regards
# 3  
Old 04-29-2009
CPU & Memory I need only first appearance of the pattern ?

Quote:
Originally Posted by Franklin52
Have you tried grep? This gives the number of the line and the line delimited by a colon:

Code:
grep -n <pattern> <file>

To get the number you can do something like:

Code:
grep -n <pattern> <file>| cut -d: -f1

Check the man page of grep and cut for the used options.

Regards
I need only first appearance of the matched pattern ?

help me......
# 4  
Old 04-29-2009
Quote:
Originally Posted by shivarajM
I need only first appearance of the matched pattern ?

help me......
Have you tried the -m option of grep?
# 5  
Old 06-28-2009
get line number using sed command

Hi,

If the file is as given below, please let me know if the command is right to get the like number containing the string - Earth with the "=" symbol.

Filename: example.txt
123|Earth|2009

sed '/Earth/
{
=
}' example.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

7. Shell Programming and Scripting

Printing previous line based on pattern using sed

Hi, I have a written a shell script to get the previous line based on the pattern. For example if a file has below lines: ---------------------------------------------- #UNBLOCK_As _per #As per 205.162.42.92 #BLOCK_As_per #----------------------- #input checks abc.com... (5 Replies)
Discussion started by: Anjan1
5 Replies

8. Shell Programming and Scripting

Need help in sed command (adding a blank line btw each block generated by pattern)

Hello friends, I have a C source code containing sql statements. I use the following sed command to print all the sql blocks in the source code.... sed -n "/exec sql/,/;/p" Sample.cpp The above sed command will print the sql blocks based on the pattern "exec sql" & ";"... (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

Printing out pattern in line

I've scoured the forum and found similar problems but I can't seem to adapt them to help me with my cause. This is a two-part question. I have a multi line file generated by ps | -ef I need to print out a certain type of pattern. The pattern is part static and part dynamic. It is a... (3 Replies)
Discussion started by: FK_Daemon
3 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question