LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-21-2012, 04:55 AM   #1
VinRoh
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Rep: Reputation: Disabled
Question Substring with specific condition


hi ,

I am trying to substring a line which is got while reading property file using shell scripting.
The line is something like this:
file\:/aaa/ccc/dd.jar, \^M

I am trying to get the substring as /aaa/ccc/dd.jar

Tried using awk for the same but not getting the right command.

Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-21-2012, 05:55 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Assuming you want the string between : and , then:
Code:
awk -F '(:)|(,)' '{ print $2 }' input.txt
 
Old 02-21-2012, 06:44 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Doesn't have to be quite so flash
Code:
awk -F"[:,]" '{print $2}' file
 
2 members found this post helpful.
Old 02-21-2012, 06:55 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by grail View Post
Doesn't have to be quite so flash
Code:
awk -F"[:,]" '{print $2}' file
Ah, grail, I love your elegant minimalism and fluency

A point of style, though -- I would use single quotes for bash wherever double quotes are not necessary.
 
1 members found this post helpful.
Old 02-21-2012, 10:55 PM   #5
VinRoh
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Smile

Hey thanks for your replies!!

Just for anyones interest ,I tried another way of doing it :

cut -d: -f2 | cut -d, -f1
 
Old 02-22-2012, 10:21 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If you can get the string into a bash variable, there are quite a few options available.

Use parameter substitution:

Code:
string='file\:/aaa/ccc/dd.jar, \^M'
substring=${string#*:}
substring=${substring%,*}
echo "$substring"
Use bash's regex ability inside [[..]], and the resulting BASH_REMATCH array:

Code:
string='file\:/aaa/ccc/dd.jar, \^M'
re=':(.+),'
[[ $string =~ $re ]] && echo "${BASH_REMATCH[1]}"
Use IFS to set the field delimiters, and break it up with an array, or read.

Code:
string='file\:/aaa/ccc/dd.jar, \^M'
IFS=':,'
array=( $string )
echo "${array[1]}"
unset IFS
Code:
string='file\:/aaa/ccc/dd.jar, \^M'
IFS=':,' read -r _ substring _ <<<"$string"
echo "$substring"
More on bash string manipulation here.


Quote:
Originally Posted by catkin View Post
A point of style, though -- I would use single quotes for bash wherever double quotes are not necessary.
I would generally agree with this, but the Japanese keyboard has the single-quote mark on the "7" key, and are not all that convenient to type, so it's personally easier for me to use double-quotes (on the "2" key, slightly easier to reach) most of the time.
 
  


Reply


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
print lines form nth line to mth line which fulfill specific condition cristalp Programming 4 11-07-2011 08:39 AM
ls -- changing color of a substring for specific file types linav Linux - General 4 06-20-2008 01:16 PM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 12:31 PM
to get a substring dimah Programming 1 06-22-2006 07:31 AM
perl: substring extraction after specific char markus1982 Programming 2 10-06-2002 06:48 AM


All times are GMT -5. The time now is 04:14 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