can we optimize this command

can we optimize this command ?

sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i1.txt;

as here i am using two files ...its overhead..can we optimise to use only 1 file

sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i.txt;

keeping them same but it doesnt work !!

anyother way so that i can use only 1 file or so ?

thanks

If you use the GNU version of sed it has an option for in-place editing.

You can also do what you want to do with sed in one invocation of sed

 sed -e '1,2d' -e 's/AAAA//g'  /study/i.txt
awk 'NR>2{gsub("AAAA","");print}' ORS="" file > newfile
sed -i '1,2d;s/AAAA//g;:a;N;s/\n/ /;ta' infile
{ rm /study/i.txt; sed -e '1,2d' -e 's/AAAA//g'  > /study/i.txt; } < /study/i.txt

most of u forgot tr command i have used in expression !! :frowning:

what to do with it ?

ps : it would be better if u explains the command too :slight_smile:

I didn't forget the tr command, I replaced it with a sed equivalent (:a;N;s/\n/ /;ta), so that everything could be done with one command.
I used the -i option for inline edit, the various sed operations are separated by a semicolon.

S.

my bash doesnt support -i option :frowning:

(:a;N;s/\n/ /;ta), could u please explain me this command in detail
thanks a lot

What version of sed are you using then?

:a is just a label
N appends next line with \n as separator (newline character)
s/\n/ / replaces a newline character with a space
ta returns to label "a" is the previous operation was successful

@:/root !ksh mcs -p /usr/bin/sed
/usr/bin/sed:

@(#)SunOS 5.10 Generic January 2005

VERSION: 11.10.0,REV=2005.01.21.15.53