2

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=$location
readonly client_id = $id
readonly client_types = $type_of_client

Here $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 pqr

Now 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/bash

readonly file_location=$location
readonly client_id=$id
readonly client_types=$type_of_client

for word in $client_types
do
    echo $word
    file_value=`cat "file_location"/"$word"_lop.xml`
    echo $file_value
done

Now 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/bash

readonly file_location=$location
readonly client_id=$id
readonly client_types=$type_of_client

for word in $client_types
do
    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.
done

Each of those XML files will be less than 10MB for sure.

1 Answer 1

2

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>'
done

Of 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>' >>"$fn"
done

Depending, of course, on just what you want in each file.

5
  • Thanks for your suggestion. My plan is to use this modified value which has string appended at top and bottom later on for some other purpose. So I wanted to store that in a variable or make a new file with it. Do you think there is any way we can do that?
    – david
    Commented Sep 15, 2015 at 22:02
  • It's usually not best to try to store long texts in bash variables. To output each one to a file, you could do something like edit above.
    – Tom Hunt
    Commented Sep 15, 2015 at 22:07
  • Ok so that means, abc_new.xml file will have appended string at top and bottom right?
    – david
    Commented Sep 15, 2015 at 22:09
  • Yes. You can set the filename to whatever, of course.
    – Tom Hunt
    Commented Sep 15, 2015 at 22:10
  • Sorry about that, you're quite right, I got carried away quoting.
    – terdon
    Commented Sep 15, 2015 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .