Questions tagged [ifs]

IFS is a variable in Unix shells (Bourne, POSIX sh, bash, ksh, …) that controls how unescaped substitutions are split into words.

IFS (short for "input field separator" and often referred to as "internal field separator") is a variable in Unix shells (, , , , , , , , …) that controls how strings are split into multiple fields. Most notably, variable substitutions (e.g. $foo) which are not inside double quotes or in a few other protected locations are split into multiple fields based on the value of IFS; the same goes for command substitutions (e.g. $(foo)). Each character that is present in IFS is a field separator. The default value contains a space, a tab and a newline.

For other meanings of the acronym IFS, use the appropriate tag:

  • IBM Integrated file system
  • QNX Image Filesystem
  • ERP by Industrial and Financial Systems AB
293 questions
1
vote
1 answer

Reading file in while loop bash scripting

I've got this code which reads an example file of /etc/passwd: #!/bin/bash OLDIFS=$IFS IFS=$'\n' while read linea resto do echo $linea echo $resto if [[ $(echo $linea | cut -d: -f6 | egrep -c 'al-03-04') == 1 ]] …
cildoz
  • 436
  • 3
  • 14
1
vote
2 answers

Excel: IFS or Nested If Statements

I'm having an issue writing an IFS statement in Excel. This is what I would like to happen: User chooses from a list of 8 names in B1. Based on the selection of B1, in B2 the user will get a dropdown of a more refined list to choose from. This is…
Christina
  • 11
  • 2
1
vote
2 answers

Bash array from input using ISF

I have a string given in the manner of 1,3, 5, 7-10,... This needs to be put in an array of 1 3 5 7 8 9 10 so anywhere there is a minus sign then I need to add in the missing numbers. I am able to use IFS to split it into an array singling out…
Josh
  • 159
  • 2
  • 10
1
vote
1 answer

Bash IFS not splitting string correctly

I am having trouble getting IFS to split the string correctly based on the colon delimiter. It seems that the -e inside the string is being considered as an option instead of being considered as a literal…
agp
  • 363
  • 5
  • 13
1
vote
2 answers

Bash: set IFS to Space after specific character only?

I'm using IFS=', ' to split a string of comma-delimited text into an array. The problem is that occasionally one of the comma-delimited items contains a space following a :. The resulting array contains that item as two separate array elements. Is…
Urphänomen
  • 81
  • 1
  • 8
1
vote
3 answers

Split file content and store into array or iterate throgh file content by delimeter

I have below file say MemberFile.txt. which contains records separated by delimiter '#' which starts on newline and is a single character of that line. As such there are three records. 3RECORDSFILE # [FIRSTNAME ] FirstName01 [MIDDLENAME ]…
Rohit Borse
  • 75
  • 1
  • 10
1
vote
2 answers

In BASH, test if file exists does not work when filename in variable has single quotes

[Update] 1) I have a file (a.cfg) that contains lines like this: FILE;'/tmp/testfile';;;+;'Add this line';$;Y 2) In my script, I read this file line by line : while read line do ... done < a.cfg 3) When a line has been read, and it is not empty, I…
1
vote
3 answers

Assign lines to variables with separator in bash

I have a file built from a grep output and it looks like this : http://google.fr Pierre google http://test.fr -- http://yahoo.com Jean Yahoo http://test.fr -- I made a separator '--' for every 3 lines. I would like to assign every line to a…
Daniel R
  • 17
  • 3
1
vote
2 answers

Why does IFS not affect the length of an array in bash?

I have two specific questions about the IFS. I'm aware that changing the internal field separator, IFS, changes what the bash script iterates over. So, why is it that the length of the array doesn't change? Here's my…
makansij
  • 9,303
  • 37
  • 105
  • 183
1
vote
2 answers

Bash/Shell | How to prioritize quote from IFS in read

I'm working with a hand fill file and I am having issue to parse it. My file input file cannot be altered, and the language of my code can't change from bash script. I made a simple example to make it easy for you ^^ var="hey","i'm","happy,…
Guillaumedk
  • 35
  • 1
  • 7
1
vote
4 answers

Shell Script split concatenate and re-use

In Shell script I want to achieve something like below: str="india,uk,us,uae" I want to split it and concatenate each item as below and assign to some variable newstr = '-myParam="india" -myParam="uk" -myParam="us" -myParam="uae"' so that I can…
Ronnie
  • 302
  • 1
  • 4
  • 16
1
vote
2 answers

How do you split a string from shell-redirect or `read`?

I'm trying to split key value pairs (around an = sign) which I then use to edit a config file, using bash. But I need an alternative to the <<< syntax for IFS. The below works on my host system, but when i log in to my ubuntu virtual machine…
Simeon
  • 848
  • 1
  • 11
  • 34
1
vote
0 answers

How to feed while loop one slice at a time?

If I want to read a string and split it on a given delimiter, I can set IFS and store string slices in an array. Then I can do whatever I want with its items: while IFS=_ read -r -a myarray; do printf "%s -\n" "${myarray[@]}" done <<<…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1
vote
1 answer

Creating Arrays of Arrays in Bash and Sorting By Derived Values

I'm having issues with creating and sorting an array in Bash which takes its contents as lines from a command, takes certain parts of each line and operates on them before appending them to each line in the array. To clarify, the command "bogoutil…
g.grinovski
  • 23
  • 1
  • 7
1
vote
0 answers

What IFS should I use in my BASH for loop if my input command arguments are multiple lines?

I want to store each single, complete, multiline result of a pcregrep into a variable for each iteration of a for loop. Ex: for a in $(pcregrep -Moh 'regex_that_spans_multiple_lines'); do blah blah done However, I'm not sure what IFS I should use…
Nolan
  • 363
  • 2
  • 10