Categories
gnu/linux

awk tricks

some of the awk tricks listed here. might come in handy in any of my future shell scripts.

Q. extract information from a single line with some defined pattern like this: remember, it’s all a single line and awk works on columns. we need a way to figure out to split this one line to multiple lines. we will use RS operator from awk to achieve this.

"d891f157811444c0a7bf6ac527bc2edd" => "3982", "15b1372d7ede4114b18acaebf262c085" => "64748", "0416ee1f57b94cf3b9c981787e515da8" => "80039", "42b8141f554f463fa61c1c6eb3ef63a5" => "141299", "b47a840b26a542b09cf104a86f6a52c1" => "75677", "3c460cd6f49b4ccaac7f5cd29d2d02dd" => "270965", "b3283a7c48ac4852968823f7f19012ee" => "1359", "b9fa9ccd157542aa96d6ef5ad7e448fb" => "43063", "9f5fb55153744895a8828f745cb72838" => "27858", "r016seh0azds8e0d1v1x0daol1swmsnr" => "272207", "global-support@nu-infra.terminaltwister.com" => "137063", "a5d7a6c5ddcd4e209b781e69f68200b2" => "159745", "99d15f054fe84606b965fa56d3027d3d" => "166823", "version8-dev@wow.terminaltwister.com" => "227962", "93b00f4d0ba6415f862ae8ae78846931" => "188579", "1620e6c43c8a4cfbbee0626419b07c52" => "303212", "db19dfb731154bd1a0be23b002b0471d" => "18969", "73c3047621974a99a83e91babdd4a2ba" => "8812", "5f3a7e94dea249c39fbf4af99f767999" => "185693", "0109dc3e617c4c6a8811fdb430c9448e" => "24243", "84bd230f087e43eb8579e012ca266c74" => "51328", "e9b49a459bc54da1b7786e5032691bf0" => "1947", "ce1deca43e85462ca18be9e922a9bda1" => "103419", "90ce34de827d416080a5eeec32135f09" => "51328", "f74935d65ed540afb4427539a3a37a77" => "64748", "a586a835038146d6a2cbc99c1187882a" => "280460", "be182960a28b48aba98d9ab1b6e04e46" => "274340", "4fdedba1c71f4faa95ca3b2de3dda178" => "12696", "ap-toro-idtp@terminaltwister.com" => "251899", "43dda12a5a0147d29fcd3cf37ee45dbb" => "227803", "8fc677efeba341e5ab51decf8b2149b1" => "237094", "839d551b5ea34d1b86e92e271fe1ae09" => "306331", "e8ea688c399245219765b80ac054b305" => "300495", "b2a23e0ed95e4d7e9a2af645602b7236" => "159340", "b1f15be3f2804d0e857de717f93edcba" => "1482", "6c1583af100845e38fa944eb7449b203" => "10083", "455768b60feb4600ab8fd4c518678762" => "65426", "da4727b0c9404593bb98960b9c5d3f60" => "5801", "d3aa982919734705a0f55dd92c39287d" => "24243", "8c80c24c9b9c4af79f5ec652f1ea465f" => "5399", "c23663746ec04f0d8e27075043932d16" => "158227", "3787fa10404748d8ae3a9502957fd263" => "24243", "63d2cfc7043444c0819709de73c5561f" => "600", "0dbf54523af54260a0a302b32a7c7221" => "51328", "4d6da7094463485092df5a6c6a9785ca" => "175561", "0a123d24c854434cbd0ba7082b33d915" => "306892", "fbb80ec4e4a54d20b13e8192ac7e95cd" => "2445", "c372fb3bfb33426a9ad9e9e30695f06c" => "47734", "email-infra@terminaltwister.com" => "16362", "e22d33ba49604581927de2236e6356b9" => "24243", "442ad7099a95417c81a4fa5786a89729" => "24243", "e7ee6268e25741f98d3d46612a122da3" => "219862", "86b10abd087b46059f59fb3f6ade7401" => "218212", "22a188db99274f7cb9a171c44f51b2e8" => "122048", "5f02cf7fd24f44fd9bf6df97deaf0d68" => "298221", "d584cb7aeae14037aa48f263099530bc" => "47734", "115a64e1463a47f89e6d5ea89dcd3f9a" => "600", "24ef909b25084299b8b0ea5864ed22c8" => "300435", "2df28e1cb2f94d2f8034191205d7a73b" => "11703", "4934ebf2958b4b129f2bf42d71951f1a" => "255145", "995fb28a0a4b4ee8b87616ab29a13e32" => "111995", "6332f0b9e4cb4a169a72171799d12c52" => "51328", "airwatch@terminaltwister.com" => "295349", "r014bkkjlo48b65k3h1x0mvhd13t3efx" => "275177", "750b47d63dfa41fd8fd84a0944ed6227" => "47734", "cab63b1a49594dfe914e92f90b1d1a6b" => "7309", "3c3c5368619446b8acc7fa8b6b3d8638" => "17403", "c82d24454ce34fc0a8780e5de997823d" => "65606", "microsoft-scom-criticalmedium@terminaltwister.com" => "150487", "b4af1f0b0e1c4217ab5fefbb2694b970" => "4374", "f86fb46c899040689b4f3863453d6ea8" => "273935", "551faaf20e8f44a7801b3dbbb1ad3976" => "42157", "f9e0b60e1bf745068417763d7b9b6680" => "11862", "92e6550428b842b2933761f0c4f61287" => "158743", "7530b47405f14a47b050ab6da436fd5b" => "300219", "9212704fbce1499889018bf929883f83" => "2792", "ba@sigma-terminaltwister.com" => "165659", "31b7ceff04614f278824b6bfc0b8fda1" => "141155", "network@terminaltwister.com" => "22515", "6042b997084f4193aad94d1c84e15afa" => "253336", "2bb3f6a7ddee4bc686f287c3ae468eb9" => "150397"

save it to a file, say keys.txt and then

awk 'BEGIN{RS=","; FS="=> \""} {print substr($2, 1, length($2)-1)}' keys.txt > keys_1.txt

explanation: the RS is the record separator, which has been set to a comma (,) will define a newline in our case. After we have split this single line to multiple line, we can proceed using our regular Field Separator FS and do the job!

1 reply on “awk tricks”

Leave a Reply

Your email address will not be published. Required fields are marked *