Greping a string for only one occurence

Hi,

I have a csv ( comma seperated value) file and I want to search for a particular string in each line of that file only if it occurs only once in the line.
That is same string may be present more than once in a line but I want it to be greped only when it occurs just once.

Please advice me asap.

You can create a new file by replacing your "," to blanks.
ie:
vi flname
:%s/,/b/g

again you have to do cat flname|xargs -n1|sort -u

may be it will help you.

Awadhesh

#!/bin/ksh
mFile='Your_File'
mStr='Your_String'
for mLine in `egrep ${mStr} ${mFile}`
do
  mLine2=`echo ${mLine} | sed "s/${mStr}//2"`
  if [ "${mLine}" = "${mLine2}" ]; then
    echo $mLine
  fi
done

Use that string as a FS with awk and search for 2-field records:

% cat infile 
a,b,c
a,b,a
% awk 'NF==2' FS="a" infile
a,b,c

You can do something like that with sed :

sed -n 's/the_string/&/2p' inputfile

Jean-Pierre.

Aigles, could you please check your solution?

The OP is asking for:

Oups ! My command print line with two occurence of the string !

sed -n '/the_string/{s/the_string//2;t;p;}' inputfile

Jean-Pierre.

Aigles, ran your solution:

sed -n '/the_string/{s/the_string//2;t;p;}' inputfile

And received an error message:

Too many {'s

Hi Every body
This is pinky new to forums

Hi
Can any body help
I am writing a shell script and the parameter through command line is
orderlink=20
I am tring to get the substring
Example
String is orderlink=20 and I wanted to have substring after "="
my new string should be 20
The input string can be different as integration=15

Thnaks in advance

This command works fine with GNU sed.
I tried the command on my AIX box, and i get the same error than you.
The t sub-command of sed interprets all that follows as being a label.

The following syntax is accepted on AIX :

sed -n -e '/the_string/{s/the_string//2;t' -e 'p;}' inputfile

Jean-Pierre.

Welcome to the forums :slight_smile:

This is not encouraged.

As a start looking for something like this,

val=`echo "orderlink=20" | sed 's/^.*=//g'`

Actually this should have been a new thread :slight_smile:

how abt this

>cat check
first abc string string
second def string
third string string string
fourth string

If searchstring is "string"

awk -F"string" 'NF - 1 == 1 { print }' check

I am so sorry I am new to forums and could not able to figure out how to start a new thread can any body route me how to create a new thread

Thanks
val=`echo "Orderlink=20" | sed 's/^.*=//g'` worked
You are really helpful

if you are open to other alternatives, here's a Python script.

#!/usr/bin/python
for line in open("file"):
    if line.count("searchstring") == 1:
        print line.strip()

If you click on any of the subforums you should find a ' NewThread ' button - where you could create a new thread. :slight_smile: