[SOLVED] bash: Split a text file into an array? (NOT line-by-line)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash: Split a text file into an array? (NOT line-by-line)
Hi all. First-time poster here...
Say I have a file (called twitterstatus.tmp) that looks like this:
Code:
<status>
<id>24854489768</id>
<text>Are we gonna ride the sun home?</text>
<id>55266987</id>
<screen_name>dj_johnnyfever</screen_name>
</status>
<status>
<id>24852047832</id>
<text>@dj_johnnyfever Hey Johnny! Can you see this yet?</text>
<id>51269031</id>
<screen_name>DJCharlieKJSR</screen_name>
</status>
<status>
<id>24845941995</id>
<text>Dog... donkey... Well, they both start with the letter "N"...</text>
<id>55266987</id>
<screen_name>dj_johnnyfever</screen_name>
</status>
How could I feed this into an array, with each element containing everything between the <status> </status> tags?
No. Once I have each segment set, I'll be searching for a specific string contained in the segment to act on.
So, from the sample I posted, say the script sees this segment:
Code:
<status>
<id>24852047832</id>
<text>@dj_johnnyfever Hey Johnny! Can you see this yet?</text>
<id>51269031</id>
<screen_name>DJCharlieKJSR</screen_name>
</status>
It would trigger on the @dj_johnnyfever keyword, and act accordingly.
The trouble I'm having is splitting the file into segments.
Since that's XML, why not just use the XML functionality to do what you want?
By the way, since this is your first post, I suggest that you "Report" your thread to the moderators and request that they move it to the Programming sub-forum where you'd get much better responses. (It's not, really, a "general" question.)
Well, ideally, I'd prefer it not be in XML. I'm actually stripping out the XML further along in the script. I need plain-text variables for that. But first I need to divide it into easily digestible segments bound by the <status> </status> tags.
eval arr=("$(sed "s/'/'\"'\"'/g; s/<status>/'&/; s/<\/status>/&'/" file)")
echo "${arr[0]}"
<status>
<id>24854489768</id>
<text>Are we gonna ride the sun home?</text>
<id>55266987</id>
<screen_name>dj_johnnyfever</screen_name>
</status>
sed puts each block into single quotes.
s/'/'\"'\"'/g protects any literal single quotes from eval by placing them in double quotes in a gap in the single quotes, e.g.
Code:
......It's now or never....
would become
'......It'"'"'s now or never....'
Basically, each segment I need is 5 lines long. So I do a post=`head -5 twitterstatus.tmp`, scan it for the keyword, and then using sed, delete the top 5 lines of the file. If the keyword is found, split $post into individual variables, and process from there!
Thanks for letting me bounce ideas off you, everyone!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.