Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored> . Now this gives me the locations of that string. Now how do I store these locations in a text file.

Did you consider redirecting the output?

---------- Post updated at 03:52 PM ---------- Previous update was at 03:50 PM ----------

Yes I want to redirect the output of the code to a text file

And where are you stuck?

After using grep -ob I am getting the location of the strings but I don't know how to store that in a text file.

Input and output redirection is a fundamental concept in all *nix OS, and even other proprietary OS have borrowed from them, so it'd be good to know their basics.
Use > filename to redirect some command's output to a file. Look into the man page of your shell for more insight.

Suppose I have given input a long string like SGHDRIAKGCJLFIFZKGCHXJFSGKCHVGZRUAJFAKGXLH
I want a text file storing the location of S like 1 24 .
Suppose the string is stored as some text file like string.txt
Now I give the command

grep "S" -ob string.txt

Resultant output is

1 S
24 S

I want to store this output in a text file.

By now we all have understood that. Haven't you read the answer RudiC already gave you in post #6?

Hint: just keeping on repeating the question will do nothing for your understanding. It might help to sit down in front of a terminal and start trying, though.

I hope this helps.

bakunin

1 Like