Delete string between 3rd tab and first pattern with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete string between 3rd tab and first pattern with SED
# 1  
Old 02-05-2012
Delete string between 3rd tab and first pattern with SED

Hello,

I have this sentence :
Code:
Pattern1        Pattern2       Pattern3       Pattern4-which-contains-HELLO-string-and-other-stuff-and-second-HELLO-and-third-HELLO

I want to delete everything between the 3rd tab (\t) and the FIRST pattern "HELLO" of the line.

Result expected is :
Code:
Pattern1       Pattern2       Pattern3       -string-and-other-stuff-and-second-HELLO-and-third-HELLO

I've this command
Code:
sed 's/\(.*\)\t.*HELLO\(.*\)/\1 \2/'

But it delete only after 2nd \t and not 3rd, and it ends at the LAST "HELLO" and not FIRST.

Can you help me ?
# 2  
Old 02-05-2012
If your input text does not contain greater/lessthan symbols, then this will work:
It also depends on the fact that there are not any tabs after the third one.

Code:
sed 's/.*\t/&</; s/HELLO/&>/; s/<.*>//'

It adds "<" after the last tab and ">" after the first hello, then deletes everything between.

Last edited by agama; 02-05-2012 at 11:38 AM.. Reason: typo
# 3  
Old 02-05-2012
Hi,

It doesn't work. It give me
Code:
pattern1       pattern2       pattern3      Pattern4-which-contains-HELLO>-string-and-other-stuff-and-second-HELLO-and-third-HELLO <

Is there a way to specify 3rd tab as 1st pattern and 1st HELLO as 2nd pattern and replace everything by nothing between these two patterns ?
# 4  
Old 02-05-2012
Those might not be tabs in your input, but a string of 8 blanks. Does this work:

Code:
sed 's/.*        /&</; s/HELLO/&>/; s/<.*>//'

# 5  
Old 02-05-2012
Hi,

Yes it is, I cannot paste it between code tags on the forum.
# 6  
Old 02-05-2012
Quote:
Originally Posted by theclem35
Hi,

Yes it is, I cannot paste it between code tags on the forum.
Right -- I initially ran into that when pasting your example into a file to test locally; had to convert them to tabs.

Ok, it might be that '\t' isn't recognised by your awk as TAB. Try running the original sed, but instad of using '\t' actually type a TAB there. If that doesn't do it, I'm not sure what is going on.
# 7  
Old 02-05-2012
Try:
Code:
perl -pe 's/[^\t]*?HELLO//'

-or-
Code:
sed 's/[^\t]*HELLO\([^\t]*HELLO[^\t]*HELLO\)/\1/'


Last edited by Scrutinizer; 02-05-2012 at 01:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed. Delete line before and after pattern.

Hi. In need to delete line before and after pattern in file. I came to following commands. Delete line before sed -ni '/pattern/{x;d;};1h;1!{x;p;};${x;p;}' /etc/testfile Delete line after sed -i '/pattern/{N;s/\n.*//;}' /etc/testfile Is it possible to merge it in single command? (3 Replies)
Discussion started by: urello
3 Replies

2. Shell Programming and Scripting

Use sed to delete remainder of line after pattern

Greetings, I need some assistance here as I cannot get a sed line to work properly for me. I have the following list: Camp.S01E04.720p.HDTV.X264-DIMENSION Royal.Pains.S05E07.720p.HDTV.x264-IMMERSE Whose.Line.is.it.Anyway.US.S09E05.720p.HDTV.x264-BAJSKORV What I would like to accomplish is to... (3 Replies)
Discussion started by: choard
3 Replies

3. Shell Programming and Scripting

Delete a pattern using sed.

I have a string in which i need to match a pattern and then i need to delete that line which contains that matching string. The string is .. This is the given string //abc/def/IC.5.4.3/test/... i need to match //abc I am writing like this sed '/^/\/\abc/d' but it is not giving me... (4 Replies)
Discussion started by: codecatcher
4 Replies

4. Shell Programming and Scripting

Delete to eol after pattern with sed

working on interactive ftp script saving log and want to remove the password from log so "quote PASS foofoo" would become "quote PASS XXXXXXX" replacing or removing the password is of no matter have tried several sed commands but have only been successful with character matching not pattern ... (2 Replies)
Discussion started by: ppaprota
2 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

7. UNIX for Dummies Questions & Answers

locate a pattern and delete it using sed

if i have a file test and have a number(123)456-7899 how do i delete this without deleting all digits in the file. parentheses isn't necessary. (2 Replies)
Discussion started by: hobiwhenuknowme
2 Replies

8. HP-UX

How to delete specific pattern in a file with SED?

I have one file which is having content as following... 0513468211,,,,20091208,084005,5,,2,3699310, 0206554475,,,,20090327,123634,85,,2,15615533 0206554475,,,,20090327,134431,554,,2,7246177 0103000300,,,,20090523,115501,89,,2,3869929 0736454328,,,,20091208,084005,75,,2,3699546... (7 Replies)
Discussion started by: ganesh.mandlik
7 Replies

9. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

10. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies
Login or Register to Ask a Question