I would like to use the grep command to print a line number for the string I'm searching for. However, when I used the command grep -n
it prints the line number with a colon : next to it. Is there any way I can use the same grep -n
command to print a line number with it being followed by the : colon?
$ grep -n 'Captain' sometextfile.txt
3: Captain America
I would like it to instead print,
3 Captain America
(without the ':' )
Any suggestions??
Best Answer
You can use
sed
along withgrep
to achieve this.grep -n 'Captain' sometextfile.txt | sed 's/:/ /'
This sed command finds the pattern
:
and replaces it with(a space).
General Syntax -
sed 's/find/replace/'
This method will replace only the first occurence of
:
in each line with. Therefore, even if a line in the text file contains
:
, it will remain unaffected.