6

StackOverflow locked my account. I'm hoping someone here might be kind enough to help me with a bash script I'm "bashing" my head with. Actually, it's zsh on MacOS if it makes any difference.

I have an input file. Four lines. No blank lines. Each of the four lines has two strings of text delimited by a tab. Each string on either side of the tab is either one word with no spaces or a bunch of words with spaces. Like this (using <tab> as a placeholder here on Devrant for where the tab actually is)

ABC<tab>DEF
GHI<tab>jkl mno pq
RST<tab>UV
wx<tab>Yz

I need to open and read the file, separate them into key-value pairs, and put them into an array for processing. I have this script to do that:

# Get input arguments
search_string_file="$1"
file_path="$2"

# Read search strings and corresponding names from the file and store in arrays
search_strings=()
search_names=()

# Read search strings and corresponding names from the file and store in arrays
while IFS= read -r line || [[ -n "$line" ]]; do
echo "Line: $line"
search_string=$(echo "$line" | awk -F'\t' '{print $1}')
name=$(echo "$line" | awk -F'\t' '{print $2}')
search_strings+=("$search_string")
search_names+=("$name")
done < "$search_string_file"

# Debug: Print the entire array of search strings
echo "Search strings array:"
for (( i=0; i<${#search_strings[@]}; i++ )); do
echo "[$i] ${search_strings[$i]} -- ${search_names[$i]}"
done

However, in the output, I get the following:

Line: ABC<tab>DEF
Line: GHI<tab>jkl mno pq
Line: RST<tab>UV
Line: wx<tab>Yz
Search strings array:
[0] --
[1] ABC -- DEF
[2] GHI -- jkl mno pq
[3] RST -- UV

That's it. I seem to be off by one because that last line...

Line: wx<tab>Yz

never gets added to the array. What I need it to be is:

[0] ABC -- DEF
[1] GHI -- jkl mno pq
[2] RST -- UV
[3] wx -- Yz

What am I doing wrong here?

Thanks.

Comments
  • 1
    Hmm, the lines seem to be ok. Can't you just do +1 in the last loop?
  • 0
    @retoor Thanks for responding. I did this:

    # Debug: Print the entire array of search strings

    echo "Search strings array:"

    for (( i=0; i<${#search_strings[@]}; i++ )); do

    index_display=$((i + 1)) # Add 1 to the loop index for display

    echo "[$i] ${search_strings[$i]} -- ${search_names[$i]}"

    done

    but it's still got the same output.
  • 3
    Just migrate to python

    import pathlib
    for line in pathlib.Path("file.txt").read_text().split("\n"):
    print("--".join(line.split("\t")))

    Three understandable lines
  • 1
    Ok, thanks. I’ll give that a try. It’s a part of a larger script that I’ll also have to rewrite. But this part acts consistently bad whether on its own or part of the whole script.
  • 2
    So essentially INI but without section headers and kv pairs are separated by a \t instead of an =?
  • 2
    @Ranchonyx Correct. I took @retoor’s advice and converted it to python. Worked nearly right away. Now I’m trying to figure out how to get Google to let my python script authenticate to secure SMTP via my own Google Workspace account so I can email the output somewhere. Apparently, for that, you have to have 2FA enabled and use an App Password, which now appears to be deprecated now that Google supports passkeys in place of 2FA. No way in :(. Might have to set up local SMTP or SendGrid and pray for deliverability.
  • 2
    @stackodev Ah so it works already. I just recently wrote an INI parser from scratch and published it as an NPM module, some of the code does some magicks I thought might had been helpful.

    But since this isn't really INI, a lot of my code would've been useless.

    In any case, glad to hear you've got it working, now, good luck with the smtp.
  • 2
    @stackodev wow, would not expect that you would really migrate to python. Awesome! It seems smtp is build in, into python.

    https://docs.python.org/3/library/...

    Edit: probably python has some package to connect to the Google services as well
  • 1
    @retoor Ooo! I’ll look for that. Yeah, I had just taken a Youtube course on Python a few months ago, so when you suggested it I was like, “Oh! Why didn’t I think of that?” Thanks!
  • 1
    @stackodev minor package install tut. Create a virtual env using:
    python3 -m venv venv
    And activate it using
    source venv/bin/activate

    Now you can install packages in a nice isolated environment that you can just delete when you fuck up.
  • 1
    MacOS sadly has an ancient Bash version from the last century...

    So no ZSH solution, a pure bash one.

    https://pastebin.com/9kT6NrHz

    Line: ABC DEF
    Line: GHI jkl mno pq
    Line: RST UV
    Line: wx Yz

    [0] wx --Yz
    [1] GHI --jkl mno pq
    [2] ABC --DEF
    [3] RST --UV

    is the output.
  • 1
    Associative Array just to remove duplicates.
  • 0
    @IntrusionCM Awesome, thanks! Yeah, zsh is definitely different. I’ll keep this snippet for future reference.
  • 0
    @stackodev NP.

    Yeah, I love Bash. Not everything's easy, but since Bash 4 with a bit of tinkering one can do a lot of shit without relying on sed / awk / ...
  • 1
    I didn't know that you can be kicked from StackOverflow for noobing out on formulating your questions to conform to the rules. That doesn't seem very inclusive considering that this industry cluster attracts a lot of people from the spectrum.
  • 1
    @Oktokolo Looking back on my questions, there’s really nothing I did differently from anyone else. I guess they just don’t want users anymore.
  • 1
    using devrant as a second SO 😆 This Legit makes me wanna create a shittier SO clone. Could probably drive some ad revenue.

    Writing and read code on devrant is horrendous
Add Comment