LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-17-2014, 07:43 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Question Split string into 2, from 2nd occurance of a character in bash


I have a script, which is having a string as command line argument.

1= "ABC_MY_STING_anything_I_dont_care"

I need to have 2 variables

var1 = "ABC_MY"
var2 = "STRING_anything_I_dont_care"

Seperation is based on the "2nd" occurance of underscore.


I found answers on how to do it in python but I'm using bash.

Using tr doesnt look to solve this.

I tried using IFS=_ but that splits my string in a way I dont want, I need only 2 parts.

I know how to make regex but not getting how to split it this way.

anything_something_rest_of_the_things
[a-z A-Z 0-9]_[a-z A-Z 0-9]_


Background:
Currently I use mutt -s $1 to send email

But I need to split my email into 2 parts, var1 I'll use as subject and var2 I'll use as message body.
 
Old 07-17-2014, 08:22 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,028

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
maybe something like
Code:
s1=`echo $1 | cut -d _ -f -2`
s2=`echo $1 | cut -d _ -f 3-`

Last edited by schneidz; 07-17-2014 at 09:14 PM.
 
Old 07-17-2014, 08:36 AM   #3
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
maybe something like
Code:
s1=`echo $1 | cut -d _ -2`
s2=`echo $1 | cut -d _ 3-`
Thanks schneidz, but it says
Quote:
cut: invalid option -- '2'
Try `cut --help' for more information.
cut: you must specify a list of bytes, characters, or fields
 
Old 07-17-2014, 11:21 AM   #4
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
s1=`echo $1 | cut -d _ -f1-2`
s2=`echo $1 | cut -d _ -f3-`
Did what I exactly wanted, but still can anyone tell what -f1-2 meant for bash

d tells that delimiter is _ and what about f
 
Old 07-17-2014, 11:40 AM   #5
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Now that I have my subject in the s1 and body in s2. I cant find out a way to make s2 as the body. I wish to use mutt but, it expects file for making its body. Can anyone help on this.
 
Old 07-17-2014, 11:47 AM   #6
Regex Ninja
LQ Newbie
 
Registered: Jul 2014
Posts: 2

Rep: Reputation: Disabled
Sorry, never used Linux, so someone might need to help edit this.
As far as regex-matching the text-strings, here's a sed example:
GNU version 4.0.7

sed -r -n 's/([^_]*_[^_]*)_(.*)/AnyTextHere \1/p' $1
sed -r -n 's/([^_]*_[^_]*)_(.*)/AnyTextHere \2/p' $1

They only output below vars if $1 meets your 2nd-occurrence requirement.
\1 = AllTextBefore2ndOccurenceOf_
\2 = AllTextAfterwards

You can always combine the \1\2 replacements, editing AnyTextHere with your needed apps (separated by &&) and then pipe to 'sed e' to auto-run the replacement text-string as a commandline.

Last edited by Regex Ninja; 07-17-2014 at 12:16 PM.
 
Old 07-20-2014, 03:15 PM   #7
Regex Ninja
LQ Newbie
 
Registered: Jul 2014
Posts: 2

Rep: Reputation: Disabled
IF $1 is of format: Mail_Subject_Body Text
Try something like below, but remove ;e for testing
Again, someone may need to help edit this for a bash shell

sed -r 's#([^_]*_[^_]*)_(.+)#echo -e "\2" | mutt -s "\1" user@earth.com#;e' $1

It creates text-lines as:
echo -e "Body Text" | mutt -s "Mail_Subject" user@earth.com

If that's not what you need, mod the replacement & test again.
Once in the correct format, add ;e back-in to execute the result.


** Edit **
Removed sed's -n and s///p option to make it simpler.
Its fine so long as $1 will always be in the right format

If not, give e a linematch like /@/e (for lines with email or whatever)
Or just pipe the changed $1's only with something like:

sed -n -r 's#([^_]*_[^_]*)_(.+)#echo -e "\2" | mutt -s "\1" user@earth.com#p' $1 | sed e

Last edited by Regex Ninja; 07-21-2014 at 01:01 PM.
 
Old 07-22-2014, 02:59 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 806

Rep: Reputation: 186Reputation: 186
See the manual for cut
http://unixhelp.ed.ac.uk/CGI/man-cgi?cut

Delimiter is _ (underscores) in your case.
-f is the field list (as found using the delimiter you specified).
-f1-2 means fields 1 to 2.
-f3- means from field no 3 onwards.

OK
 
Old 07-24-2014, 07:58 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 806

Rep: Reputation: 186Reputation: 186
echo $s2 | mutt $s1 should be OK. If not, then substitute mutt with mailx.

OK
 
Old 07-24-2014, 08:19 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,122

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
that looks overkill and overcomplicated for me. we can use IFS:
Code:
string="ABC_MY_STING_anything_I_dont_care"
IFS=_ read -a a <<< "$string"
var1=${a[0]}_${a[1]}
var2=${string#${var1}_}

echo $var1
echo $var2
I think you use some script to construct this string and I suggest you to use different separator between the two fields. That will simplify this thing....

Last edited by pan64; 07-24-2014 at 08:23 AM. Reason: additional tip
 
  


Reply

Tags
bash, nth, regex, split


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace 2nd occurance of a special character after nth occurance of a delimiter from dhiru_b25@rediffmail.com Programming 6 11-02-2013 12:27 AM
[SOLVED] how to split a string of characters after nth character samanp Programming 2 08-15-2012 03:59 PM
[SOLVED] How to escape bash-special character in a bash string? fantasy1215 Programming 12 03-10-2012 10:45 AM
[SOLVED] split a string into array in bash xeon123 Linux - Newbie 8 03-17-2011 12:16 PM
Split a string on newlines (bash) rose_bud4201 Programming 7 04-14-2005 02:58 PM


All times are GMT -5. The time now is 12:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration