frazman
frazman

Reputation: 33213

find a string in a file

I have a file of form foo.txt

  id, some_string, stringy_string.. 
  and so on

How do I find a particular id in this foo.txt

This id will/should be present in just one line. (i.e no multiple instances?) Thanks

Upvotes: 1

Views: 158

Answers (3)

reegan vijay
reegan vijay

Reputation: 161

grep "id" filename

You can easily search an id or string or anything from a file.

If you want to replace the string to another string,

you can use sed:

sed 's/your string/new string/g'

Upvotes: 1

Anitha Mani
Anitha Mani

Reputation: 867

sed -n '/id/p' filename it will display that particular pattern (id) and grep -o -n 'id' filename it will display that id and also line number of the pattern matches in that file.

Upvotes: 4

choroba
choroba

Reputation: 241758

It depends on how the id's look like. If they do not contain any grep special characters, you can use

grep '^id,' foo.txt

The ^ matches the beginning of the line, the comma just makes sure the id ends there (so id12 is not matched in id123).

You have to escape special characters if they can appear in the id.

Upvotes: 7

Related Questions