从字符串中删除模式,然后将整个字符串存储到数组中的一个索引中

从字符串中删除模式,然后将整个字符串存储到数组中的一个索引中

所以我有一个包含许多字段的文件,这些字段以“Content:”开头,如下所示......

注意:内容跨多行

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

我正在尝试使用 sed 删除“Content:”部分,并将尾随段落存储在数组中的一个索引处。

到目前为止我从人们的回答中得到了

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

所以我想要得到的是

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! 

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!

等等...

然后我尝试循环遍历这个数组并打印出以下语句:

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

然而,它将所有内容共同连接成一个大字符串,而不是各自索引中的每个内容

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!)

答案1

bash脚本将用于grep检索以 开头的行Content:,使用 的字符串操作功能删除前导的“Content:”,bash并将每行存储为数组元素:

#!/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

相关内容