1

I have data in the following form:

<id_mytextadded1829>
<text1>    <text2>    <text3>.
<id_m_abcdef829>
<text4>    <text5>    <text6>.
<id_mytextadded1829>
<text7>    <text2>    <text8>.
<id_mytextadded1829>
<text2>    <text1>    <text9>.
<id_m_abcdef829>
<text11>    <text12>    <text2>.

Now I want to the number of lines in which <text2> is present. I know I can do the same using python's regex. But regex would tell me whether a pattern is present in a line or not? On the other hand my requirement is to find a string which is present exactly in the middle of a line. I know sed is good for replacing contents present in a line. But instead of replacing if I only want the number of lines..is it possible to do so using sed.

EDIT: Sorry I forgot to mention. I want lines where <text2> occurs in the middle of the line. I dont want lines where <text2> occurs in the beginning or at the end of the line. E.g. in the data shown above the number of lines which have <text2> in the middle are 2 (rather than 4).

Is there some way by which I may achieve the desired count of the number of lines by which I may find out the number of lines which have <text2> in middle using linux or python

5 Answers 5

4

I want lines where <text2> occurs in the middle of the line.

You could say:

grep -P '.+<text2>.+' filename

to list the lines containing <text2> not at the beginning or the end of a line.

In order to get only the count of matches, you could say:

grep -cP '.+<text2>.+' filename
2
  • 1
    I don't think the -P is really necessary in this case. (-E would work just fine, as would escaping the + as \+. My man page for grep suggests that the option is highly experimental)
    – Hasturkun
    Nov 10, 2013 at 12:48
  • @Hasturkun Typing -P takes lesser effort in comparison to escaping. PCRE, i.e. YMMV.
    – devnull
    Nov 10, 2013 at 20:08
1

You can use grep for this. For example, this will count number of lines in the file that match the ^123[a-z]+$ pattern:

egrep -c ^123[a-z]+$ file.txt

P.S. I'm not quite sure about the syntax and I don't have the possibility to test it at the moment. Maybe the regex should be quoted.

Edit: the question is a bit tricky since we don't know for sure what your data is and what exactly you're trying to count in it, but it all comes down to correctly formulating a regular expression.

If we assume that <text2> is an exact sequence of characters that should be present in the middle of the line and should not be present at the beginning and in the end, then this should be the regex you're looking for: ^<text[^2]>.*text2.*<text[^2]>\.$

4
  • Sorry I forgot to mention. I want lines where <text2> occurs in the middle of the line. I dont want lines where <text2> occurs in the beginning or at the end of the line. E.g. in the data shown above the number of lines which have <text2> in the middle are 2 (rather than 4). Nov 10, 2013 at 12:21
  • +1 to you..thanks for the help..but my actually requirement is slightly different..i have added it now..very sorry for this Nov 10, 2013 at 12:21
  • Your edit prompts me to comment that please don't suugest ^<text[^2]>.*text2.*<text[^2]>\.$ -- this wouldn't match lines like <text21> <text2> <text1> which the OP seems to want to match.
    – devnull
    Nov 10, 2013 at 12:33
  • @devnull That's right, and that's why I was saying that given such a problem definition it's quite hard to come up with the regex that is really needed. So all we can do, is give a hint as to what it might look like. Nov 10, 2013 at 12:44
0

Using awk you can do this:

awk '$2~/text2/ {a++} END {print a}' file
2

It will count all line with text2 in the middle of the line.

0

I want lines where occurs in the middle of the line. I dont want lines where occurs in the beginning or at the end of the line.

Try using grep with -c

grep -c '>.*<text2>.*<' file

Output:

2
0

Where occur (everywhere)

sed -n "/<text2>/ =" filename

if you want in the middle (like write later in comment)

sed -n "/[^ ] \{1,\}<text2> \{1,\}[^ ]/ =" filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.