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
# 8  
Old 12-01-2010
Quote:
Originally Posted by skunky
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 ----------



thanks for this reply, but will this work on the ONE line if 'coat' appears multiple times within the file?
When redirecting in shell you can not redirect to the same filename. What the shell does is see > and automatically creates a new file erasing the contents before it performs work on the actual file.
# 9  
Old 12-01-2010
Quote:
Originally Posted by birei
Hi,

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

Regards,
Birei

hmmmm, didn't pull 'coat' out of the line Smilie

Seems like there should be some easier sed/grep&delete/awk commands to do this, but I'm not familiar, obv, with them

---------- Post updated at 02:57 PM ---------- Previous update was at 02:51 PM ----------

Quote:
Originally Posted by codecaine
When redirecting in shell you can not redirect to the same filename. What the shell does is see > and automatically creates a new file erasing the contents before it performs work on the actual file.
ah.

well it seems logical that this would do the trick, running the command on that file and not changing the output of the file:

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

but that didn't seem to work either

Last edited by skunky; 12-01-2010 at 04:52 PM.. Reason: mistyped a word
# 10  
Old 12-01-2010
Here is a example of using awk to remove any of the lines that contain the word coat.

Code:
awk -F \; ' 
{ for(x=1; x <= NF; x++){
	if(index($x,"coat") == 0 )
		printf("%s;",  $x); 
	} 
} 

END{
	printf("\n");
} ' filename.txt

# 11  
Old 12-01-2010
Quote:
Originally Posted by codecaine
Here is a example of using awk to remove any of the lines that contain the word coat.

Code:
awk -F \; ' 
{ for(x=1; x <= NF; x++){
    if(index($x,"coat") == 0 )
        printf("%s;",  $x); 
    } 
} 

END{
    printf("\n");
} ' filename.txt

while helpful, it's not what i'm trying to do.

i'm trying to search a file to find that one specific line (mentioned in the original post), remove 'coat' IF it's contained in the line, and leave the rest of the file as is.

myfile:

test text
text
coat
line 4
coat
*.cat;dog;kennel;house;barn;horse;hay;coat hat
dust
*coat coat

[ The space between coat and hat is a tab (if that matters)]

>>>>> i want to have a script that will search "myfile" for the line:

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

>>>> and just remove the word 'coat'. the line will be in that exact format too
# 12  
Old 12-01-2010
[QUOTE=skunky;302476449]while helpful, it's not what i'%

---------- Post updated at 05:39 PM ---------- Previous update was at 05:25 PM ----------

Here is the code revised to suit your needs. It only removes lines that have exactly coat[tab]hat
Code:
awk -F \; ' 
{ 
	for(x=1; x <= NF; x++){
		if(index($x,"coat\that") == 0 )
			if(x == NF)
				printf("%s\n",  $x); 
			else
				printf("%s;", $x);
	} 
} ' filename.txt


Last edited by codecaine; 12-01-2010 at 06:32 PM..
# 13  
Old 12-02-2010
[QUOTE=codecaine;302476459]
Quote:
Originally Posted by skunky
while helpful, it's not what i'%

---------- Post updated at 05:39 PM ---------- Previous update was at 05:25 PM ----------

Here is the code revised to suit your needs. It only removes lines that have exactly coat[tab]hat
Code:
awk -F \; ' 
{ 
    for(x=1; x <= NF; x++){
        if(index($x,"coat\that") == 0 )
            if(x == NF)
                printf("%s\n",  $x); 
            else
                printf("%s;", $x);
    } 
} ' filename.txt


thing is, i only want the word 'coat' removed, and the line left there.......

anyone know an easy sed command to search out the line with that exact string first, and then remove the word 'coat' IF it appears in that line??

---------- Post updated 12-02-10 at 10:17 AM ---------- Previous update was 12-01-10 at 07:42 PM ----------

bump, any help on this??
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