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 01-02-2013, 10:30 PM   #1
raheel_com88
LQ Newbie
 
Registered: Nov 2012
Location: Islamabad, Pakistan
Posts: 17

Rep: Reputation: Disabled
How to extract only certain part from string?


Hi,

I want to know how I can extract only certain part from string? For Example I want to extract only "UpToDate" after "/" from "ds:UpToDate/UpToDate"?
Quick Replies are highly appreciated.
 
Old 01-02-2013, 11:16 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Try:
Code:
awk -F="/" '{print $2}' infile.txt
 
Old 01-02-2013, 11:20 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
And what have you tried? What language (bash, perl, awk, etc) are you trying to use?
 
Old 01-03-2013, 12:10 AM   #4
raheel_com88
LQ Newbie
 
Registered: Nov 2012
Location: Islamabad, Pakistan
Posts: 17

Original Poster
Rep: Reputation: Disabled
How to extract only certain part from string?

grail: I am using bash and also using awk.
shivaa: Your suggested method is not working.
 
Old 01-03-2013, 12:37 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
shivaa: Your suggested method is not working..
No, it is working fine, if infile.txt contains line ds:UpToDate/UpToDate.
Anyway, once mention sample input data or file and what you've tried, so then people can suggest you appropriate solutions.
 
Old 01-03-2013, 01:19 AM   #6
trijit
Member
 
Registered: Sep 2010
Location: Kolkata
Distribution: Ubuntu
Posts: 34

Rep: Reputation: 3
Quote:
Originally Posted by raheel_com88 View Post
Hi,

I want to know how I can extract only certain part from string? For Example I want to extract only "UpToDate" after "/" from "ds:UpToDate/UpToDate"?
Quick Replies are highly appreciated.

Try this:

cat file.txt | cut -d":" -f2 | cut -d"/" -f2

FYI: assuming the string provided is the content of file.txt

It's a lengthy one but should work.

Last edited by trijit; 01-03-2013 at 01:20 AM. Reason: edit
 
Old 01-03-2013, 02:00 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Try this:

cat file.txt | cut -d":" -f2 | cut -d"/" -f2
It can simply be done as:
Code:
cut -d"/" -f2 file.txt
@raheel_com88, provide sample input file/data and what you've tried.

Last edited by shivaa; 01-03-2013 at 02:12 AM.
 
  


Reply

Tags
extract, string


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
extract part of a string and reinsert back into original tlin Programming 5 02-15-2012 01:38 PM
Extract part of a string based on regex winairmvs Linux - Software 5 02-14-2011 12:56 PM
[SOLVED] extract part of string himu3118 Programming 4 05-07-2010 07:13 AM
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 07:18 AM


All times are GMT -5. The time now is 02:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration