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
or
or
or
or
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
We need to write output to another file called file_split.txt
The output will look as below
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.
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.
In Unix it is pretty simple. We can use below commands to split
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f1 -d"|"
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $1}
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f2 -d"|"
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $2}
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f3 -d"|"
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $3}
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|cut -f4 -d"|"
Code:
echo "GO 4 EXPERT|99999|20130101|20131231"|awk -F"|" '{print $4}
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
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.
shabbir
likes this
