<Resolved> How can I use grep or sed to display ordered information?

Discussion in 'Programming/Scripts' started by Milly, Jan 28, 2021.

  1. Milly

    Milly Member

    Hi there
    I have been reading the documentation about this but it is not in my language and it is a bit difficult, I have been trying for days, maybe someone can guide me a bit.

    I have a file with 30 lines similar to the following:

    I need to show a range as an example from line 1 to line 2 and that it is displayed in the terminal with the following format:

    I was trying with "grep and sed" but I could not achieve it

    If someone can guide me please, thank you very much.
  2. Jesse Norell

    Jesse Norell Well-Known Member Staff Member Howtoforge Staff

    You could use sed for that, it's a lengthy set of substitutions (one for each month); combine with awk or cut to pick the output fields. Eg. for a start:
    $ cat lines.txt
    02/28/2021 @ 10:00 -> 02/28/2021 @ 11:00!03069 Activity1
    03/01/2021 @ 21:00 -> 03/01/2021 @ 21:30!00023 Actviry2
    03/03/2021 @ 21:00 -> 03/03/2021 @ 21:30!00069 Activity3
    03/04/2021 @ 17:00 -> 03/04/2021 @ 19:00!00069 Activiry4
    $ sed -n '1,2p' lines.txt | sed -E -e 's|^02/(..)|February \1 |g' -e 's|^03/(..)|March \1 |g' | cut -d' ' -f1,2,5,10-
    February 28 10:00 Activity1
    March 01 21:00 Actviry2
    Milly likes this.
  3. Milly

    Milly Member

    It works perfect, thank you very much, it was difficult for me to understand sed, thanks for your time.

Share This Page