0
plym    fury    77      73      2500
chevy   nova    79      60      3000
ford    mustang 65      45      17000
volvo   gl      78      102     9850
ford    ltd     83      15      10500
Chevy   nova    80      50      3500
fiat    600     65      115     450
honda   accord  81      30      6000
ford    thundbd 84      10      17000
toyota  tercel  82      180     750
chevy   impala  65      85      1550
ford    bronco  83      25      9525

This is the sample data with cars.txt

The expected outcome should be like that.

plym    fury    77      73      2500
chevy   nova    79      60      3000
volvo   gl      78      102     9850
Chevy   nova    80      50      3500
fiat    600     65      115     450
honda   accord  81      30      6000
toyota  tercel  82      180     750
chevy   impala  65      85      1550

How do I write the regular expression to get the result like excluding the column that contain "ford" ?

I've tried like the following

egrep "^[a-zA-Z][a-z][^\rd].*" cars.txt

However, it doesn't work.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Nelson
  • 1
  • 1
  • I'm allowed to use "egrep" only to make it work. Plus, I cannot use any options such as -v -i etc to exclude that line with "ford". – Nelson Jul 15 '23 at 22:51

2 Answers2

1
grep -v -w -i '^ford' cars.txt

Check your grep man page for the meaning of the options.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Yes, it is worked. But, I want to use a different way to exclude "ford" row without including -v option, just by using regular expression. – Nelson Jul 15 '23 at 22:48
  • Why did you not put that requirement in your question? – glenn jackman Jul 16 '23 at 17:12
  • I apologized for late reply. Actually, my question is a tutorial from a college. The answer is different thing. I already asked back my professor and I got the correct answer. I don't mean your answer is not correct. It should work like you wrote too on my tutorial, however, it didn't work out. Thanks for your help. – Nelson Jul 27 '23 at 14:51
1

I suggest with egrep and without -v:

egrep '^([^f]|f[^o]|fo[^r]|for[^d])' cars.txt

Output:

plym    fury    77      73      2500
chevy   nova    79      60      3000
volvo   gl      78      102     9850
Chevy   nova    80      50      3500
fiat    600     65      115     450
honda   accord  81      30      6000
toyota  tercel  82      180     750
chevy   impala  65      85      1550

See: The Stack Overflow Regular Expressions FAQ

Cyrus
  • 84,225
  • 14
  • 89
  • 153