Operating System - Linux
1829921 Members
2417 Online
109993 Solutions
New Discussion

SED to delete first three occurances...

 
SOLVED
Go to solution
jmckinzie
Super Advisor

SED to delete first three occurances...

I have a file with 5 lines.
all of which contain the switch -t.

I want to move the -t off the first three occurances and leave an remaining ones alone.

I have this command but it removed all -t within the file.

sed 's/-t//' filename


Any ideas?
17 REPLIES 17
Ivan Ferreira
Honored Contributor

Re: SED to delete first three occurances...

Try with:

sed '1,3 s/-t//' filename
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor
Solution

Re: SED to delete first three occurances...

Hi Jody:

If by "occurances" you mean "lines":

# sed -e '1,3s/-t//' filename

Regards!

...JRF...
jmckinzie
Super Advisor

Re: SED to delete first three occurances...

No, i do not want to delete the lines, just the -t
jmckinzie
Super Advisor

Re: SED to delete first three occurances...

Ivan,

That did not work.

Appreciate the help.
Patrick Wallek
Honored Contributor

Re: SED to delete first three occurances...

What didn't work?

I just tried the following:

# cat test
-t -b
-t -c
-t -d
-t -e
-t -f

# sed 1,3s/-t// test
-b
-c
-d
-t -e
-t -f

James R. Ferguson
Acclaimed Contributor

Re: SED to delete first three occurances...

Hi (again) Jody:

# cat .filename
line 1 -t
line 2 -t
line 3 -t
line 4 -t
line 5 -t

# sed -e '1,3s/-t//' filename
line 1
line 2
line 3
line 4 -t
line 5 -t

Regards!

...JRF...
Sandman!
Honored Contributor

Re: SED to delete first three occurances...

# sed '1,3 s/\(.*\)-t/\1/g' file
jmckinzie
Super Advisor

Re: SED to delete first three occurances...

filename = /tmp/cron

cat /tmp/cron

00 03 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 12 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t > /tmp/tiscan 2>&1
00 17 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -t -o > /tmp/tiscan 2>&1

sed '1,3 s/\(.*\)-t/\1/g' /tmp/cron

doesn't work
neither does the one above that.

I wonder why?
Patrick Wallek
Honored Contributor

Re: SED to delete first three occurances...

It worked for me.

Using your data in a file called test.

$ sed 1,3s/-t// test
00 03 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o
00 12 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o > /tmp/tis
can 2>&1
00 17 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -t -o > /tmp/t
iscan 2>&1
James R. Ferguson
Acclaimed Contributor

Re: SED to delete first three occurances...

Jody:

"doesn't work"

Exactly what output do you get? Any errors?

# which sed

...reports?

# wc -l filename

...reports?

Regards!

...JRF...
jmckinzie
Super Advisor

Re: SED to delete first three occurances...

ok, does this say for the first three lines?

what if these entries aren't in the first three lines?

# cat cron
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 12 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t > /tmp/tiscan 2>&1
00 17 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -t -o > /tmp/tiscan 2>&1

now with SED.....

# sed 1,3s/-t// cron
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o
00 12 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t > /tmp/tiscan 2>&1
00 17 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -t -o > /tmp/tiscan 2>&1


with additional lines?

# sed 1,3s/-t// cron
ewfwefj
wefwefwef
wefwefwef
wefwefwe
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 06 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t
00 12 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -o -t > /tmp/tiscan 2>&1
00 17 * * 0-6 /opt/TIscan/bin/scan -c /opt/TIscan/etc/sysscan.cfg -t -o > /tmp/tiscan 2>&1
James R. Ferguson
Acclaimed Contributor

Re: SED to delete first three occurances...

Jody:

> what if these entries aren't in the first three lines?

You never made that clear. However:

# perl -nle 'm/-t/ && $i++ <=2 && s/-t//;print' filename

Regards!

...JRF...


jmckinzie
Super Advisor

Re: SED to delete first three occurances...

James,

That worked....thanks.

Sorry for my vague description.

for the sake of my learning sed and perl for that matter, could you explain what each character is doing?

perl -nle 'm/-t/ && $i++ <=2 && s/-t//;print'

perl -n (???) l (octal-what does that mean?) e (followed by a command) 'm/-t (i do not know) && counter <=2 (0-2=3) s/-t//; print'

TIA
James R. Ferguson
Acclaimed Contributor

Re: SED to delete first three occurances...

Hi (again) Jody:

# perl -pe 'm/\s-t\s/ && $i++ <=2 && s/-t//'

...is a wee-bit shorter and safer.

The '-p' says read (a line) and print. The '-n' in the first variation says read but don't automatically print.

The m/\s-t\s/ says match the '-t' if it is bounded by a whitespace (space, tab) character on either side. After all, we don't match things like "this-thing".

The && means AND.

The variable $i is undefined but for Perl's purpose will be zero when the code begins. Thus for the values 0, 1 and 2 of $i, we will substitute nothing for the regular expression /-t'/.

In all, we read the file(s) specified on the command line, looping over each line. If we find a " -t " and we haven't counted three times, we remove it from the line we read. In any event, every line of the file gets printed.

Regards!

...JRF...
jmckinzie
Super Advisor

Re: SED to delete first three occurances...

Thanks a bunch...this helped significantly.

Points have been assigned to all!
James R. Ferguson
Acclaimed Contributor

Re: SED to delete first three occurances...

Hi (again) Jody:

Although a bit more advanced, this solution is a bit more rigorous:

# perl -pe 'if ($i<3){$i++ if s/((?<=\W)|\A)-t(?=\W|\z)//g}' filename

All occurances of the pattern "-t" is snipped from the number of lines tested against $i. The pattern is sought where it is left-bounded by a non-word character or the begining of the line; and it is right-bounded by a non-word character or the end of the line.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: SED to delete first three occurances...

If you really really want to use sed(1) and gotos, you can try:
sed -e '
1,/-t/ {
s/-t//g
t second
}
: second
1,/-t/{
s/-t//g
t third
}
: third
1,/-t/{
s/-t//g
t fourth
}
: fourth
' filename

I have no idea why it works, except sed(1) must remember where it found the first "-t" for the first block and not reevaluate it.