grep first n and last n characters from a line in a file

22,558

Solution 1

you can do something like this

awk '{print $1,$2,$3,":",$NF}' logfile

Solution 2

You can use cut as:

Command:

cut --complement -c17-43 file1.txt

Output:

Mar 23 08:20:23 : 235
Mar 23 08:21:45 : 127
Mar 23 08:22:34 : 875
Mar 23 08:25:46 : 322
Mar 23 08:26:12 : 639

Solution 3

You can use sed:

sed -r "s/^(.{15} ?).*(.{5})$/\1\2/" logfile

Per suggestions, I have made the first pattern accommodate single-digit days which may not be zero-padded, and use .* for the middle pattern to be more flexible.

Solution 4

In awk, it's something like this. Very simple.

[zee@dev-instance temp]$ cat file1.txt 
Mar 23 08:20:23 New file got created in sec: 235
Mar 23 08:21:45 New file got created in sec: 127
Mar 23 08:22:34 New file got created in sec: 875
Mar 23 08:25:46 New file got created in sec: 322
Mar 23 08:26:12 New file got created in sec: 639
[zee@dev-instance temp]$ awk -F" " '{ print $1" "$2" "$3" : "$10 }'<file1.txt 
Mar 23 08:20:23 : 235
Mar 23 08:21:45 : 127
Mar 23 08:22:34 : 875
Mar 23 08:25:46 : 322
Mar 23 08:26:12 : 639
[zee@dev-instance temp]$ 

Solution 5

perl -F '' -lane 'print @F[0..15, -5..-1]' yourfile

Explanation

-F '' => split the line into individual characters, IOW, all fields are 1-char thick.

-l => ORS=\n

-a => @F array holds the fields, e.g., $F[15] => holds the 16-th character

-n => don't print unless specifically asked to

@F[0..15, -5..-1] => is a slice of the array @F with the first 16 characters, and the last 5 characters, something along the lines of cut

Share:
22,558

Related videos on Youtube

IAmNoob
Author by

IAmNoob

Updated on September 18, 2022

Comments

  • IAmNoob
    IAmNoob over 1 year

    I have a log file which looks like:

    Mar 23 08:20:23 New file got created in sec: 235
    Mar 23 08:21:45 New file got created in sec: 127
    Mar 23 08:22:34 New file got created in sec: 875
    Mar 23 08:25:46 New file got created in sec: 322
    Mar 23 08:26:12 New file got created in sec: 639
    

    I need the output to look like:

    Mar 23 08:20:23 : 235
    Mar 23 08:21:45 : 127
    Mar 23 08:22:34 : 875
    Mar 23 08:25:46 : 322
    Mar 23 08:26:12 : 639
    

    What I am able to do is just grep either first part or the last part of the line. I am not able to put the two together. How can I get the desired output from my input?

    • IAmNoob
      IAmNoob about 7 years
      @StephenRauch what if the middle content of the line is not same always?
    • IAmNoob
      IAmNoob about 7 years
      @StephenRauch Yes the length is always the same.
    • MikeD
      MikeD about 7 years
      I updated my answer based on knowing the length will be the same.
  • IAmNoob
    IAmNoob about 7 years
    what if the middle content of the line is not same always?
  • pabouk - Ukraine stay strong
    pabouk - Ukraine stay strong about 7 years
    It is much better to use .* instead of .{27} and anchor to the start and end of the line using ^ and $ (but it should work without the anchors too) otherwise you are uselessly depending on the length of the middle part.
  • Prem Joshi
    Prem Joshi about 7 years
    @pabouk that would be best ! i have changed it as per your suggestion...
  • David Conrad
    David Conrad about 7 years
    Note that this may fail when the day of the month is a single digit, depending on whether it is zero-padded.
  • David Conrad
    David Conrad about 7 years
    This will fail if single-digit days aren't zero-padded.
  • thrig
    thrig about 7 years
    Err, the question only shows 10 columns, so what would $F[15] be selecting?
  • Арсений Черенков
    Арсений Черенков about 7 years
    I guess some explanation would be welcomed. OP might want to adapt command to fit other need, of file format change.
  • Admin
    Admin about 7 years
    You need to realize that the columns are all 1-character wide, due to the -F option being supplied with nothing. Better to say: -F '' I guess to make the intent unambiguous.
  • deltab
    deltab about 7 years
    It's also possible to use $(NF - 1) to get the penultimate field, etc.
  • MikeD
    MikeD about 7 years
    Thanks all, I have updated the patterns to accommodate your suggestions and concerns.
  • IAmNoob
    IAmNoob about 7 years
    works exactly I wanted it to.
  • Prem Joshi
    Prem Joshi about 7 years
    @IAmAndroid I'm glad ..... Keep scripting ....