LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   To have separator IFS on a condition (https://www.linuxquestions.org/questions/programming-9/to-have-separator-ifs-on-a-condition-4175684132/)

BudiKusasi 10-18-2020 12:12 PM

IFS is to be two bytes value in Bash
 
How to set IFS to two bytes value in Bash ?

Code:

IFS=',;'
will delimit each part of which resides between two simple , and/or simple ; instead of ,; as a delimiter
How is emulated/workaround way, so that gives a solution? Thanks before

NevemTeve 10-18-2020 12:48 PM

You could try sed:
Code:

sed 's/,;/@/g' |
while IFS='@' read -r W; do
    echo "$W"
done


boughtonp 10-18-2020 05:33 PM

Bash doesn't have any options to support multi-character IFS.

Either replace it with a character guaranteed not to appear, and use that character for IFS (as per NevemTeve's post), or use a tool which does support multi-character separators, like Awk.

Depending on what you need to do, either FS or RS may be more appropriate...
Code:

echo 'abc,def,;ghi;jkl,;mno,pqr' | awk -vFS=",;" -vOFS="---" '{print $1 , $2 , $3}'
echo 'abc,def,;ghi;jkl,;mno,pqr' | awk -vRS=",;" '{print}'


BudiKusasi 10-23-2020 05:45 PM

To have separator IFS on a condition
 
Is there any way or workaround resorts to correctly have separator IFS on a condition that
it's a certain character only if not preceded by another certain character ?

Let e.g. separator must be `;` not being preceded by `\`
or e.g. separator must be `|` not being preceded by `\`

IFS= # ... ??
# ?

NevemTeve 10-24-2020 01:21 AM

No, IFS doesn't support regular expression; try some scripting language like Perl/PHP/Python/awk.

grail 10-24-2020 02:38 AM

So obviously you didn't like any of the answers provided here,
however the answer is still the same that IFS uses a single character at all times. You can assign multiple but it will stop once it finds a matching character in its list.

As above, you need to pick a language that has native regex. Bash does have a regex part to it but then you would be looking to do the line separating using your own function.

pan64 10-24-2020 03:53 AM

additionally you may download the source of bash and implement this feature.
but need to take care about the original behavior too: https://bash.cyberciti.biz/guide/$IFS

astrogeek 10-24-2020 01:07 PM

Please post your question only once to allow participants to more easily follow the development of the discussion and prevent duplication of effort. Asking about two character IFS, or IFS with one character that follows another looks like the same question so your two threads have been merged into one.


All times are GMT -5. The time now is 07:13 AM.