LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 03: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, 04:19 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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 04:21 AM.
 
Old 08-19-2008, 12: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, 01:30 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

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


Reply

Tags
cut, parag, paragkalra, query



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 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
Open Source Consulting | Domain Registration