Modifying contents of the file in shell script

Hello all,
I have a Kconfig file that looks like something below ...

menu "Application type"
                config GUI_TYPE_STANDARD
                        bool "Standard Application"

        source "cfg/config/std.in"
        source "cfg/config/cust.in"
        source "cfg/config/lang.in"
        source "cfg/config/type.in"
endmenu

=================================

I want to modify this file in shell script so that it should look like below ..

=================================

        source "./non_Profile/app/std.in"
        source "./non_Profile/app/cust.in"
        source "./non_Profile/app/lang.in"
        source "./non_Profile/app/type.in"

==================================

Thanks in advance !!!

Hi,

Please use code tag while posting your code/Input file/desire output file.

here is the solution of your problem.

 awk '/^source/{sub("cfg/config","./non_Profile/app",$0);print}' filename

Dear Friend,
I am new to shell scripting and "awk" so not able to find the issue but it is deleting contents of the file.

Regards,
Anand Shah

Is there a problem with Pravin27's solution?
If so what?

Dear Jonte,
When i run command given by Pravin on my_file then contents of my_file vanishes. Anyway I am not doubting solution given by Pravin but some problem with the way I am using it.

Regards,
Anand Shah

Test this (works fine for me)

echo 'menu "Application type"
config GUI_TYPE_STANDARD
bool "Standard Application"

source "cfg/config/std.in"
source "cfg/config/cust.in"
source "cfg/config/lang.in"
source "cfg/config/type.in"
endmenu' | awk '/^source/{sub("cfg/config","./non_Profile/app",$0);print}'
source ./non_Profile/app/std.in
source ./non_Profile/app/cust.in
source ./non_Profile/app/lang.in
source ./non_Profile/app/type.in

Dear Jotne,
When I run "awk" on the output of echo (it's same way you have shown), its working fine for me. But what i want is, whatever is there in echo command is part of file and I want to run "awk" on the file. In this way its not working.
What I want is ....

awk '/^source/{sub("cfg/config","./non_Profile/app",$0);print}' my_file 

where my_file contents

menu "Application type"
config GUI_TYPE_STANDARD
bool "Standard Application"

source "cfg/config/std.in"
source "cfg/config/cust.in"
source "cfg/config/lang.in"
source "cfg/config/type.in"
endmenu

This also works fine for me.
What os do you have?

cat my_file
menu "Application type"
config GUI_TYPE_STANDARD
bool "Standard Application"

source "cfg/config/std.in"
source "cfg/config/cust.in"
source "cfg/config/lang.in"
source "cfg/config/type.in"
endmenu
awk '/^source/{sub("cfg/config","./non_Profile/app",$0);print}' my_file
source "./non_Profile/app/std.in"
source "./non_Profile/app/cust.in"
source "./non_Profile/app/lang.in"
source "./non_Profile/app/type.in"

Hi,
I got the issue bit don't know how to fix it.
Actually my file has spaces in front of every line that's why it was not working. When I tried by removing spaces from every line it worked !!! but unfortunately I can not avoid those spaces and has deal with it. Can you please help me ?

            menu "Application type"
                       config GUI_TYPE_STANDARD
             bool "Standard Application"

            source "cfg/config/std.in"
            source "cfg/config/cust.in"
            source "cfg/config/lang.in"
            source "cfg/config/type.in"

           endmenu

This should fix it.

awk '{$1=$1} /^source/{sub("cfg/config","./non_Profile/app",$0);print}'

You can also remove the ^

awk '/source/{sub("cfg/config","./non_Profile/app",$0);print}'
1 Like

Or perhaps this?

sed -n 's|cfg/config/|./non_Profile/app|p' file

--

Actually all this confusion could have been avoided if proper code tags would have been used for those data samples.. I have added them in post #1 and see how it looks now..

1 Like

/source/ is too unprecise and risky.
$1~/source/ is okay, and $1=="source" is precise.

1 Like

Thank you everyone. Your solution worked !!!