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).

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.

Hi,

Replacing the same file:

$ perl -lpi -e 's/coat\s*//g' infile

Get a backup of the original file:

$ perl -lpi.bak -e 's/coat\s*//g' infile

Regards,
Birei

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?

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.

[LEFT]Hi,

Sorry, it was my fault. Perhaps this other solution works:

$ perl -lpi.bak -e 's/coat\t+(hat)/\1/' infile

Regards,
Birei
[/LEFT]

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.

hmmmm, didn't pull 'coat' out of the line :confused:

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 ----------

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

Here is a example of using awk to remove any of the lines that contain the word coat.

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

[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

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 

[quote="codecaine,post:12,topic:278306"]

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??