Issue
I have a file: queueN.txt, that looks like this:
uid=524357(aali95) gid=524357(aali95) groups=524357(aali95),611684(com_perkinsd),695433(bios420)
uid=524534(aalman4) gid=524534(aalman4) groups=524534(aalman4),426634(chem_shared)
uid=748146(abaner29) gid=748146(abaner29) groups=748146(abaner29)
uid=583567(abench2) gid=583567(abench2) groups=583567(abench2),875029(cs494_paka)
uid=570875(abhima4) gid=570875(abhima4) groups=570875(abhima4),875029(cs494_paka)
uid=603522(abokifa) gid=603522(abokifa) groups=603522(abokifa),750403(cme_abokifa),831682(vasphpc)
uid=747491(aboyap2) gid=747491(aboyap2) groups=747491(aboyap2),10(wheel),516955(acr_admin)
uid=500092(adavar2) gid=500092(adavar2) groups=500092(adavar2),10513(domain_users),426636(cme_shared),605853(cme_sarakad),831682(vasphpc)
uid=543988(adench2) gid=543988(adench2) groups=543988(adench2),545740(phys_hyowon)
uid=738525(aditya06) gid=738525(aditya06) groups=738525(aditya06),705095(acr_workshop)\
How would I extract strings only from the first and the last set of parentheses so that my output looks like this:
aali95 bios420
aalman4 chem_shared
abaner29 abaner29
abench2 cs494_paka
...
I am guessing the solution would be something of a loop with a sed command inside...
for f in `cat queueN.txt`; do cat ${f} | sed 's...'; done > queue.txt
Solution
Avoid for loop with cat
.
This sed
should work for you:
sed -E 's/^[^(]*\(([^)]+)\).*\(([^)]+)\).*/\1 \2/' file
aali95 bios420
aalman4 chem_shared
abaner29 abaner29
abench2 cs494_paka
abhima4 cs494_paka
abokifa vasphpc
aboyap2 acr_admin
adavar2 vasphpc
adench2 phys_hyowon
aditya06 acr_workshop
We match using:
^
: Start[^(]*
: Match 0 or more of any char except(
\(
: Match a(
([^)]+)
: Match 1+ of any char that is not)
and capture in group #1\)
: Match a)
.*
: Match 0 or more of any characters\(
: Match a(
([^)]+)
: Match 1+ of any char that is not)
and capture in group #2\)
: Match a)
.*
: Match any text
And replace using:
\1 \2
: Replace with back-reference #1 then a space then back-reference #2
Answered By - anubhava Answer Checked By - Senaida (WPSolving Volunteer)