The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help needed nnayagam Shell Programming and Scripting 2 03-07-2008 03:34 AM
Little help needed. Netghost AIX 5 08-10-2006 11:29 AM
Help needed dsravan Shell Programming and Scripting 2 07-20-2006 06:37 AM
awk help needed. cskumar Shell Programming and Scripting 0 07-20-2006 04:24 AM
Sed help needed stevefox Shell Programming and Scripting 5 12-04-2005 10:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-15-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Question SED help needed

Could someone please tell me the command to delete a line with a specific word and also deleting all lines below this line? I want this to be done without opening the file itself using SED in HP Unix?

e.g. I have a text file input.txt and with 4 lines of text below:

John 123 A--G
Chris 190 C--T
Mat 098 F--K
Mike 674 B--N
Pat 838 L--U

I want to delete the line which has string "098" and all lines below this line to look like the below:

John 123 A--G
Chris 190 C--T
Reply With Quote
Forum Sponsor
  #2  
Old 11-15-2005
Registered User
 

Join Date: Sep 2005
Location: india
Posts: 78
I think below code can cater to ur need.

csplit filename "/`grep 098 filename`/"

will create files with xx prefix xx00 file will contain what u need.
Reply With Quote
  #3  
Old 11-15-2005
Registered User
 

Join Date: Jul 2005
Location: Chennai
Posts: 39
This will help u ...

sed '/098/,$ d' filename

this deletes all lines starting from the first line having 098 uptill the end of file ...
Reply With Quote
  #4  
Old 11-15-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Thanks everyone!
It worked!
Reply With Quote
  #5  
Old 11-15-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Question

Could someone please tell me how to make a Korn Shell to do the below:

I have the files below with the same format (different data) as mentioned previously and I want a shell to run "sed '/098/,$ d' filename" on all of the files in the directory.

list20051001.txt
list20051002.txt
list20051003.txt
...
list20051031.txt
Reply With Quote
  #6  
Old 11-15-2005
Registered User
 

Join Date: Oct 2005
Posts: 7
Hi
Try the following:
for filename in `ls directory `
do
sed '/098/,$ d' $filename > $filename.1
mv $filename.1 $filename
done
Reply With Quote
  #7  
Old 11-16-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Question

Niyati
Thanks for your replay.
However I get the error below and it isn't working:


./testshell.ksh[4]: ls /MyFolder/Test.1: cannot create
mv: ls: cannot access: No such file or directory
mv: /MyFolder/Test.1: cannot access: No such file or directory
mv: ls: cannot access: No such file or directory


Below is the contents of testshell.ksh:

#!/bin/ksh
for filename in 'ls /MyFolder/Test'
do
sed '/098/,$ d' $filename > $filename.1
mv $filename.1 $filename
done


Below is the contents of /MyFolder/Test directory:
list1.txt list12.txt list2.txt list3.txt


Any help will be appreciated.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0