'perl oneliner to read file line by line and skip some words by position

Given a log like this:

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

Is it possible to read the log line by line, parse it and skip, for example, 3rd, 4th, and 5th word (where "word" is anything separated by whitespace) with a one line command so that I would get this output?

2022-03-30 21:05:00.266 Mylog  

or 3rd and 5th to have output like this?

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


Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 choroba