LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2020, 12:12 PM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
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
 
Old 10-18-2020, 12:48 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You could try sed:
Code:
sed 's/,;/@/g' |
while IFS='@' read -r W; do
    echo "$W"
done
 
1 members found this post helpful.
Old 10-18-2020, 05:33 PM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,601

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
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}'
 
1 members found this post helpful.
Old 10-23-2020, 05:45 PM   #4
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Original Poster
Rep: Reputation: 15
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= # ... ??
# ?
 
Old 10-24-2020, 01:21 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
No, IFS doesn't support regular expression; try some scripting language like Perl/PHP/Python/awk.
 
2 members found this post helpful.
Old 10-24-2020, 02:38 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 10-24-2020, 03:53 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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
 
Old 10-24-2020, 01:07 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
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.
 
  


Reply

Tags
bash script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Been bitten by IFS yet again: 'mkinitrd_command_generator.sh --run' fails if IFS not set to space. luvr Slackware 3 06-20-2019 04:09 PM
check Negative egrep condition in an if condition novicunix Programming 5 02-02-2013 12:52 AM
Bash: when an empty IFS does not work like a default IFS (info) catkin Programming 13 04-19-2012 09:40 AM
Bash command separator/arguments separator reverse Programming 11 12-02-2007 09:21 PM
using OR condition in if condition Fond_of_Opensource Linux - Newbie 2 10-20-2006 12:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration