Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split String in shell script while reading from file

Tags:

bash

shell

I have a following script which should read line by line from a ".properties" file and then tokenize it on base of "=" delimiter and store values into two variables and then display it. But I am not getting understanding of how to tokenize it and then store it in two different variables and then use it for further purposes.

Following script works fine in reading the file line by line but i need help in implementing the logic of splitting the string.

"Properties File"

FNAME=John
LNAME=Lock
DOB=01111989

Script

#!/bin/bash
echo "FileReading Starts"
while read line
do 
    value=$line
    echo $value
    #Tokenize Logic
    property=sample
    property_value=sample
    echo $property
    echo $property_value
done <testprop.properties

echo "Finish"
like image 653
Umair Khalid Avatar asked Dec 16 '14 08:12

Umair Khalid


People also ask

How do you split text in shell?

In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.

How do I strip a string in shell script?

`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command. Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command.

What is $1 and $2 in shell script?

These are positional arguments of the script. Executing ./script.sh Hello World. Will make $0 = ./script.sh $1 = Hello $2 = World. Note. If you execute ./script.sh , $0 will give output ./script.sh but if you execute it with bash script.sh it will give output script.sh .


2 Answers

Try this :

#!/bin/bash

while IFS='=' read -r col1 col2
do 
    echo "$col1"
    echo "$col2"
done <testprop.properties

IFS is the Input Filed Separator.

But instead of parsing the file (like fedorqui said), you can source the file and accessing the variables directly:

source testprop.properties
echo "$FNAME"

From $ LANG=C help source :

source: source filename [arguments]
Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.

Last but not least, use more quotes ! See http://mywiki.wooledge.org/Quotes, http://mywiki.wooledge.org/Arguments and http://wiki.bash-hackers.org/syntax/words.

like image 87
Gilles Quenot Avatar answered Oct 22 '22 04:10

Gilles Quenot


The IFS can be used to set field separator values to read

Example

while IFS="=" read line val
do 
   echo $line : $val; 
done < inputFile 

Gives output as

FNAME : John
LNAME : Lock
DOB : 01111989

Internal Variable

$IFS
    internal field separator
    This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.
like image 25
nu11p01n73R Avatar answered Oct 22 '22 04:10

nu11p01n73R