one-liner: remove the same line from many files

In one of the projects I work on there are thousands of YAML file with data. For historical reason some of those files had an entry called id: followed by a number. e.g. id: 529.

They were referring to the id in the original format from where the YAML…


This content originally appeared on DEV Community and was authored by Gábor Szabó

In one of the projects I work on there are thousands of YAML file with data. For historical reason some of those files had an entry called id: followed by a number. e.g. id: 529.

They were referring to the id in the original format from where the YAML files were created. At one point we did not need that line any more and it was just confusing the non-technical editors of these files.

So I wanted to remove them.

The solution is a Perl one-liner:

perl -n -i -e 'print if not /^id:/' *.yaml
  • -n means: go over each line of each file
  • -i means "inplace editing" - replace the files with what the program prints
  • -e means the following string is the code
  • 'print if not /^id:/' means: print the current line if it does not start with id:
  • This code uses behind the scenes the default variable that is also called $_, but it is better to use it implicitly
  • /^id:/ is just a simple regular expression
  • Perl allows the inverting of the if-statements writing: statement if condition.
  • *.yaml is an instruction to the Unix/Linux shell to list all the files with YAML extension as parameters of the command


This content originally appeared on DEV Community and was authored by Gábor Szabó


Print Share Comment Cite Upload Translate Updates
APA

Gábor Szabó | Sciencx (2023-02-26T16:59:48+00:00) one-liner: remove the same line from many files. Retrieved from https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/

MLA
" » one-liner: remove the same line from many files." Gábor Szabó | Sciencx - Sunday February 26, 2023, https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/
HARVARD
Gábor Szabó | Sciencx Sunday February 26, 2023 » one-liner: remove the same line from many files., viewed ,<https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/>
VANCOUVER
Gábor Szabó | Sciencx - » one-liner: remove the same line from many files. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/
CHICAGO
" » one-liner: remove the same line from many files." Gábor Szabó | Sciencx - Accessed . https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/
IEEE
" » one-liner: remove the same line from many files." Gábor Szabó | Sciencx [Online]. Available: https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/. [Accessed: ]
rf:citation
» one-liner: remove the same line from many files | Gábor Szabó | Sciencx | https://www.scien.cx/2023/02/26/one-liner-remove-the-same-line-from-many-files/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.