The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing string using specific delimiter primp Shell Programming and Scripting 8 09-22-2008 01:46 AM
string replacement in a sequence of characters raoscb UNIX for Dummies Questions & Answers 3 07-08-2008 06:00 AM
Adding a sequence string to a file MrPeabody Shell Programming and Scripting 5 07-21-2006 03:50 PM
Searching for a specific string in an argumnet dinplant Shell Programming and Scripting 1 03-11-2002 03:28 PM
Search all files for specific string sureshy UNIX for Dummies Questions & Answers 4 03-06-2002 12:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-20-2008
LisaS LisaS is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 59
cmd sequence to find & cut out a specific string

A developer of mine has this requirement - I couldn't tell her quickly how to do it with UNIX commands or a quick script so she's writing a quick program to do it - but that got my curiousity up and thought I'd ask here for advice.

In a text file, there are some records (about half of them) that have a specific string, say "ABC" followed by a 15 digit number, always at least 2 leading zeros. In rows that have this, it will appear twice, identically.
I essentially want to cut out these 18 chars into a file of their own. But, they are not in a fixed column position within the file.

Logically, the task is:
a) find the rows with ABC00
b) get the position of that first A
c) cut starting at that position for 18 characters and write to a new file.

example data:
ab cdefgABC000000000012345ABC000000000012345sadlfk
abcde fgABC000000000012346ABC000000000012346sadlfk
abc defgghi jklmn1349d5sadlfk
abcdef sldkfdgABC000000000056789ABC000000000056789abcdlkdfj134239d


and so on.

Desired output
ABC00000000012345
ABC00000000012346
ABC00000000056789

Thanks for having a look.
Lisa
  #2 (permalink)  
Old 10-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink One approach

Lisa,
There are probably many, but here is one approach --

Code:
> sed "s/ABC[0-9][0-9]/~+&/" file220 | tr "~" "\n" | grep "+" | cut -c2-19
ABC000000000012345
ABC000000000012346
ABC000000000056789
  #3 (permalink)  
Old 10-20-2008
LisaS LisaS is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 59
wow! that's slick - and it worked on my data stream so 1000s of thanks. Now, the ethical dilemma, do I just give it to the developer as if I did it or do I 'fess up that I asked for help.

Lisa
  #4 (permalink)  
Old 10-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink under the assumption that no programming is ever truly unique and created...

You found a solution and verified it works.

Most every problem has already been pondered and solved, so there truly are no "new" answers. Ha ha

Back to the initial problem, the creative use of sed to place extra characters and then tr to convert them so a grep and cut can extract them -- is one useful process to pull apart data records.

Let him think you were the genius.
  #5 (permalink)  
Old 10-21-2008
vimes vimes is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 46
Inevitably, a perl approach

Code:
perl -ne '/(ABC00\d{13})/ && print "$1\n"' list.txt
Sponsored Links
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0