0

A file named start.txt exists. I am using a perl command which gives modified output using the content of start.txt and i am trying to overwrite the modified output to the same file

cat start.txt | perl -ne 'print "$1-$1.mp4 Lecture $2 -\n" while /.*\/videos\/(.*?mp4).*?(?<= )(\d+)/g;'> start.txt

Running the above command makes the file start.txt empty for some reason

There's no issue with my command because if i create a new file for redirecting the output like..

cat start.txt | perl -ne 'print "$1-$1.mp4 Lecture $2 -\n" while /.*\/videos\/(.*?mp4).*?(?<= )(\d+)/g;'> test.txt

This perfectly saves the expected output in test.txt but I don't want to create extra files , i need to overwrite the exiting file itself.

I tried >|after reading its used to force overwrite files but still same issue. I even tried | tee start.txt , still causes the file to become empty .

On a side note : There are some awk commands which i use , if i try to save/redirect the output in a file from which the awk function reads text from , it causes the same issue of making the output file empty I would paste the awk command too if someone asks although i think there would be a universal solution for this

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Sachin
  • 1,217
  • 2
  • 11
  • 31
  • 2
    I recommend not writing to the file you are reading from, it will break the file. – Cyrus Jun 20 '20 at 08:47
  • Take a look at perl's `-i` option. – Cyrus Jun 20 '20 at 08:48
  • @Cyrus thanks i understood the issue now – Sachin Jun 20 '20 at 09:17
  • 1
    Duplicate of [How can I use a file in a command and redirect output to the same file without truncating it?](https://stackoverflow.com/questions/6696842/how-can-i-use-a-file-in-a-command-and-redirect-output-to-the-same-file-without-t) – oguz ismail Jun 20 '20 at 09:19
  • 1
    @oguzismail that's nice , the accepted answer will definitely serve my purpose , thanks for sharing – Sachin Jun 20 '20 at 09:20
  • @oguz ismail, Those are nice general solutions, but Perl's `-i` option would be better here. – ikegami Jun 20 '20 at 13:52
  • @ikegami the point is that the question is a duplicate. – oguz ismail Jun 20 '20 at 14:53
  • @oguz ismail Maybe, but not of the question you linked. The same question can have different answers for different languages, and that's the case here. – ikegami Jun 20 '20 at 14:55

1 Answers1

1

The reason for an empty file in this command

cat start.txt | perl -ne 'print "$1-$1.mp4 Lecture $2 -\n" while /.*\/videos\/(.*?mp4).*?(?<= )(\d+)/g;'> start.txt

is the

> start.txt

bit.

When you enter the command into the shell, the shell parses it and prepare things for running. It creates the unnamed pipe to communicate cat with perl and it also opens the output file, truncating it, and redirected the perl standard output to the file. Finally, now that all needed redirections are in place, it starts the different commands. cat and perl in this case. But the file that cat is going to read has already been truncated by the redirection you asked the shell to do.

As already pointed in a comment, your best bet is to avoid writing to the same file you are reading from.

Poshi
  • 5,332
  • 3
  • 15
  • 32
  • . I remembered a very small thing i had to ask which remotely relates to the question too , is there any command in linux which can clear all text from a file , not delete file but just delete its text/content ? – Sachin Jun 20 '20 at 09:13
  • 1
    You just used it! The source of all your trouble: `> file_to_clear` – Poshi Jun 20 '20 at 09:20
  • thanks although for some reason doing it was causing my terminal to get stuck , doing - ```: > file_to_clear``` working instantly – Sachin Jun 20 '20 at 09:28
  • 1
    @Sachin See also the `truncate` utility. – ikegami Jun 20 '20 at 13:53
  • @ikegami thanks fo suggestion , i will check out perl -i and truncate utility – Sachin Jun 20 '20 at 13:54