Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to make a .xml file, the bash command need to copy this block of code, and replace the CITY-FROM-TEXT-FILE-LINE-1 attribute from city.txt (one city per line)

<ss:Row ss:Height ="22">
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">CITY-FROM-TEXT-FILE-LINE-1</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">CALIFORNIA</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">CA</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
            <ss:Cell>
               <ss:Data ss:Type="String">xx</ss:Data>
            </ss:Cell>
        </ss:Row>
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Create a file template.xml containing the text above

IFS=$(echo -en "\n\b") ; for C in `cat city.txt` ; do sed s/CITY-FROM-TEXT-FILE-LINE-1/$C/ template.xml ; done > cities.xml

Commands used

  • cat ; concatenate a file to STDOUT
  • sed ; stream editor

We also use the standard bash for loop mechanism, and redirection to a file.

For further reading I recommend ;

share|improve this answer
    
This is great, how ever some of the cities names are two words and now the output breaks the names. –  eddie Feb 6 '12 at 7:36
    
You always learn something posting here ; the IFS variable controls how bash splits loop input (apparently). Have edited one-liner to reflect this - it splits on NEWLINE or BACKSPACE (not sure why the backspace, but hey...) –  Adrian Feb 6 '12 at 21:35
add comment

There's not really enough information to know exactly what you're desiring, but I'll try to take a guess, and it may help. If you had a file called cities.txt which contained:

xx
CITY-FROM-TEXT-FILE-LINE-1
CALIFORNIA
CA
xx

and then you ran the awk command on that input:

awk 'BEGIN{print "<ss:Row ss:Height =\"22\">"}{printf "  <ss:Cell>\n    <ss:Data ss:Type=\"String\">%s</ss:Data>\n  </ss:Cell>\n",$0}END{print "</ss:Row>"}' < cities.txt

then you would should the output:

<ss:Row ss:Height ="22">
  <ss:Cell>
    <ss:Data ss:Type="String">xx</ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String">CITY-FROM-TEXT-FILE-LINE-1</ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String">CALIFORNIA</ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String">CA</ss:Data>
  </ss:Cell>
  <ss:Cell>
    <ss:Data ss:Type="String">xx</ss:Data>
  </ss:Cell>
</ss:Row>

This may not answer your query directly, but hopefully should get you one possible way (of many) to get your started.

share|improve this answer
    
Thanks for your reply, this is infect the result I am trying to get, I do have a city.txt file which have a city name per line, and all this is static it stays the same but the: CITY-FROM-TEXT-FILE-LINE-1 xx CALIFORNIA CA xx –  eddie Feb 5 '12 at 20:12
    
@eddie: If you paste a snippet of your actual city.txt I can try and tweak the command above (or you can tweak it yourself! ). Without knowing what you're actually dealing with, I can only really guess. –  sladen Feb 5 '12 at 21:44
    
The city.txt have only city names in this format: city1 city2 etc –  eddie Feb 6 '12 at 6:52
    
@eddie: does the city.txt file contain each of the names "city1, city2, etc" all on one line (separated by eg. spaces/commas); or one city per line; or one city and several other pieces of data per line; or something else? I'm happy to assist further, but it would be very useful if you could paste a short snippet to clarify what the input looks like. –  sladen Feb 6 '12 at 10:02
    
im sorry im not clear to begin with .. It is one city name per line no commas or anything like that Thanks –  eddie Feb 6 '12 at 12:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.