0

I'm trying to extract all the lines from a file that end with /42 and then remove said /42 with the command cut but for some reason its not working. Here is the command

grep "42$" questionnaires.txt > test42 | cut -d '/' -f 1 test42

and the output when I use cat ont test42 is

La dernière armure... ?/42

I don't know what I'm doing wrong so any help is appreciated.

1 Answer 1

3

This command

grep "42$" questionnaires.txt > test42 | cut -d '/' -f 1 test42

doesn't do anything like you think it does.

The first part:

grep "42$" questionnaires.txt > test42

That writes the output of grep to test42. The output of that is nothing.

So that nothing then gets passed to

cut -d '/' -f 1 test42

Which is trying to read the file you're currently writing to, and sending the results to the screen. That's likely to be no output, and the file test42 will have the data without the cut being applied.

Now if you want the final file to just have the data you want to change the order:

grep "42$" questionnaires.txt | cut -d '/' -f 1 > test42

Now this means "get the results of the grep" and then "pass that through cut" and store the results of that in the file test42

2
  • thank you that was exactly what I needed and I will have to talk to my teacher.
    – zactrixo
    Commented Feb 23, 2020 at 2:42
  • This will be a problem with the cut command if you have other '/' characters in the line. It's better to just remove the entire pattern since you know it. (e.g. something like sed -e 's|/42||g'
    – Angelo
    Commented Feb 23, 2020 at 5:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .