How to delete a particular text without opening the file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete a particular text without opening the file.
# 1  
Old 08-29-2005
Question How to delete a particular text without opening the file.

Hi,
Could someone tell me how to delete a particular text inside an existing file without opening the file?
e.g. I have a text file called mytext.txt which contains three lines of text below and I want to delete “ ;” and delete the second line “b ;” including the carriage return.

a ;
b ;
c ;

So I want the file to have the contents below:

a
c

Any help will be appreciated.
p.s. I can do this in vi by running “%s/ ;//g” and deleting the second line with dd but I want this done without opening the file.
# 2  
Old 08-29-2005
Try:

Code:
sed -e '/b ;/d' mytext.txt > mytext.txt.new

Regards,
Nir
# 3  
Old 08-29-2005
Code:
sed -e "s/;$//g" -e "/b/d" mytext.txt > mytext.txt.new

Vino
# 4  
Old 08-29-2005
Bug

Thanks Nir!

That command did delete the second line but it still contained " ;" for lines one and three.

I added an extra command to get rid of the " ;" for lines one and two as well.

sed -e '/b ;/d' mytext.txt | sed -e 's/ ;//' > mytext.txt.new


Thanks for your help.

Cheers
Steve
# 5  
Old 08-29-2005
Quote:
Originally Posted by stevefox
Thanks Nir!

That command did delete the second line but it still contained " ;" for lines one and three.

I added an extra command to get rid of the " ;" for lines one and two as well.

sed -e '/b ;/d' mytext.txt | sed -e 's/ ;//' > mytext.txt.new


Thanks for your help.

Cheers
Steve
Avoid using sed twice. Instead give two different scripts with one sed.

Give it as

Code:
sed -e "s/;$//g" -e "/b/d" mytext.txt > mytext.txt.new

vino
# 6  
Old 08-29-2005
Question New question

Thanks Vino,

I have another question.

Could someone tell me how to delete a string from a line in a specific position using sed or other editors which doesn't require files to be opened?

e.g. I have a file called myfile.txt which contains three lines of text below and I want all of the lines to have 5 characters or less.

1234567
a b c d e
098


I want the result to be like below:

12345
a b c
098

Any help will be appreciated.

BTW please let me know if you have any recommended sites for learning unix commands such as above.

Cheers
Steve
# 7  
Old 08-29-2005
Quite a number of ways to do it.

Here are two ways using sed

Code:
sh-2.05b$ echo "1234567
> a b c d e 
> 098" | sed -e "s/\(.....\).*/\1/g"
12345
a b c
098



sh-2.05b$ echo "1234567
> a b c d e 
> 098" | sed -e "s/\(.\{5\}\).*/\1/g"
12345
a b c
098

These are not the only 2 ways using sed. There could be other ways as well.

vino
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Delete records based on a text file from a text file

Hi Folks, I am a novice and need to build a script in bash. I have 2 text files data.txt file is big file, column 2 is the we need to search and delete in the output. The filter file contains the rows to be deleted. Data.txt state city zone Alabama Huntsville 4 California SanDiego 3... (3 Replies)
Discussion started by: tech_frk
3 Replies

2. UNIX for Dummies Questions & Answers

How to read a file without opening the file and delete last line?

I have file called "text". The contents are as below : aaa bbb ccc ddd eee ffff ddd hhhh iiii I want to read this file without opening and and delete the last line. How can it be done? (4 Replies)
Discussion started by: the_hunter
4 Replies

3. UNIX for Dummies Questions & Answers

^H characters appear when opening text file using vi - RHEL

Version Info: $ cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.8 (Tikanga) $ $ uname -a Linux stryker138 2.6.18-308.13.1.el5 #1 SMP Thu Jul 26 05:45:09 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux I redirected manpage of ksh command's output to a text file as shown... (6 Replies)
Discussion started by: kraljic
6 Replies

4. Shell Programming and Scripting

How to delete lines of a text file based on another text file?

I have 2 TXT files with with 8 columns in them(tab separated). First file has 2000 entries whereas 2nd file has 300 entries. The first file has ALL the lines of second file. Now I need to remove those 300 lines (which are in both files) from first file so that first file's line count become... (2 Replies)
Discussion started by: prvnrk
2 Replies

5. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

6. Web Development

opening text files with javascript

Im aware under IE, javascript uses ActiveX Objects to open up and manipulate text files, excel files etc.. How abouts would I open a text file using javascript in Firefox ? thanks (1 Reply)
Discussion started by: JamesGoh
1 Replies

7. Shell Programming and Scripting

How to Delete string without opening a file

Hi Experts, I have several big size file arround 900 MB. From the file I need to delete some common strings but without opening the file. here is example- in file <?xml version='1.0' encoding='ISO-8859-1' standalone='no'?> <LogItems> <log logid="8423b5ae190810252359350480/1/1/1"> ... (6 Replies)
Discussion started by: thepurple
6 Replies

8. UNIX for Dummies Questions & Answers

Urgent help needed to delete some text without opening the file in unix

Hi To delete some text in 2 files in line1 ( not complete line) in unix without opening the files. For example: source file is like this <?xml version="1.0"... (5 Replies)
Discussion started by: pyaranoid
5 Replies

9. UNIX for Dummies Questions & Answers

delete file without opening vi

hi there guys, wonder if any gurus can help me out on this one... try searching the past threads but cant find anything. i have this huge file but when i use vi to open it it gives me the following error: <"pmrepserver.txt""/var/tmp/Ex86200" There is not enough space in the file... (7 Replies)
Discussion started by: lweegp
7 Replies

10. Shell Programming and Scripting

how to delete away text in a file?

I have this xml file which is call p.txt and it contains the follwing: 16:13:56 Msg send to Queue=<pregate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <system_c>HPREGATE</system_c> <trans_c>HSPG</trans_c> <trans_dt>20060105161333</trans_dt> <user_id_m></user_id_m> ... (5 Replies)
Discussion started by: forevercalz
5 Replies
Login or Register to Ask a Question