If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Sorting Text File Lines by Characters in Bash

Started by vietnamstyle89, Oct 13, 2024, 01:55 AM

Previous topic - Next topic

vietnamstyle89Topic starter

Greetings.

Imagine you've got a log file with a massive dataset of 100500 entries. Each entry is formatted like this:

12345:qidhiqe
321:wuid
23456:348wic
1:4823
34567:2y7dfg73
3278:293874
18278:edui
45678:38fdh83

The task at hand is to filter and sort entries using a Bash script, specifically targeting those where the segment before the colon is 5 characters or fewer. Once filtered, these entries should be saved into a new file. How would you achieve this using terminal commands?
  •  


malota

You can leverage the power of awk and sort to tackle this task efficiently. Use the following command:
awk -F: 'length(\$1) <= 5' logfile.txt | sort > filtered_sorted_log.txt
This command uses awk to split each line by the colon and checks if the length of the segment before the colon is 5 characters or fewer. The matching entries are then piped into sort to organize them in ascending order, and the output is redirected to a new file. This approach is efficient and straightforward, allowing you to handle massive datasets with minimal overhead.
  •  

WambLyday

One method involves leveraging the power of grep with Perl-compatible regular expressions:

grep -oP '^[^:\n]{5}(?=\:)' file_name
Here, file_name refers to the dоcument containing the data that needs to be processed.

Alternatively, you can employ a Bash script to accomplish the same task:

#!/bin/bash
while IFS=: read -r prefix _; do
  if [ ${#prefix} -eq 5 ]; then
    echo "$prefix"
  fi
done < "\$1"

Save this script into a file, make it executable, and run it by providing the source file's name as an argument, like so: script file_name. In this command, script is the file containing the Bash program, and file_name is the dоcument with your data.

Another approach involves using sed for stream editing:

sed -n -E '/^[^:]{5}\:/p' test_file | awk -F':' '{print \$1}'
Each of these methods offers a unique way to slice and dice your data, allowing you to choose the one that best fits your workflow.
  •  

gzaamywinend

You might craft your regex-fu to achieve the following:

awk '/^[0-9]{5}\:[0-9]{3}/' file_name
While it's not my place to dictate your learning path, diving into the world of regex through some solid literature or insightful articles might be a game-changer for you.

To tackle both conditions simultaneously, consider this approach:
awk '/^[0-9]{5}\:[0-9]{3}/' filename > out_file && awk '/^[^:\n]{5}(?=\:)/' filename >> out_file
Here's a quick breakdown of the components:

filename is your source data file.
out_file is where you'll stash your filtered results.
> will overwrite out_file with the initial command's output.
>> appends the subsequent findings to the existing content in out_file.
&& ensures the second command runs only if the first one succeeds.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...