LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to extract only certain part from string? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-extract-only-certain-part-from-string-4175443900/)

raheel_com88 01-02-2013 10:30 PM

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.

shivaa 01-02-2013 11:16 PM

Try:
Code:

awk -F="/" '{print $2}' infile.txt

grail 01-02-2013 11:20 PM

And what have you tried? What language (bash, perl, awk, etc) are you trying to use?

raheel_com88 01-03-2013 12:10 AM

How to extract only certain part from string?
 
grail: I am using bash and also using awk.
shivaa: Your suggested method is not working.

shivaa 01-03-2013 12:37 AM

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.

trijit 01-03-2013 01:19 AM

Quote:

Originally Posted by raheel_com88 (Post 4861747)
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.

shivaa 01-03-2013 02:00 AM

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.


All times are GMT -5. The time now is 09:42 AM.