help with awk or sed

Hi Folks,

I need help about three things

  1. First i have filename "beatiful.txt" have word "good" wants to replace that word with "bad".

  2. Second I have file data.txt that containts words "p49" I want to delete that character from entire file.

  3. If i grep a particular word from a file it show me all similiar charter like

I have words in a file

#cat kk
oper ouier o9u o5

#cat kk |egrep -e "o5"
oper ouier o9u o5

its showing all, how to see particular match

sed -i 's/\<good\>/bad/g' beatiful.txt
sed -i 's/\<p49\>//g' data.txt

The -o option is for showing only the part which matched.

egrep -o o5 kk

Maybe you also want the -w option which avoids partial matches (like the pattern "dog" matches "endogenous").

Incidentally, notice that egrep accepts a file name argument; it doesn't need help from the cat to open and read a file. (This is called a "useless use of cat".)

Thanks,

what about if i want to do this on particular line number.

linenumber=42
sed -i "${linenumber}s/\<good\>/bad/g' beautiful.txt
sed -i "${linenumber}s/\<p49\>//g' data.txt

lets suppose i have following lines in a file

papo-k.abcd.com
tito-k.abcd.com
reta-k.abcd.com
rtsa-k.abcd.com

how to delete tito-k.abcd.com and rtsa-k.abcd and wants to add "pola-k.abcd.com at the end of file and might be also on line2.

Thanks,
bash

sed -e 's/tito-k.abcd.com/pola-k.abcd.com/' -e '/rtsa-k.abcd.com/d' file

Regards

its not working. :frowning:

Regards,
Bash

i want to delete lines and add one line from said file.

i have file

papo-k.abcd.com
tito-k.abcd.com
reta-k.abcd.com
rtsa-k.abcd.com

i want to delete line tito-k.abcd.com and reta-k.abcd.com.

wants to add pppp.abcd.com at the end of file and one more thing if i delete two lines that create space in file i want to delete blank lines as well.

It's not allowed to bump up questions! Please read the rules.

Do you get errors? Don't you get the desired output? Edit and play around with the given example, try to understand how it works and how to get the desired output.

Regards

Hi Frank,

in your rule its replacing but i want to delete it that i test it.

pls check it

Thanks,
Bash

Try awk..

awk '!/^tito|^reta|^$/{print}END{print "pppp.abcd.com"}' file

yes its working, but only on display but file have no change, i want to do it with redirection to any other file then overwrite to that file.

Kindly advice.

Thanks,
Bash

That's not very clear what you want, anyway ...

awk '!/^tito|^reta|^$/{print}END{print "pppp.abcd.com"}' file > new_file

thanks, its typo , i want it without redirection.

like i enter command and it change in a file directly no need for redirection.

kindly advice.

Thanks,
Bash

Some commands have an option to use a temporary file behind your back, and replace the original file with the temporary file if the command is successful, but awk is not among those commands. Your sed might or might not have an -i option; Perl also has it (all modern versions and ancient ones too, as far back as I remember).

perl -i~ -ne 'print unless /^(tito|reta)-k\.abcd\.com$/; END { print "pppp.abcd.com\n" }' file

The ~ argument to -i says to save the original file in file~ with a tilde appended, just so you can rescue it if things go wrong. If you don't want a backup, just remove the tilde from the command line.

[quote=era;302230635]
Some commands have an option to use a temporary file behind your back, and replace the original file with the temporary file if the command is successful, but awk is not among those commands. Your sed might or might not have an -i option; Perl also has it (all modern versions and ancient ones too, as far back as I remember).

perl -i~ -ne 'print unless /^(tito|reta)-k\.abcd\.com$/; END { print "pppp.abcd.com\n" }' file

This command is removing perfectly but not adding that line which is required. after i run command a blank line add up.

Kindly advice.

Thanks,
Bash

Using GNU sed:

sed -i '/^tito\|^reta\|^$/d;$a pppp.abcd.com' file
sed -i '/^tito\|^reta\|^$/d;$a pppp.abcd.com' file

[/quote]

one thing it will not add if pppp.abcd.com already exist.

Thanks,
Bash