0

So I have a file of many fields which start with "Content:" like below...

NOTE: The content goes over multiple lines

Content:Wonderful time- even with the snow! What a great experience! From the goldfish in the room (which my daughter loved) to the fact that the valet parking staff who put on my chains on for me it was fabulous. The staff was attentive and went above and beyond to make our stay enjoyable. Oh, and about the parking: the charge is about what you would pay at any garage or lot- and I bet they wouldn't help you out in the snow! 

Date:Dec 23, 2008

No. Reader:-1

No. Helpful:-1

Overall:5

Value:4

Rooms:5

Content:Lovely hotel, unique decor, friendly front desk staff, central location It's only recently that I've started staying at boutique hotels, so I'm definitely not an expert on the subtleties that these types of establishments provide. But I've already stayed at a few Kimptons and I must say that my experiences are mostly good. Hotel Monaco is no exception. The decor at the Kimpton Hotels is always unique - they have a wonderful way of pulling off colors, textures and patterns so one feels cool and trendy yet comfortable. The lobby and lounge were decorated really nicely in very rich tones. Our room provided a nice view and had this modern, romantic, shabby-chic feel to it, for lack of a better description. All the staff was friendly and helpful without any attitude. Sazerac Restaurant served us delicious breakfast in a really great setting. Seattle is a cute town in and of itself, so the hotel only enhanced our stay. The location was ideal! We walked everywhere and had no need for a taxi. There are lots of restaurants nearby, and it only took a good 10-15 minutes to walk to Pike Place Market. From what I remember, the library is right across the street and is a must see. Gorgeous architecture. Overall, this was a nice hotel and I'd stay here again. I only gave it 4-stars because I recently stayed at a Shangri-La and was so wowed by the experience. But if I hadn't been, the Hotel Monaco would've gotten another star! 

Date:Nov 13, 2008

No. Reader:-1

No. Helpful:-1

Overall:4

Value:5

Rooms:4

I'm trying to remove the "Content:" part using sed and store the trailing paragraph in an array at one index.

From peoples answers so far I have

Content=( "$(sed -n -e '/<Content>.*/p' $file | cut -d">" -f2-)" )

So what im trying to get is

Content[0]=Wonderful time- even with the snow! What a great experience! From the goldfish in the room (which my daughter loved) to the fact that the valet parking staff who put on my chains on for me it was fabulous. The staff was attentive and went above and beyond to make our stay enjoyable. Oh, and about the parking: the charge is about what you would pay at any garage or lot- and I bet they wouldn't help you out in the snow! 

and

Content[1]=Lovely hotel, unique decor, friendly front desk staff, central location It's only recently that I've started staying at boutique hotels, so I'm definitely not an expert on the subtleties that these types of establishments provide. But I've already stayed at a few Kimptons and I must say that my experiences are mostly good. Hotel Monaco is no exception. The decor at the Kimpton Hotels is always unique - they have a wonderful way of pulling off colors, textures and patterns so one feels cool and trendy yet comfortable. The lobby and lounge were decorated really nicely in very rich tones. Our room provided a nice view and had this modern, romantic, shabby-chic feel to it, for lack of a better description. All the staff was friendly and helpful without any attitude. Sazerac Restaurant served us delicious breakfast in a really great setting. Seattle is a cute town in and of itself, so the hotel only enhanced our stay. The location was ideal! We walked everywhere and had no need for a taxi. There are lots of restaurants nearby, and it only took a good 10-15 minutes to walk to Pike Place Market. From what I remember, the library is right across the street and is a must see. Gorgeous architecture. Overall, this was a nice hotel and I'd stay here again. I only gave it 4-stars because I recently stayed at a Shangri-La and was so wowed by the experience. But if I hadn't been, the Hotel Monaco would've gotten another star!

and so on...

I then try to loop through this array and print out this statement:

for ((i=1; i<${arraylength}+1; i++));
    do
        echo  INSERT INTO tbl1 VALUES (${Content[$i-1]}) >> filltable.sql
    done

However it collectively joins all the content into one big string instead of each Content in its respective index

INSERT INTO tbl1 VALUES (Wonderful time- even with the snow! What a great experience! From the goldfish in the room (which my daughter loved) to the fact that the valet parking staff who put on my chains on for me it was fabulous. The staff was attentive and went above and beyond to make our stay enjoyable. Oh, and about the parking: the charge is about what you would pay at any garage or lot- and I bet they wouldn't help you out in the snow!Lovely hotel, unique decor, friendly front desk staff, central location It's only recently that I've started staying at boutique hotels, so I'm definitely not an expert on the subtleties that these types of establishments provide. But I've already stayed at a few Kimptons and I must say that my experiences are mostly good. Hotel Monaco is no exception. The decor at the Kimpton Hotels is always unique - they have a wonderful way of pulling off colors, textures and patterns so one feels cool and trendy yet comfortable. The lobby and lounge were decorated really nicely in very rich tones. Our room provided a nice view and had this modern, romantic, shabby-chic feel to it, for lack of a better description. All the staff was friendly and helpful without any attitude. Sazerac Restaurant served us delicious breakfast in a really great setting. Seattle is a cute town in and of itself, so the hotel only enhanced our stay. The location was ideal! We walked everywhere and had no need for a taxi. There are lots of restaurants nearby, and it only took a good 10-15 minutes to walk to Pike Place Market. From what I remember, the library is right across the street and is a must see. Gorgeous architecture. Overall, this was a nice hotel and I'd stay here again. I only gave it 4-stars because I recently stayed at a Shangri-La and was so wowed by the experience. But if I hadn't been, the Hotel Monaco would've gotten another star!)
  • The text following content is on separate lines, so I'll re-edit the question. I am storing each content part into an array and then looping through it and printing them into a sql query "INSERT INTO .... VALUES (content) – tom Apr 22 '16 at 13:37
  • have added the sql part too to show you what im trying to achieve – tom Apr 22 '16 at 14:21
  • How about sed -n 's/^Content:\(.*\)/INSERT INTO tbl1 VALUES (\1)/p' infile >> filltable.sql ? – don_crissti Apr 22 '16 at 14:25
  • No that doesn't seem to work either, is there a way I could cut the text from the end of the colon in Content: to the start of Date: and then store that in an array – tom Apr 22 '16 at 14:32
  • It does work with your input sample. Unless the text in Content: is multiple lines - and if so you should edit your post to reflect that. As to that array stuff that you keep bringing up - please read Steven's post here – don_crissti Apr 22 '16 at 14:39
0

This bash script will use grep to retrieve lines starting with Content:, remove the leading "Content:" using the string manipulation features of bash and store each line as an array element:

#!/bin/bash

declare -a array=()
while read line; do
    array+=("${line##Content:}")
done < <(grep "^Content:" file)

# Display array elements
for a in "${array[@]}"; do
    echo "CONTENT: " $a
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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