DEV Community

Gabor Szabo
Gabor Szabo

Posted on

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 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
Enter fullscreen mode Exit fullscreen mode
  • -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

One could also use unless instead of if not, but it seems to be harder to understand.

perl -n -i -e 'print unless /^id:/' *.yaml
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
yet_anotherdev profile image
Lucas Barret

Perl is not something we see often on dev article nowadays.
Really cool thanks :D