How to delete all character before certain word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to delete all character before certain word
# 1  
Old 01-30-2009
How to delete all character before certain word

Hi,

For example, i have a string

"123 456 789 abc 111 222 333"

and I would like to delete all the characters before abc so that it becomes

"abc 111 222 333"

how can i do that in unix? using sed?

note: I actually don't know how many words/charachters before "abc", so the "cut" command should not work.

Thanks.
Andrew

Last edited by phandy; 01-30-2009 at 01:24 AM.. Reason: Use better title
# 2  
Old 01-30-2009
Code:
$ echo "123 456 789 abc 111 222 333" | sed 's/^.*abc/abc/'
abc 111 222 333
$

# 3  
Old 01-30-2009
Yes, it works! Many thanks Corona688Smilie
# 4  
Old 01-30-2009
Oh! I found that this command will delete until the last occurance instead of first, meaning if

Code:
$ echo "123 456 789 abc 111 abc 222 333" | sed 's/^.*abc/abc/'

will return "abc 222 333".

Is it possible to return "abc 111 abc 222 333" instead? Many thanks.
# 5  
Old 01-30-2009
Any idea?Smilie
# 6  
Old 01-31-2009
sed can't do this, but perl can:

Code:
$ echo "123 456 789 abc 111 abc 222 333" | perl -p -e 's/^.*?abc/abc/'
abc 111 abc 222 333
$

# 7  
Old 01-31-2009
Quote:
Originally Posted by Corona688
sed can't do this, but perl can:

Code:
$ echo "123 456 789 abc 111 abc 222 333" | perl -p -e 's/^.*?abc/abc/'
abc 111 abc 222 333
$

With sed you do something like:

Code:
$
$ echo "123 456 abc 111 abc 222 abc 333" | sed 's/[^abc]*\(abc.*\)/\1/'
abc 111 abc 222 abc 333
$
$

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Remove word before a character

Hi, I have a file which looks like this id integer, name string, create_dt date, I want to remove all words that are present before the character , My output should be id, name, create_dt, Thanks wah (2 Replies)
Discussion started by: wahi80
2 Replies

3. Shell Programming and Scripting

Position of character in word

How can I represent the position of 1 (considering only the 1s after the colon) in the word from field5 and above; counting from right to left. Input: TT-124 06-03-14 08-02-10 FAS CAT1:10 TT-125-1 05-03-14 10-06-08 CAS CAT2:1010 FAT1:10000 TT-125-3 07-03-14 11-02-06 FAS FAT1:1101... (6 Replies)
Discussion started by: aydj
6 Replies

4. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

5. Shell Programming and Scripting

How can we get the character before and after a word in a string?

Hi friends, I am working in a korn shell. i want to know the command which gives me the previous one character and next one character of a given keyword in a string? for exmaple: input string: /bin/dir/folder1/.proc_name^folderone Keyword: proc_name output : .^ ... (10 Replies)
Discussion started by: neelmani
10 Replies

6. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

7. Shell Programming and Scripting

How to chose certain character in a word?

Hi Expert, I would like to know on how to export only the first 6 character of below 0050569868B7 ABCDEFGHTY to 005056 ABCDEF Thank you. Reggy (7 Replies)
Discussion started by: regmaster
7 Replies

8. Shell Programming and Scripting

Delete character from a word

Friends, I'm looking for a command that delete the first tho caractere in a word. Here is an exp : I want to replace "20091001" by "091001" or "replace" by "place" Thx, (13 Replies)
Discussion started by: newpromo
13 Replies

9. Shell Programming and Scripting

Command to parse a word character by character

Hi All, Can anyone help me please, I have a word like below. 6,76 I want to read this word and check if it has "," (comma) and if yes then i want to replace it with "." (dot). That means i want to be changed to 6.76 If the word doesnot contain "," then its should not be changed. Eg. ... (6 Replies)
Discussion started by: girish.raos
6 Replies

10. UNIX for Dummies Questions & Answers

Delete between 10th character and 20th character

Hi, I have a .txt and I need to delete the characters betwwen the 10th and 20th... How can I do that... I need to do somethink like these: %s/I don't know how to define a range between 10th and 20th character//g Can you help me... If I want the 10 first characters i do this:... (2 Replies)
Discussion started by: nuno_fbo
2 Replies
Login or Register to Ask a Question