Help with scripting-->Inserting a line before a pattern

Hi Guys,

I have written the following script test.sh in Linux .

read -p "Please enter the name of the application: " DIRTOCREATE

read -p "Please enter the number of associates to be given access to svn:" COUNT

for (( i=0 ; i<$COUNT ; i++ ))

do

read -p "Enter the associate id  :" ID

echo  $ID >> result.txt 

Lets assume the DIRTOCREATE = Testing and the
result.txt contains the below

12345
67891

I want to convert the contents in result.txt and Testing in the format

Testing = 12345, 67891

Finally want to insert the above line just before "[/]" in a file say access.txt which is of format

AppConfigurator = 260812, 260812, 291758, 266551
1713 = 258028, 271602, 407682, 407356
LHS = 291758, 260812

[/]

In short , the output should be ,

AppConfigurator = 260812, 260812, 291758, 266551
1713 = 258028, 271602, 407682, 407356
LHS = 291758, 260812
Testing = 12345, 67891
[/]

Doable ???

Thanks Guys

Thanks.

Try

read -p "Please enter the name of the application: " DIRTOCREATE
printf "%s %s " $DIRTOCREATE "=" >result.txt
TMP=""
read -p "Please enter the number of associates to be given access to svn:" COUNT
for (( i=0 ; i<$COUNT ; i++ ))
do
read -p "Enter the associate id  :" ID
printf "%s%s" $TMP $ID >>result.txt
TMP=","
done
printf "\n" >>result.txt

and then

awk '/\[\/\]/ {getline X < "result.txt"; print X}1' file

EDIT: or

sed '1{x;d}; /\[\/\]/{x;G}' result.txt file
1 Like

Thanks a lot RudiC..:slight_smile: