LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2006, 05:29 PM   #1
nazs
Member
 
Registered: Apr 2005
Posts: 57

Rep: Reputation: 15
Help deleteing lines using Sed


Hi All,
I was hopeing someone might be able to help me with a script I think using Sed that would find the file (ex.Nazs1) go inside this file and delete the lines that end in 88888888. And then make a new file with what is left after the lines have been deleted. Below is what is inside one of the files. I have about 50 files that need to be done. I would like to be able to just insert the filename into the script and then run it.

Mar 3 17:14 bin_prtrimester-20060303.1714.56.88888888
Mar 3 17:14 bin_prtrimester-20060303.1714.24.88888888
Dec 5 10:53 bin_safepr-20051205.1053.24.5790877
Nov 3 14:14 bin_safepr-20051103.1414.44.88888888
Nov 3 2004 bin_danna-20041103.1638.36.4057569
Nov 3 2004 bin_danna-20041103.1636.23.4057847
Nov 3 2004 bin_danna-20041103.1636.19.4057500
Nov 3 2004 bin_danna-20041103.1635.43.4057598
Nov 3 2004 bin_danna-20041103.1635.42.4057580
Nov 3 2004 bin_danna-20041103.1635.41.4057554
Nov 3 2004 bin_danna-20041103.1635.40.4057511
Nov 3 2004 bin_danna-20041103.1635.36.4057449
Nov 3 2004 bin_danna-20041103.1022.57.4057592
Oct 7 2004 bin_fxfever-20041007.1655.19.84900353
Sep 22 2004 bin_fxfountain-20040922.1808.12.88888888
Aug 3 2004 bin_fxfever-20040803.1746.53.88888888
Aug 3 2004 bin_fxfever-20040803.1745.37.88888888
Aug 3 2004 bin_fxfever-20040803.1444.16.88888888
Sep 10 2002 bin_show-20010613.1630.12.88888888


All my files are in the /tmp directory.

Thanks,

Nazs
 
Old 03-06-2006, 06:02 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Yes, sed would be the answer. Here is a sample of what sed does with the output you wanted. I shrank the output that you gave to save space:

Code:
twantrd@twantrd:~/bash_scripting$ more sample
Dec 5 10:53 bin_safepr-20051205.1053.24.5790877
Nov 3 2004 bin_danna-20041103.1638.36.4057569
Nov 3 2004 bin_danna-20041103.1636.23.4057847
Nov 3 2004 bin_danna-20041103.1636.19.4057500
Aug 3 2004 bin_fxfever-20040803.1745.37.88888888
Aug 3 2004 bin_fxfever-20040803.1444.16.88888888
Sep 10 2002 bin_show-20010613.1630.12.88888888

twantrd@twantrd:~/bash_scripting$ cat sample | sed 's/88888888$//g'
Dec 5 10:53 bin_safepr-20051205.1053.24.5790877
Nov 3 2004 bin_danna-20041103.1638.36.4057569
Nov 3 2004 bin_danna-20041103.1636.23.4057847
Nov 3 2004 bin_danna-20041103.1636.19.4057500
Aug 3 2004 bin_fxfever-20040803.1745.37.
Aug 3 2004 bin_fxfever-20040803.1444.16.
Sep 10 2002 bin_show-20010613.1630.12.
So, the way I used sed is that it searches for all lines in file 'sample' and strips away anything that ends in '88888888'.

I'll leave it up to you to create the script . If you need help, paste the script that you have written so far and we'll help fix whatever issues you have.

-twantrd
 
Old 03-06-2006, 06:20 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can use the -i option to edit the files in place.

sed -i '/\.88888888$/d' filename

You didn't provide information on how the file can be selected, such as the filename pattern. or if you need to search for files with the pattern. That would determine what would work best, a for log, or piping the output of a grep search.

find /tmp -iname "<pattern>" | sed -i '/\.88888888$/d'

or

for file in /tmp/<pattern>; do
sed -i '/\.88888888$/d' "$file"
done

Using a "grep" option, you can search for the "\.88888888$" pattern and return the names of the files containing the pattern.
 
Old 03-07-2006, 12:26 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
easier one with grep itself,
Code:
grep -v 88888888 filename > newfilename
 
Old 03-07-2006, 12:27 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
cat sample | sed 's/88888888$//g'
what is the need for two process cat and sed and kernel ds,
just sed would do,

Code:
sed 's/88888888$//g' filename
 
Old 03-07-2006, 12:38 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
i missed out one more easier way in awk;

Code:
awk -F" " ' { if ( $0 !~ /888888/ ) { print $0 } } ' filename > newfilename
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge lines in a file using sed arobic Programming 8 01-20-2012 02:11 PM
Join all lines using sed chipix Programming 3 04-03-2007 09:55 AM
Removing duplicate lines with sed tireseas Programming 10 01-12-2005 03:27 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
Deleteing locked/hidden files Miyamoto Linux - Software 7 06-23-2003 06:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration