2

I have the following file:

export TMOUT

PS1="$(hostname):${LOGNAME}:\${PWD} ${PROMPT} "
PS1="${FGOLD}$(hostname):${LOGNAME}:\${PWD} ${PROMPT} ${NORMAL}"  
PS1=abd  
PS1="$(hostname):$LOGNAME:\$PWD $PROMPT "  
export PS1 LANG

I need to add the line below under the line matching export TMOUT:

FGOLD=`echo "\033[1;32m"`    

So, the final output should look like this:

export TMOUT  
FGOLD=`echo "\033[1;32m"`    

PS1="$(hostname):${LOGNAME}:\${PWD} ${PROMPT} "
PS1="${FGOLD}$(hostname):${LOGNAME}:\${PWD} ${PROMPT} ${NORMAL}"  
PS1=abd  
PS1="$(hostname):$LOGNAME:\$PWD $PROMPT "  
export PS1 LANG

The command below is not working:

sed 's/.*export.*\TMOUT./&\FGOLD=`echo "\033[1;32m"`/' pro

outputs:

export TMOUT  

PS1="$(hostname):${LOGNAME}:\${PWD} ${PROMPT} "
PS1="${FGOLD}$(hostname):${LOGNAME}:\${PWD} ${PROMPT} ${NORMAL}"
PS1=abd
PS1="$(hostname):$LOGNAME:\$PWD $PROMPT "
export PS1 LANG
Sivas
  • 67
  • 1
    Can you edit this and start out with a question or explanation on what you're trying to do? – Brandin Sep 19 '15 at 10:01
  • Right now it just looks like a code dump and I'm really not feeling like reading into it. Start out with at least a few sentences explaining what you're doing in a conversational way like "I'm trying to X and Y and I tried Z but have this problem P... " etc. THEN put your code dump so that we can have context. – Brandin Sep 19 '15 at 10:25
  • -i is Gnu, not available in options ! @don_crissti – Sivas Sep 19 '15 at 11:32

2 Answers2

1

With any sed:

sed '/export TMOUT/ a\
FGOLD=`echo "\\033[1;32m"`' file

or place the string in a variable

myvar='FGOLD=`echo "\\033[1;32m"`'
sed '/export TMOUT/ a\
'"$myvar"'' file
fd0
  • 1,449
0

You are escaping the T of TMOUT:

sed 's/.*export.*\TMOUT./&\FGOLD=`echo "\033[1;32m"`/' pro
                 ^
                 |-----??

That shouldn't make much difference but it's pointless. Then, you are also matching a character after TMOUT but your file has none (sed won't match the final newline). Try this instead:

sed 's/export TMOUT./&\nFGOLD=`echo "\\033[1;32m"`/' file    

I don't have a non-GNU sed to test this on and I'm not sure if the \n will work in the replacement. If that doesn't work, try adding a \ and then pressing Enter to get the newline:

sed 's/export TMOUT/&\    ## hit enter and continue writing on a new line
FGOLD=`echo "\\033[1;32m"`/' file 

So, other options are:

perl -pe 's/export TMOUT/$&\nFGOLD=\`echo "\\033[1;32m"\`/' file    

Or awk:

awk '{
        if(/export TMOUT/){a=1}
        {
            print;
            if(a==1){
                print "FGOLD=`echo \"\\033[1;32m\"`";
                a=0
        }}}' file
terdon
  • 242,166