• Home
  • Docs
  • About
  • Resume
  • Services
    • Testimonials
  • Contact
Humbug
Bah! Humbug!
Home 2010 Bash Tricks: Split / Cut a String with Multi Character Delimiters

Bash Tricks: Split / Cut a String with Multi Character Delimiters

  • Leave a comment
  • Tweet
Pratik Sinha | July 31, 2010

Its simple enough to split a string when it has single character delimiters using the cut command. However cut doesn’t support multi-character delimiters. Here’s a sample script to demonstrate how to split strings with multi-character delimiters.

#!/bin/bash
#Inputs to the script, the delimiter, and the string itself
D="<>"   #Multi Character Delimiter
string="abcd<>efgh<>ijkl<>mn op<>qr st<>uv wx<>yz" #String with delimiters

#Split the String into Substrings
sList=($(echo $string | sed -e 's/'"$D"'/\n/g' | while read line; do echo $line | sed 's/[\t ]/'"$D"'/g'; done))
for (( i = 0; i < ${#sList[@]}; i++ )); do
  sList[i]=$(echo ${sList[i]} | sed 's/'"$D"'/ /')
done

#Output the Split String
echo No of SubStrings - ${#sList[@]}
for (( i = 0; i < ${#sList[@]}; i++ )); do
  echo ${sList[i]}
done

In the above script, the string is being split and then stored in the sList array. You can access the individual substrings using ${sList[0]}, ${sList[1]}, ${sList[2]} etc. The output for the above script is:

@$ sh cut.sh
No of SubStrings - 7
abcd
efgh
ijkl
mn op
qr st
uv wx
yz

For those who are wondering how to cut using the cut command. Here’s an example

echo "a|b|c|d|e" |  cut -d '|' -f 1
echo "a|b|c|d|e" |  cut -d '|' -f 2
echo "a|b|c|d|e" |  cut -d '|' -f 3
etc..

Posted in Code-Snippets | Tagged bash, cut, delimiter | Leave a response

  • Tweet
Logging In...

Profile cancel

Sign in with Twitter Sign in with Facebook
or

Not published

 

« Previous Next »

Search

Recommend on Google
  • RSS
Follow @free_thinker

Get the latest posts delivered straight to your inbox.

Categories

  • Bookmarks
  • Code-Snippets
  • Guides
  • Info
  • Software
  • Testimonials
  • Tips-N-Tricks
  • Updates

Recently Popular

  • Make English the Default Language for Google Chrome Search
  • Parse Simple XML Files using Bash – Extract Name Value Pairs and Attributes
  • Kill/Quit and Restart Plasma on KDE
  • Bash Tricks: Split / Cut a String with Multi Character Delimiters
  • Enable SPDY in Firefox 11 on Ubuntu 12.04 beta
  • Utility to Send Commands or Data to Other Terminals (tty/pts)
  • A Sample Loop in XSL, Alternative for While, For Loops
  • Bash Tricks: Create variables dynamically using some eval magic
  • Kickstrap – CSS Framework: Enhancements for Bootstrap
  • WordPress Plugin: Cleaner WordPress Editor – Trying To Make WordPress Editing a Pleasure

Latest Tweets

  • 2 unexploded bombs found under Ho Chi Minh City house - Approximately 800,000 tons of bombs and mines still present humbug.in/z/9i #fb 4 weeks ago
  • 3 Vietnamese bloggers charged over their writing | Fox News humbug.in/z/9g #fb 1 month ago
  • Implement strong WiFi encryption the easy way with hostapd - Using WPA2-Personal with individual keys for each user humbug.in/z/9e 1 month ago
  • Soon, wonder vaccine that will kill 90% of cancers - Health - DNA humbug.in/z/9d 1 month ago
  • PHP like str_replace function in C: This post is part of a series of posts where I want to document a bunch of C f... humbug.in/z/9c 1 month ago

Services Offered

  • Embedded Linux Systems and Services
  • Gateway Routers (Frontend and Backend)
  • Free and Open Source Software Solutions
  • Network Monitoring Solutions
  • Device Driver Development


Mobile and Web Analytics

Recent Comments

  • Haakon Dahl commented on Make English the Default Language for Google Chrome Search
    (1 weeks ago)
  • pawan commented on Wordpress Error: The plugin generated 1 characters of unexpected output during activation
    (1 weeks ago)
  • simon commented on Detect Mobile Browser using Server Side Includes (SSI)
    (2 weeks ago)
  • hakatagroup commented on Twitter Weekly Updates between 2012-04-19 and 2012-04-25
    (3 weeks ago)

Tags

Address bar android awk bash C css custom post type debug Default Browser DNS dreamhost English Facebook firefox Google iphone JavaScript jquery KDE keyboard shortcuts Kubuntu Launchpad Linux Mozilla Firefox p2 page template pdf php plugin PPA python ruby ruby-on-rails Ruby1.9.2 Search Results SNMP theme tweets Twitter Ubuntu United States update-alternatives Vietnam wordpress xsl

Copyright © 2012 Humbug.

Powered by WordPress and Hybrid.