Home > Net >  Sed command printing out full line
Sed command printing out full line

Time:09-17

Im having trouble with sed in UNIX. I would like print out every instance of the letter I, but instead this command prints out the full line everytime it finds an I. Any suggestions? For Example: if the line is "There is something that I want" I would like it to just print out I instead of the full line.

find | sed -nE '/\bI\b/p' file.txt 

CodePudding user response:

Because it is what you are telling it to do:

  • -n means don't print lines by default
  • /\bI\b/ means match a separate letter I
  • p means print the line that matched the previous pattern

Maybe this is what you want, matching everything but I and replacing it with nothing:

]$ echo "xIxIxIx" | sed 's/[^I]//g'
III

CodePudding user response:

You can use the complement option in tr.

echo "There is something that I want" | tr -cd 'I'

When you want to find I in different files, you can combine with find:

find . -type f -name file.txt -exec cat {}   | tr -cd 'I'
  •  Tags:  
  • unix
  • Related