# How to split a data assigned to a variable

**URL:** <https://community.unix.com/t/how-to-split-a-data-assigned-to-a-variable/285764>\
**Category:** Shell Programming and Scripting\
**Created:** [April 5, 2011, 1:50am UTC](https://community.unix.com/t/how-to-split-a-data-assigned-to-a-variable/285764 "2011-04-05T01:50:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jassz](https://community.unix.com/letter_avatar/jassz/32/5_5575768a8748004e209b776fc1b2916d.png) [@Jassz](https://community.unix.com/u/Jassz)\
**Post date:** [April 5, 2011, 1:50am UTC](https://community.unix.com/t/how-to-split-a-data-assigned-to-a-variable/285764/1 "2011-04-05T01:50:23Z")

</div>

The requirement is, there is a log file which contains a huge data. i need to get a particular field out of it by searching with another field.

ex:

2011-03-28 13:00:07,423 [http-8080-9] : millis=231 q={ call get\_data\_account(?,?,?,?,?) }, params=[1:xxxxxx,2:bbbbbbb3:ccccccc,4:dddddd,5:null]

i need to search for the word "get\_data\_account" in file and from that i need to get the field "xxxxxx" and store it in a variable.

this "get\_data\_account" will be present more than one time and and there will be different "XXXX" values.

i used it in a for loop for all the files and below is the code.

```nohighlight
IFS='\n'
for Search in "$(less "$logfile" | grep get_data_account)"
do
 accountId=`echo "$Search" | awk -F ':' '{print $8}' | cut -d ',' -f1`
 
 echo $accountId 

done

```

but when i echo the accountId i get the below  
accountId='1234  
5678  
9012  
2344'

but when i use it another for loop to display it it is taken as a single string not as a single accountId's and display it in a single line same as the above output.

i have tried both with and with out IFS both are giving the same output.

---

<div class="post-metadata">

**Author:** ![cgkmal](https://community.unix.com/letter_avatar/cgkmal/32/5_5575768a8748004e209b776fc1b2916d.png) [@cgkmal](https://community.unix.com/u/cgkmal)\
**Post date:** [April 5, 2011, 2:23am UTC](https://community.unix.com/t/how-to-split-a-data-assigned-to-a-variable/285764/2 "2011-04-05T02:23:51Z")

</div>

Hi Jassz,

Using the sample you put with awk, using ":" as Field Separator, It's needed to print $5(you're printing $8) to get the parameter you need as follow:

```nohighlight
echo "2011-03-28 13:00:07,423 [http-8080-9] : millis=231 q={ call get_data_account(?,?,?,?,?) }, params=[1:xxxxxx,2:bbbbbbb3:ccccccc,4:dddddd,5:null]
2011-03-28 13:00:07,423 [http-8080-9] : millis=231 q={ call get_data_account(?,?,?,?,?) }, params=[1:yxx1xxxx,2:bbbbbbb3:ccccccc,4:dddddd,5:null]" | 
awk -F":" '/get_data_account/{print substr($5,1,index($5,",")-1)}'
xxxxxx
yxx1xxxx

Or if you have grep above in your code for "get_data_account" simply it could be used:
 awk -F":" '{print substr($5,1,index($5,",")-1)}'

```

Regards
