- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- SED to delete first three occurances...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:16 AM
05-24-2007 06:16 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:24 AM
05-24-2007 06:24 AM
Re: SED to delete first three occurances...
sed '1,3 s/-t//' filename
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:30 AM
05-24-2007 06:30 AM
Re: SED to delete first three occurances...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:32 AM
05-24-2007 06:32 AM
Re: SED to delete first three occurances...
That did not work.
Appreciate the help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:36 AM
05-24-2007 06:36 AM
Re: SED to delete first three occurances...
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:37 AM
05-24-2007 06:37 AM
Re: SED to delete first three occurances...
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:38 AM
05-24-2007 06:38 AM
Re: SED to delete first three occurances...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:42 AM
05-24-2007 06:42 AM
Re: SED to delete first three occurances...
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:48 AM
05-24-2007 06:48 AM
Re: SED to delete first three occurances...
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:52 AM
05-24-2007 06:52 AM
Re: SED to delete first three occurances...
"doesn't work"
Exactly what output do you get? Any errors?
# which sed
...reports?
# wc -l filename
...reports?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 06:56 AM
05-24-2007 06:56 AM
Re: SED to delete first three occurances...
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 07:07 AM
05-24-2007 07:07 AM
Re: SED to delete first three occurances...
> 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 07:23 AM
05-24-2007 07:23 AM
Re: SED to delete first three occurances...
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 07:36 AM
05-24-2007 07:36 AM
Re: SED to delete first three occurances...
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 07:47 AM
05-24-2007 07:47 AM
Re: SED to delete first three occurances...
Points have been assigned to all!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 01:50 PM
05-24-2007 01:50 PM
Re: SED to delete first three occurances...
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2007 03:33 PM
05-24-2007 03:33 PM
Re: SED to delete first three occurances...
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.
- Tags:
- sed