Skip to main content
Solved

extract string with backslashes


Forum|alt.badge.img+5

Sorry for a noob question but I have a hard time to extract a string from another string and I'm sure one of you geeks has a simple answer for this.

I have a string like "domainnameusername" that I extract from a plist using defaults read. I got the domainname with cut -d '' -f1 but I can't get the username. I tried cut -d '' -f2 and cut -d '' -f3 and cut -d '' -f2 and I always end with an empty string. I'm sure it's all about escaping but I don't know enough yet to figure this out.

Thanks,
Dirk

Best answer by tron_jones

string='domainnameusername'

username=echo $string | cut -d '' -f3

echo $username

View original
Did this topic help you find an answer to your question?

3 replies

Forum|alt.badge.img+9
  • Contributor
  • 94 replies
  • Answer
  • March 13, 2014

string='domainnameusername'

username=echo $string | cut -d '' -f3

echo $username


Forum|alt.badge.img+5
  • Author
  • Contributor
  • 50 replies
  • March 13, 2014

I knew it was easy for you.

I tired to do everything in one line and that didn't work. Extracting the full string first to a temp variable and then using echo works perfectly. Lesson learned. Thanks!


Forum|alt.badge.img+12
  • Contributor
  • 529 replies
  • March 20, 2014

You can get echo to do all the work for you:

#!/bin/bash

string='domainname\\username'

username=`echo ${string##*\\}`
domainname=`echo ${string%%\\*}`

echo $username
echo $domainname

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings