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ó
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.