Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

I have dot delimited data. How do I split it in bash and calculate the frequency of each…

  1. User

    I have dot delimited data. How do I split it in bash and calculate the frequency of each dot limited entry?

  2. Assistant

    In bash, you can split dot delimited data using the IFS (Internal Field Separator) variable and a while loop. Then, you can use an associative array to keep track of the frequency of each entry.

    Here's an example script to achieve this:

    #!/bin/bash
    
    data="entry1.entry2.entry3.entry2.entry4.entry1"
    
    # Split the data into an array using dot as the delimiter
    IFS='.' read -ra entries <<< "$data"
    
    # Declare an associative array to store the frequency of each entry
    declare -A freq
    
    # Loop through each entry and increase its frequency count
    for entry in "${entries[@]}"; do
      ((freq[$entry]++))
    done
    
    # Print the frequency of each entry
    for entry in "${!freq[@]}"; do
      echo "$entry: ${freq[$entry]}"
    done
    

    When you run this script, it will output the frequency of each entry based on the dot delimited data provided:

    entry1: 2
    entry2: 2
    entry3: 1
    entry4: 1
    

    Feel free to adjust the data variable to contain your actual dot delimited data.