To group records in the file in UNIX

Hi All,

I am using RHEL 6.9. I got a requirement to group the records in a file.The file content as shown below.

#### FAILED JOBS IN XXX #####

1> ABCD failed in the project XXX
2> HJK Job is in compiled state in the project XXX
3> ILKD failed in the project XXX
4> DFG failed in the project XXX
5> HOI Job is in compiled state in the project XXX
6> TYU failed in the project XXX

My requirement is to group all the record with message containing Job is in compiled state into one area and other group with rest all jobs. So my expected output is
given below

#### FAILED JOBS IN XXX#####
1> ABCD failed in the project XXX
2> ILKD failed in the project XXX
3> DFG failed in the project XXX
4> TYU failed in the project XXX

#### COMPILED JOBS IN  XXX#####

1> HJK Job is in compiled state in the project XXX
 2> HOI Job is in compiled state in the project XXX

Appreciate your help

How about

awk '
NR == 1         {print
                 next
                }
! NF            {next
                }
/failed/        {$1 = ++FCNT ">"
                 print
                 next
                }
                {$1 = ++CCNT ">"
                 C[CCNT] = $0
                }
END             {print ORS "#### COMPILED JOBS IN  XXX#####" ORS
                 for (c=1; c<=CCNT; c++) print C[c]
                }
' file
#### FAILED JOBS IN XXX #####
1> ABCD failed in the project XXX
2> ILKD failed in the project XXX
3> DFG failed in the project XXX
4> TYU failed in the project XXX

#### COMPILED JOBS IN  XXX#####

1> HJK Job is in compiled state in the project XXX
2> HOI Job is in compiled state in the project XXX

This is printing as expected in the file. But its not changing inside that file..Is there any option make that change inside the file itsel

------ Post updated at 06:08 AM ------

Thanks a lot Rudic..Its working as expected.I redirect it to a temp file and overwrite the temp file with actual one

Hi All, Now my requirement is change, but almost similar to the above one. But using the above code it is not working.I tried different option but its not giving correctly.

My input file is given below

#### RUNNING JOBS IN SITE#####

#### NOT RUNNING JOBS IN SITE#####

1> XXX Job is Running in the project WQW
2> SSS Job is Running in the project WQW
3> DDD Job is Running in the project WQW
4> EEE Job is Running in the project WQW
5> FFF Job is not running(Failed/Stopped/Reset/Not Compiled/Compiled) in the project WQW

My expected output file is given below

#### RUNNING JOBS IN SITE#####

1> XXX Job is Running in the project WQW
2> SSS Job is Running in the project WQW
3> DDD Job is Running in the project WQW
4> EEE Job is Running in the project WQW

#### NOT RUNNING JOBS IN SITE#####

1> FFF Job is not running(Failed/Stopped/Reset/Not Compiled/Compiled) in the project WQW

I tried this code as given below, but it didn't work

awk '
NR == 1         {print
                 next
                }
! NF            {next
                }
/is running/        {$1 = ++FCNT ">"
                 print
                 next
                }
                {$1 = ++CCNT ">"
                 C[CCNT] = $0
                }
END             {print ORS "#### NOT RUNNING JOBS CCM SITE#####" ORS
                 for (c=1; c<=CCNT; c++) print C[c]
                }
'

Appreciate your help here

/is running/ doesn't match is Running .

1 Like

I tried with is Running also, but didn't work

------ Post updated at 03:32 AM ------

Its working, Actually there was a minor issue in my code :slight_smile:

1 Like