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
RudiC
March 15, 2016, 6:33am
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.
RudiC
March 16, 2016, 6:01am
4
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"
looney
March 16, 2016, 7:41am
5
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.
RudiC
March 16, 2016, 8:20am
6
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.
looney
March 16, 2016, 8:34am
7
It's working fine Mr RudiC, may be i was doing some small mistake while running code.
Thanks,