Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/articles/unix/)
-   -   Splitting String Using Delimiter in unix (http://www.go4expert.com/articles/splitting-string-using-delimiter-unix-t29491/)

bashamsc 15Feb2013 17:30

Splitting String Using Delimiter in unix
 
Now let us try to do same in Unix as we have discussed in the following Article - Splitting String Using Delimiter in Oracle

In Unix it is pretty simple. We can use below commands to split

Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f1 -d"|"
or

Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $1}
Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f2 -d"|"
or

Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $2}
Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f3 -d"|"
or

Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $3}
Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f4 -d"|"
or

Code:

echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $4}
But this not universal. Now we will try to write a universal code which work for all as we have seen in above thread.

Let us suppose we have a file called file_string.txt and which has below data

Code:

GO 4 EXPERT|99999|20130101|20131231
ABCD|345|99999|123|2034123
XYZXYZ|345|99999|123|2034123|20131231

We need to write output to another file called file_split.txt

The output will look as below

Code:

GO 4 EXPERT
99999
20130101
20131231
ABCD
345
99999
123
2034123
XYZXYZ
345
99999
123
2034123
20131231

Now we can observe that the first sting has 3 delimiters , second has 4 and third has 5.

Below code will split the string's and writes to a file.

Code:

while read line   
do   

        counter=`echo $line | tr -cd  '|' |wc -c`
       
        counter=`expr $counter + 1`
       
        cnt=1
       
        while [[ $counter -gt 0 ]] 

        do

         
        split=`echo $line |cut -f$cnt -d"|"`

        counter=`expr $counter - 1`

        cnt=`expr $cnt + 1`

        echo $split>>file_split.txt

        done                               
       
                                               

done <file_string.txt


In above tr command will work similar to regexp_replace and wc -c acts similar to length in oracle.

The tr -cd '|' will delete the characters and displays only delimiter and wc -c will count the characters.

As we see no. of strings are no. of delimiter's plus one. So we have used counter + 1 in the code as we can see same in oracle code also.

David Ledger 11Mar2013 18:11

Re: Splitting String Using Delimiter in unix
 
A much easier solution is

$ echo "GO 4 EXPERT|99999|20130101|20131231" | tr "|" "\n"

David


All times are GMT +5.5. The time now is 07:11.