Search code examples
perlsplitcutuniq

Perl way of using cut,grep,uniq


I am running a Perl script inside a Perl script, and the output of the script is something like this

# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/angyvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/aqmmgvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/69aqvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/69aavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/qqmavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/wttvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/9aaqvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/9aaavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/a1vqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
# aAM axac: cmt /tm9/raaqHRW /myqq1fqq1er/maaavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a1.1.e.1234567891008547.ahqva
[qNFq] cqqq aarvqca qntereatqng 1qatvqnqth. heqqq 1qatvqnqth.
[qNFq] cqqq aarvqca qntereatqng fmtvqnqth. heqqq fmtvqnqth.
[qNFq] cqqq aarvqca qntereatqng gcaavqnqth. heqqq gcaavqnqth.
[qNFq] cqqq aarvqca qntereatqng gq9vqnqth. heqqq gq9vqnqth.
[qNFq] cqqq aarvqca qntereatqng hvavqnqth. heqqq hvavqnqth.
[qNFq] cqqq aarvqca qntereatqng q9vqnqth. heqqq q9vqnqth.
[qNFq] cqqq aarvqca qntereatqng 9vaqvqnqth. heqqq 9vaqvqnqth.
[qNFq] cqqq aarvqca qntereatqng mcaavqnqth. heqqq mcaavqnqth.
[qNFq] cqqq aarvqca qntereatqng mcqrvqnqth. heqqq mcqrvqnqth.
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/angyvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/aqmmgvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/69aqvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/69aavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/qqmavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/wttvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/9aaqvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/9aaavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/a1vqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
# aAM axac: cmt /tm9/raawV9h /myqq1fqq1er/maaavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a4.1.e.1234567891014523.ahqva
[qNFq] cqqq aarvqca qntereatqng 1qatvqnqth. heqqq 1qatvqnqth.
[qNFq] cqqq aarvqca qntereatqng fmtvqnqth. heqqq fmtvqnqth.
[qNFq] cqqq aarvqca qntereatqng 6caavqnqth. heqqq 6caavqnqth.
[qNFq] cqqq aarvqca qntereatqng 6q9tvqnqth. heqqq 6q9tvqnqth.
[qNFq] cqqq aarvqca qntereatqng q9vqnqth. heqqq q9vqnqth.
[qNFq] cqqq aarvqca qntereatqng 9vaqvqnqth. heqqq 9vaqvqnqth.
[qNFq] cqqq aarvqca qntereatqng mcaavqnqth. heqqq mcaavqnqth.
[qNFq] cqqq aarvqca qntereatqng mcqrvqnqth. heqqq mcqrvqnqth.
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/angyvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/aqmmgvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/69aqvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/69aavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/qqmavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/wttvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/9aaqvqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/9aaavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/a1vqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva
# aAM axac: cmt /tm9/raaaRX6 /myqq1fqq1er/maaavqnqth/rmn/cqqqa/newfqq1er/111111111/ty9a7.1.e.1234567891020719.ahqva

I need to get an output like this

tm9/raaqHRW /111111111/ty9a1
tm9/raawV9h /111111111/ty9a4
tm9/raaaRX6 /111111111/ty9a7

I am currently writing the output of the script to a file and doing as follows,

grep "cmt /tm9" output_file | cut -d'/' -f2,3,9,10 | cut -d '.' -f1 | uniq

Is there a Perl way to do this?


Solution

  • perldoc perlrun will teach you how to fish:

    • -n to read in a file line by line
    • -a to split each line into @F (and emulate cut)
    • list slices to access items stored in @F to emulate cut's -f flag
    • regex conditional to emulate grep
    • use hash keys to emulate uniq