Home > Net >  perl oneliner to read file line by line and skip some word by position
perl oneliner to read file line by line and skip some word by position

Time:03-31

Given a log like this:

2022-03-30 21:05:00.266  0200 CEST debug  Mylog 

is it possible to parse it (with a oneliner command) and skip, for example 3rd 4th and 5th so that I would get this output?

2022-03-30 21:05:00.266 Mylog  

CodePudding user response:

perl -lane 'splice @F, 2, 3; print "@F"'
  • -l removes newlines from input and adds them to output
  • -n reads the input line by line, running the code for each line
  • -a splits each line on whitespace into the @F array
  • splice removes three elements from @F starting with position 2 (numbering starts at 0)
  • Related