-2

I'm currently working on a homework assignment and it is basically saying, "Find the single line with "Ju" but does not contain the letter "w" in that line"

I believe that I have to use grep. However I'm not sure if I can just add this to a file.

grep "Ju" | grep -v "w"

maybe?

grep "alpha" | grep -v "beta" > file-name
  • 3
    Why not try it and see what happens? (Looking at it, it's much more likely to do what you want than to make your computer explode ;) ) – Ulrich Schwarz Feb 9 '16 at 7:09
  • @UlrichSchwarz I would I'm just terrified I would ruin the progress that I have already made. Does grep change files? I can't remember. – Joe Feb 9 '16 at 7:17
2

Your command is right, just add to it the filename

grep "Ju" file.txt | grep -v "w"

Answering your comment, grep doesn't change files.

  • so having (grep "Ju" file.txt | grep -v "w" > newfile.txt) would be correct – Joe Feb 9 '16 at 7:18
  • Yes, you may replace '>' with '>>' if you don't want to overwrite the file (newfile.txt) – Marware Feb 9 '16 at 7:20
  • Awesome! let me try this out – Joe Feb 9 '16 at 7:24
2

You can use:

grep '^[^w]*Ju[^w]*$' file.txt
  • ^ and $ denote start and end of the line respectively

  • [^w]* matches everything excepts character w

So the above Regex necessary matches Ju in the line and make sure that the line does not contain w ([^w]*).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.