LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-19-2008, 04:47 AM   #1
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Rep: Reputation: 31
Simple "CUT" Query


Hello All,

I have a simple Cut query. I want to extract 17195 from the following and store it in a variable.

Quote:
ABCD PQRS LMNO XYZW (2.3.17195)
Would someone like to enlighten me.
 
Old 08-19-2008, 05:19 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you have this text in a variable, and are using bash or a similar shell, you can do it without needing to use cut or sed - this will be more efficient as spawning a new process is much more expensive than manipulating strings inside the shell itself.
Code:
s="ABCD PQRS LMNO XYZW (2.3.17195)"
s=${s##*.}
echo "I have: ${s%)}"
will output:
Code:
I have: 17195
See the "Parameter Expansion" section of the bash manual page for an explanation and other, similar methods.

Last edited by matthewg42; 08-19-2008 at 05:21 AM.
 
Old 08-19-2008, 01:43 PM   #3
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Original Poster
Rep: Reputation: 31
This was what I was looking for:
Quote:
cat /tmp/test.txt | cut -f 3 -d "." | cut -c 1-5
Anyways...thanks matthewg42
 
Old 08-19-2008, 02:30 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
..but matthewg42's is faster and simpler code.
 
  


Reply

Tags
cut, parag, paragkalra, query


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
Simple "sed" query paragkalra Programming 16 07-20-2008 08:01 AM
bash: "cut" and "case" help please tredegar Linux - Newbie 14 06-26-2008 05:31 PM
"Cut & Paste" To and From Shell TatforTit Linux - Newbie 5 03-05-2006 12:34 PM
display only first line in "cut" ALInux Linux - General 4 02-08-2006 07:11 AM
"Cut the Gordian Knot" of ATI SlackerLX LinuxQuestions.org Member Success Stories 27 01-01-2005 11:35 AM


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

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