Splitting a file

I have a source file where a record starts with # entry-id: followed by a number.
i want to move the record in file 1 iff the second row is

"dn: uid=.*,ou=perm,dc=mssb,dc=com"

and the record to file 2 if contents of second row anything else

# entry-id: 1
dn: uid=abcd,ou=perm,dc=mssb,dc=com
Entry3
Entry4
Entry5
Entry6
Entry7
Entry8

# entry-id: 2
dn: uid=efgh,ou=term,dc=mssb,dc=com
Entry3
Entry4
Entry5

# entry-id: 3
dn: uid=xidydh,ou=perm,dc=mssb,dc=com
Entry3
Entry4

so Entry id 1 and Entry id 3 should be in file1 and entry-id2 to file 2

Any attempts/ideas/thoughts from your side?

---------- Post updated at 11:33 ---------- Previous update was at 11:11 ----------

Howsoever, try

awk '{print $0 ORS > "file" 2- ($2 ~ /dn: uid=.*,ou=perm,dc=mssb,dc=com/)}' RS="" FS="\n" file
2 Likes

Thanks RudiC.

I wanted to sort the file as well based on

# entry id 1

value. Using sort command but unable to do so.

How about

awk '{gsub (FS, "\t")}1' RS="" FS="\n" file1 | sort -k3,3n |  awk '{gsub ("\t", FS); print $0 ORS > "FILE" 2- ($2 ~ /dn: uid=.*,ou=perm,dc=mssb,dc=com/)}'  FS="\n"

Hi RudiC,
in code

awk '{print $0 ORS > "file" 2- ($2 ~ /dn: uid=.*,ou=perm,dc=mssb,dc=com/)}' RS="" FS="\n" file

Kindly explain what is the purpose of highlighted part-, also this code is not working in my linux.

The ~ match operation will result a boolean result, 0 or 1, which is subtracted from 2 to yield the desired file names. Please note that the use of boolean results in arithmetic operations is not commonly accepted.

Try the script again with the file name concatenation in parentheses.

---------- Post updated at 13:20 ---------- Previous update was at 13:19 ----------

And, "is not working" is quite poor an error description to analyse and track down the error.

It's working fine Mr RudiC, may be i was doing some small mistake while running code.
Thanks,