Home > Enterprise >  perl oneliner to read file line by line and skip some words by position
perl oneliner to read file line by line and skip some words by position

Time:04-10

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 

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)
  •  Tags:  
  • perl
  • Related