LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-16-2008, 07:38 AM   #1
snkmchnb
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Rep: Reputation: 1
Question Need Help Extracting A Substring


I'm writing a shell script that uses cat to combine a bunch of csv files into one. What I'm trying to do is extract a 16 digit number with a known prefix out of each line and display that only. I've got it using grep to display only the lines with the number but how would you suggest extracting/displaying just the number from the string? I'm sure it could/should be done with awk or sed but I'm relatively knew to the commands and I'm horrible at regex.

Any help would be appreciated. Thanks!
 
Old 09-16-2008, 08: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 the pattern you pass to grep matches just the part of the line you are interested in, you can use the -o option to the GNU grep to output just that part of the matching lines.

If not, there are a multitude of ways to extract parts of a line. cut, awk, sed, perl. Which is best depends on the nature of the problem, your platform and personal preference. If you describe in more detail what you are trying to do (i.e. include example input, output and metrics such as data set size), I'm happy to make a suggestion.
 
Old 09-16-2008, 08:25 AM   #3
harry2006
Member
 
Registered: Aug 2006
Location: /hawaii/honolulu/downtown
Distribution: Fedora 10[Cambridge] and Ubuntu 9.04[Jaunty]
Posts: 201

Rep: Reputation: 30
a more detailed explanation of ur prob ll put more light on how to attack the problem...but as matthew rightly suggested u can make use of simple comands like cut...keep life simple...i'll do teh job...but again it all depends on ur requirement...so if u r still stuck with the prob...state ur problem more clearly...
 
Old 09-16-2008, 08:28 AM   #4
snkmchnb
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by matthewg42 View Post
If the pattern you pass to grep matches just the part of the line you are interested in, you can use the -o option to the GNU grep to output just that part of the matching lines.

If not, there are a multitude of ways to extract parts of a line. cut, awk, sed, perl. Which is best depends on the nature of the problem, your platform and personal preference. If you describe in more detail what you are trying to do (i.e. include example input, output and metrics such as data set size), I'm happy to make a suggestion.
the grep -o worked perfectly, thanks!
 
  


Reply



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
How to substring santhoshv Linux - Newbie 3 09-09-2008 05:09 AM
C++ problem extracting a substring - extra character Nylex Programming 4 07-22-2007 11:21 AM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 11:31 AM
Need Help Extracting A Substring eur0dad Linux - General 6 09-21-2006 10:34 PM
to get a substring dimah Programming 1 06-22-2006 06:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:28 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