how to sort text file and split into smaller files
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how to sort text file and split into smaller files
Hi,
I want to be able to sort a big file which is in the format below. So i want to sort it by ip address then split the file by the ipaddress so i will end up with multiple file containing records for one ip only in each of them... i came across the sort command but not sure how to split the file with the ipaddress as criteria..
thx
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:22: Team "TERRORIST" triggered "Terrorists_Win" (CT "4") (T "2")
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:22: World triggered "Round_End"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:26: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "aaaa ti p envi coupe oli" (dead)
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:27: "theEnd<5><STEAM_ID_LAN><TERRORIST>" triggered "Spawned_With_The_Bomb"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:29: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "salope vans"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:22: "x3non<2><STEAM_ID_LAN><TERRORIST>" killed "noob<8><STEAM_ID_LAN><CT>" with "galil"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:22: "TaZ<4><STEAM_ID_LAN><CT>" killed "x3non<2><STEAM_ID_LAN><TERRORIST>" with "awp"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:33: World triggered "Round_Start"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:34: "payne<2><STEAM_ID_LAN><TERRORIST>" say " kill oli get 1000 pt XD"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:34: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "ti p atane oli"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:27: "TaZ<4><STEAM_ID_LAN><CT>" killed "NuLL<9><STEAM_ID_LAN><TERRORIST>" with "awp"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: "TaZ<4><STEAM_ID_LAN><CT>" killed "Emo|Jpol<6><STEAM_ID_LAN><TERRORIST>" with "deagle"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: Team "CT" triggered "CTs_Win" (CT "13") (T "5")
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: World triggered "Round_End"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:39: "theEnd<5><STEAM_ID_LAN><TERRORIST>" say "twa vans?"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:32: "x3non<2><STEAM_ID_LAN><TERRORIST>" say_team "ok" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:33: "noob<8><STEAM_ID_LAN><CT>" say "wawa" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:34: "x3non<2><STEAM_ID_LAN><TERRORIST>" say_team "aster" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:35: World triggered "Round_Start"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:45: "oLi<3><STEAM_ID_LAN><TERRORIST>" say "lol lash sa? :P"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:36: "NuLL<9><STEAM_ID_LAN><TERRORIST>" say_team "pa P bon ditou :S"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:51: "la vie | Ishikawa<6><STEAM_ID_LAN><CT>" say "koter?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:51: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "OLI"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:54: "oLi<3><STEAM_ID_LAN><TERRORIST>" say "wa?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:54: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "apache dir toi rode to team"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:57: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "li vini dan 5min"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:50: "TaZ<4><STEAM_ID_LAN><CT>" killed "x3non<2><STEAM_ID_LAN
This sorts the infile first, the awk will put a specific line into a file named $2 (which is the ip adres). Awk can handle multiple field seperators (I used <> and : in this example).
A sample run looks like this:
Code:
$ cat sort.split.infile
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:22: Team "TERRORIST" triggered "Terrorists_Win" (CT "4") (T "2")
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:22: World triggered "Round_End"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:26: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "aaaa ti p envi coupe oli" (dead)
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:27: "theEnd<5><STEAM_ID_LAN><TERRORIST>" triggered "Spawned_With_The_Bomb"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:29: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "salope vans"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:22: "x3non<2><STEAM_ID_LAN><TERRORIST>" killed "noob<8><STEAM_ID_LAN><CT>" with "galil"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:22: "TaZ<4><STEAM_ID_LAN><CT>" killed "x3non<2><STEAM_ID_LAN><TERRORIST>" with "awp"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:33: World triggered "Round_Start"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:34: "payne<2><STEAM_ID_LAN><TERRORIST>" say " kill oli get 1000 pt XD"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:34: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "ti p atane oli"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:27: "TaZ<4><STEAM_ID_LAN><CT>" killed "NuLL<9><STEAM_ID_LAN><TERRORIST>" with "awp"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: "TaZ<4><STEAM_ID_LAN><CT>" killed "Emo|Jpol<6><STEAM_ID_LAN><TERRORIST>" with "deagle"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: Team "CT" triggered "CTs_Win" (CT "13") (T "5")
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: World triggered "Round_End"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:39: "theEnd<5><STEAM_ID_LAN><TERRORIST>" say "twa vans?"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:32: "x3non<2><STEAM_ID_LAN><TERRORIST>" say_team "ok" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:33: "noob<8><STEAM_ID_LAN><CT>" say "wawa" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:34: "x3non<2><STEAM_ID_LAN><TERRORIST>" say_team "aster" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:35: World triggered "Round_Start"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:45: "oLi<3><STEAM_ID_LAN><TERRORIST>" say "lol lash sa? :P"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:36: "NuLL<9><STEAM_ID_LAN><TERRORIST>" say_team "pa P bon ditou :S"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:51: "la vie | Ishikawa<6><STEAM_ID_LAN><CT>" say "koter?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:51: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "OLI"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:54: "oLi<3><STEAM_ID_LAN><TERRORIST>" say "wa?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:54: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "apache dir toi rode to team"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:57: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "li vini dan 5min"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:50: "TaZ<4><STEAM_ID_LAN><CT>" killed "x3non<2><STEAM_ID_LAN
$ ./sort.split.sh sort.split.infile
$ ls -l 4*
-rw-r----- 1 druuna internet 1631 Oct 8 12:24 41.212.144.207
-rw-r----- 1 druuna internet 1370 Oct 8 12:24 41.212.158.233
$ cat 41.212.144.207
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:22: Team "TERRORIST" triggered "Terrorists_Win" (CT "4") (T "2")
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:22: World triggered "Round_End"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:26: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "aaaa ti p envi coupe oli" (dead)
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:27: "theEnd<5><STEAM_ID_LAN><TERRORIST>" triggered "Spawned_With_The_Bomb"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:29: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "salope vans"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:33: World triggered "Round_Start"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:34: "(o.0)_.!.<7><STEAM_ID_LAN><CT>" say "ti p atane oli"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:34: "payne<2><STEAM_ID_LAN><TERRORIST>" say " kill oli get 1000 pt XD"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:39: "theEnd<5><STEAM_ID_LAN><TERRORIST>" say "twa vans?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:45: "oLi<3><STEAM_ID_LAN><TERRORIST>" say "lol lash sa? :P"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:51: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "OLI"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:51: "la vie | Ishikawa<6><STEAM_ID_LAN><CT>" say "koter?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:54: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "apache dir toi rode to team"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:54: "oLi<3><STEAM_ID_LAN><TERRORIST>" say "wa?"
RECV <41.212.144.207:27015>: L 10/07/2007 - 13:25:57: "- IceBladder -<4><STEAM_ID_LAN><CT>" say "li vini dan 5min"
$ cat 41.212.158.233
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:22: "TaZ<4><STEAM_ID_LAN><CT>" killed "x3non<2><STEAM_ID_LAN><TERRORIST>" with "awp"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:22: "x3non<2><STEAM_ID_LAN><TERRORIST>" killed "noob<8><STEAM_ID_LAN><CT>" with "galil"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:27: "TaZ<4><STEAM_ID_LAN><CT>" killed "NuLL<9><STEAM_ID_LAN><TERRORIST>" with "awp"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: "TaZ<4><STEAM_ID_LAN><CT>" killed "Emo|Jpol<6><STEAM_ID_LAN><TERRORIST>" with "deagle"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: Team "CT" triggered "CTs_Win" (CT "13") (T "5")
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:29: World triggered "Round_End"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:32: "x3non<2><STEAM_ID_LAN><TERRORIST>" say_team "ok" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:33: "noob<8><STEAM_ID_LAN><CT>" say "wawa" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:34: "x3non<2><STEAM_ID_LAN><TERRORIST>" say_team "aster" (dead)
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:35: World triggered "Round_Start"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:36: "NuLL<9><STEAM_ID_LAN><TERRORIST>" say_team "pa P bon ditou :S"
RECV <41.212.158.233:27015>: L 10/07/2007 - 13:23:50: "TaZ<4><STEAM_ID_LAN><CT>" killed "x3non<2><STEAM_ID_LAN
The awk statement is altered. It now searches for lines that have RECV in them, ignoring all others. If a line matches, everything from character 30 to end is printed (removing the RECV <xx.xxx.xxx.xxx:99999>: part).
but i want to add a number infront of the logfiles so that the output files are like this:
10000.111.111.11.11.log
10001.200.111.11.21.log
10002.111.111.11.31.log
10003.111.111.11.41.log
10004.111.111.11.51.log
10005.111.111.11.61.log
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.