I am working on a shell script which is getting data from another program and I am using that variable value to read the content from file and append few things inside those data at start and bottom:Below is an example:readonly file_location=$locationreadonly client_id = $idreadonly client_types = $type_of_clientHere $location, $id and $type_of_client value is being passed from another program. Below is an example:$location will be full path name like this:/home/david/data/12345678$id will be number: 120 $type_of_client will be space separted word: abc def pqrNow inside this location /home/david/data/12345678 I have files like this: abc_lop.xml, def_lop.xml and pqr_lop.xml. Meaning _lop.xml is going to be same always so we can hardcode this. We just need to iterate client_types variable and make a file name as shown above and read the whole file into a String for now (or may be if there is any other better way). This I got working as shown below:#!/bin/bashreadonly file_location=$locationreadonly client_id=$idreadonly client_types=$type_of_clientfor word in $client_typesdo    echo $word    file_value=`cat file_location/$word_lop.xml`    echo $file_valuedoneNow I need to do something special like string manipulation in shell script and I am not sure how we can do that. Now whatever value we read from those file I need to append something at the top of it and something at the bottom of it in all those files which we are reading. I need to do exactly same thing as shown below:For example: If file abc_lop.xml is like this after we view it using vi:<Hello version=100><!-- some stuff here --></Hello>I need to make it like this:<hello_function><name>Data</name><Hello version=100><!-- some stuff here --></Hello></hello_function>As you can see, I added this at the top <hello_function><name>Data</name> and I added this at the bottom </hello_function>. How can we do this in shell script?So In general echo $file_value should print out above appened values for each of those client_types:#!/bin/bashreadonly file_location=$locationreadonly client_id=$idreadonly client_types=$type_of_clientfor word in $client_typesdo    echo $word    file_value=`cat file_location/$word_lop.xml`    // may be do something here?    echo $file_value // this should print out appended string value both at top and bottom.doneEach of those XML files will be less than 10MB for sure.
append few strings at the start and bottom of string coming from file read
linux;bash;shell;sed
There's no need to read into a variable if all you're doing is outputting the data. For your main loop:for word in $client_types; do    echo $word    echo '<hello_function>'    echo '<name>Data</name>'    cat $file_location/${word}_lop.xml    echo '</hello_function>'doneOf course, this outputs the concatenation of all the modified files, which may be hard to separate later. But it works.Edit: To output to separate files:for word in $client_types; do    fn=${word}_new.xml    echo $word    echo '<hello_function>' >>$fn    echo '<name>Data</name>' >>$fn    cat $file_location/${word}_lop.xml >>$fn    echo '</hello_function>' >>$fndoneDepending, of course, on just what you want in each file.