Search for a line, delete a string in it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a line, delete a string in it
# 1  
Old 12-01-2010
Search for a line, delete a string in it

let me start out by saying i have ZERO exp with any kind of scripting, so sorry if this is really basic stuff.....

For example, I need to have a script that will search a file and find this line in the file:

*.cat;dog;kennel;house;barn;horse;hay;coat hat

and remove the "coat" from the line. The space between coat and hat is a tab (if that matters).
# 2  
Old 12-01-2010
Code:
sed 's/coat  hat$/hat/'  inputfile > outputfile

See the /coat...hat/ part? the dotted line is really a single tab character, so the /coat hat/
bit above has a tab for whitepsace in it.
# 3  
Old 12-01-2010
Hi,

Replacing the same file:
Code:
$ perl -lpi -e 's/coat\s*//g' infile

Get a backup of the original file:
Code:
$ perl -lpi.bak -e 's/coat\s*//g' infile

Regards,
Birei
# 4  
Old 12-01-2010
thanks, but I think this might be missing something? I tested it like this:

sed 's/coat (tab) hat$/hat/' myfile > myfile

and it ended up deleting everything else in the file, and all i want removed is 'coat'

maybe i'm the one missing something?? ha.

---------- Post updated at 02:07 PM ---------- Previous update was at 02:06 PM ----------

Quote:
Originally Posted by birei
Hi,

Replacing the same file:
Code:
$ perl -lpi -e 's/coat\s*//g' infile

Get a backup of the original file:
Code:
$ perl -lpi.bak -e 's/coat\s*//g' infile

Regards,
Birei
thanks for this reply, but will this work on the ONE line if 'coat' appears multiple times within the file?
# 5  
Old 12-01-2010
Hi,

Yeah. It should delete all 'coat' of all lines altought the word appears several times per line.

Regards,
Birei.
# 6  
Old 12-01-2010
Quote:
Originally Posted by birei
Hi,

Yeah. It should delete all 'coat' of all lines altought the word appears several times per line.

Regards,
Birei.
Ok, that's what I thought, but I only want the instance of 'coat' in that specific line removed. I want all the other instances of 'coat' left alone. I should have been more clear to begin with.
# 7  
Old 12-01-2010
Hi,

Sorry, it was my fault. Perhaps this other solution works:
Code:
$ perl -lpi.bak -e 's/coat\t+(hat)/\1/' infile

Regards,
Birei

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. UNIX for Dummies Questions & Answers

search for a string and delete it from the file

Hi , I am breaking my head from past one day ...to delete lines from a file which match to the string pattern.:wall: I am storing the search string in a variable and search if the file exists in the folder,if not delete that entry from the file. I am having problem to delete that line from... (2 Replies)
Discussion started by: rashmisb
2 Replies

7. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Shell Programming and Scripting

search string and delete the line

Hi All, I have a file from Mainframe which has one of the lines with so many words... i tried to fold, format to 80 chararcter.. stil did not work. So i have decided to search for a string in that line Ex.FLIGHT PLAN and once if it is found i want to delete the entire line. Please help... (2 Replies)
Discussion started by: digitalrg
2 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question