LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: 19

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,800
Blog Entries: 4

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

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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: 19

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,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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: 35

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,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:37 AM.

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
Open Source Consulting | Domain Registration