1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Splitting String Using Delimiter in unix

Discussion in 'Unix' started by bashamsc, Feb 15, 2013.

  1. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    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.
     
    Last edited: Feb 15, 2013
    shabbir likes this.
  2. David Ledger

    David Ledger New Member

    Joined:
    Oct 21, 2010
    Messages:
    10
    Likes Received:
    5
    Trophy Points:
    0
    Location:
    Gloucester UK
    A much easier solution is

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

    David
     
    shabbir likes this.

Share This Page