-1

I have a text string here:

lorem ipsum dolor: sit amet

and i want to split it into 2 variables: col1 and col2 based on string "dolor:" delimiter.

col1 is assigned with the first string:

lorem ipsum dolor:

and col2 is assigned with the second string:

sit amet

how do i do that from the terminal?

oguz ismail
  • 1
  • 16
  • 47
  • 69
himekami
  • 1,419
  • 3
  • 13
  • 21
  • 1
    Does this answer your question? [How do I split a string on a delimiter in Bash?](https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash) – toydarian Jul 29 '20 at 06:21
  • not really. I need to do it interactively in the terminal. But i learned from that as well. Thanks bro – himekami Jul 29 '20 at 06:39

1 Answers1

3

Bash makes things simple with parameter expansions that allow trimming from the front (beginning) or back (end) of a string. For example:

s="lorem ipsum dolor: sit amet"  ## full string
b="${s##*dolor: }"               ## remove everything from front through "dolor: "
a="${s% $b}"                     ## remove everything saved in $b from back + space

Example:

s="lorem ipsum dolor: sit amet"
b="${s##*dolor: }"
a="${s% $b}"
printf "a: %s\nb: %s\n" "$a" "$b"
a: lorem ipsum dolor:
b: sit amet

Where $a and $b contain the desired strings. The parameter expansions can be found in man bash but the ones to trim from front or back are summarized as:

${var#pattern}      Strip shortest match of pattern from front of $var
${var##pattern}     Strip longest match of pattern from front of $var
${var%pattern}      Strip shortest match of pattern from back of $var
${var%%pattern}     Strip longest match of pattern from back of $var

(note: pattern can contain the normal globbing characters such as '*')

Let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • My God. that is all? here i am fiddling with grep and what not. thanks – himekami Jul 29 '20 at 06:38
  • *Parameter Expansions* are mighty powerful text processing tools in bash. Best part about it -- they are built-in and no additional subshells are spawned. Makes them safe to use in a loop. Good luck with your scripting ! – David C. Rankin Jul 29 '20 at 06:43
  • If you really wanted to torment yourself, you could split the string with `awk` into two separate lines and then set `$IFS` to split on newlines and feed both strings into an array. It gets ugly quick. If you just want to test the split with `awk` you can use `awk -F: '{print $1":\n"substr($2,2)}' <<< "lorem ipsum dolor: sit amet"` -- but again that will spawn an unnecessary subshell `:)` – David C. Rankin Jul 29 '20 at 06:45
  • You can use a second form of expansion (*parameter expansion with substring replacement*) to do the same thing -- but unlike substring removal, these are not POSIX compliant, `a="${s/:*/}:"` and `b="${s/*: /}"` will do the same thing. Basically the expansion is `${var/pattern/replace}` to replace the 1st occurrence of pattern or `${var//pattern/replace}` to replace all occurrences. In the examples the `replace` part is empty which just removes `pattern`. (you can omit the `'/'` before an empty pattern and replace is taken as empty) – David C. Rankin Jul 29 '20 at 06:55