If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > how to delete characters in a file except pattern

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-17-09, 09:49
Balaji ramaswamy Balaji ramaswamy is offline
Registered User
 
Join Date: Apr 2009
Posts: 2
how to delete characters in a file except pattern

hi all,

anyone of you help me what command to be used in unix to delete all characters before pattern starts in every line of a text file

for example, the pattern is "user".

the file has line like,

1234|needhelp234|user234

from the above line, onle user234 to be retained remaining characters to be removed.

thanks
balaji
Reply With Quote
  #2 (permalink)  
Old 04-20-09, 08:40
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,258
sed 's/.*\(user*\)/\1/'
Reply With Quote
  #3 (permalink)  
Old 04-22-09, 07:39
Balaji ramaswamy Balaji ramaswamy is offline
Registered User
 
Join Date: Apr 2009
Posts: 2
Talking

thanks..it is done...
Reply With Quote
  #4 (permalink)  
Old 04-22-09, 09:19
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,770
..and the solution you found was?

..so that others in your position can find out what to do...
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 04-22-09, 10:51
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by healdem
..and the solution you found was?
I think pdreyer had the right solution mark though he may of missed a dot before the * ie
Code:
sed 's/.*\(user.*\)/\1/'
Those brackets remember everything from user onwards and put it into a "variable" called \1 then the whole string is replaced by this variable.
Reply With Quote
  #6 (permalink)  
Old 06-17-09, 15:44
Lerac Lerac is offline
Registered User
 
Join Date: Jun 2009
Location: South Africa
Posts: 33
There are many ways to skin a cat..

As an alternative, you can also use the "cut" command for the same result :

echo "1234|needhelp234|user234" | cut -f 3 -d "|"
or
cat /my/file | cut -f 3 -d "|"

The cut -f 3 -d "|" says to retain only field number 3 (-f3) of the input, and assume that all fields are delimited with the "|" character (-d"|").
Reply With Quote
Reply

Thread Tools
Display Modes

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