LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-31-2012, 02:46 PM   #1
verona
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
Regex problem in script


I have a file full of pathnames eg

//blah/blah/blah/123.txt
//blah/blah/blah/blah/456.txt
//blah/blah/789.txt


and I need to end up with :

//blah/blah/blah/123.txt 123
//blah/blah/blah/blah/456.txt 456
//blah/blah/789.txt 789

I need to achieve this with a command that I can put into a script, eg sed, but despite my fiddling I haven't managed to achieve this.

Really appreciate any help.
 
Old 07-31-2012, 02:57 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Here you can find what you need http://www.gnu.org/software/sed/manu...s_0022-Command If you still got problems post what you have tried. And then people can point you in the right direction.
 
Old 07-31-2012, 03:19 PM   #3
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by verona View Post
and I need to end up with :

//blah/blah/blah/123.txt 123
//blah/blah/blah/blah/456.txt 456
//blah/blah/789.txt 789
Indeed, sed seems a good way of doing this.

In the pattern part of sed 's/pattern/substitution/':

- Find a regular expression that matches the filenames you're interested in (such as 456.txt).
- Use escaped parentheses to store the part of the matches you're interested in.

In the substitution part:

- Use \0 to print the entire match and \1 to print the stored part.

I'm not sure about your exact requirements, but maybe this is a place to start from: cat input | sed 's/\/\([0-9]*\)\.txt$/\0 \1/g'

Regards
dmdeb
 
Old 08-01-2012, 02:49 PM   #4
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Code:
sed -r 's,.*/([0-9]{3})\.txt,\0 \1,' inputfile
 
Old 08-01-2012, 02:56 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Note that ruario's code only matches a 3-digit filename with a .txt extension - if you have different requirements, you're gonna have to be more specific about what exactly you want.
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Check Argument with REGEX mrm5102 Programming 9 04-10-2012 10:07 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
execute script shell with regex as argument eguider Linux - Newbie 2 12-15-2009 07:26 PM
performing regex on shell script variable? ocicat Programming 3 07-17-2007 02:10 PM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 06:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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