Sponsored Content
Top Forums Shell Programming and Scripting 'for LINE in $(cat file)' breaking at spaces, not just newlines Post 302548609 by natedawg1013 on Thursday 18th of August 2011 10:19:04 PM
Old 08-18-2011
Quote:
Originally Posted by Corona688
Here's an awesomely ugly bash mini-xml parser:

Code:
#!/bin/bash

TAG=()
TPOS=0

# Turn <tag> <stuff> into < tag > \n < stuff >
sed 's#># >\n#g;s#<\(/*\)#<\1 #g;' > /tmp/$$
...
...

Wow you're right that looks ugly lol.

@yazu:
there won't be any random escapes or anything in the XML. I will be preprocessing it, so I know what will be in it and I can filter out anything I don't want. Also, this is highly application-specific. I plan on using it on one XML that just keeps being updated with different data.

After all this fuss, I think I should give you guys my point and see if you have better sugestions. I am running a linux home media server. I use pianobar for pandora, will soon be setting up a music player for my local audio and was hoping to use something like this:

*turns out I can't post links yet. Google Search "script listen to iheartradio", it's the top result from the maemo site.*


to stream internet radio. My problems with it are:
*keeps spitting out song info because of "print" in a loop
*no way to change stations on the fly
*has a timeout
*doesn't have simple arguments like:
--scriptname channel <channelname>
--scriptname stop
--scriptname info
etc.

I knew if I could extract the url and song/artist info from the XML, I would be able to easily write a shell script that could accomplish all of this. That turned out to be a much tougher task then expected. If any of you have python experience and can give some suggestions, please let me know. I would be teaching myself python right now, but I'm too busy and as you can see, I am still having trouble with bash.

Edit:

The main thing I want is to know what is going on. Please whoever answers following this, please include comments in code, descriptive variables, or an explanation. Thank you.

and @corona688:
my code looked surprisingly similar to yours once I analyzed what yours did

And yeah, I should have stated this all from the start. I just wanted a little help where I was stuck so I could try to figure the rest out myself, but now I do see how hard it is to work with XMLs in bash. I guess I was going completely in the wrong direction. Besides, with my code the way it was, it worked fine for anything without a space, but it took 30 seconds to parse. That is 3 times as long as the original python script's refresh interval!!!

Last edited by natedawg1013; 08-18-2011 at 11:35 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cat'ing a multiple line file to one line

I am writing a script that is running a loop on one file to obtain records from another file. Using egrep, I am finding matching records in file b, then outputing feilds of both into another file. **************************** filea=this.txt fileb=that.txt cat $filea | while read line do... (1 Reply)
Discussion started by: djsal
1 Replies

2. UNIX for Dummies Questions & Answers

newb help! file name with spaces breaking up when trying to retrieve it

for file in `ls *.txt` do sed '/s/old/new/g' $file > /tmp/tempfile.tmp mv /tmp/tempfile.tmp $file done the txt files names look like "text file one.txt", "text file two.txt" but when I run it, all i get is: sed: 0602-419 Cannot find or open file text. sed: 0602-419 Cannot find or... (3 Replies)
Discussion started by: DeuceLee
3 Replies

3. Shell Programming and Scripting

Breaking line

My input file is like USER_WORK.ABC USER_WORK.DEF I want output file like ABC DEF (4 Replies)
Discussion started by: scorp_rahul23
4 Replies

4. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

5. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

6. Shell Programming and Scripting

split a line of a file and cat a file with another

Hi, I have two files one.txt laptop boy apple two.txt unix linux OS openS I want to split one.txt into one line each and concatenate it with the two.txt output files onea.txt laptop (4 Replies)
Discussion started by: avatar_007
4 Replies

7. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

8. AIX

Script to cat and dd last line!!! of each file

hi Guys, Am new to this awesome forum, and yea i need some help here asap thnx :) i have a directory with over 34000 text files, i need a script that will delete the last line of each of this file without me necessary opening the files. illustration:- file1 200 records file2 130 records... (5 Replies)
Discussion started by: eetang
5 Replies

9. UNIX for Dummies Questions & Answers

Sendmail with cat adding extra spaces in email body

when I try to read a file and send email using cat and sendmail: The email received having additional spaces.(Between the letters of words in the text) My code: export MAILTO="sa@y.com" export SUBJECT="mydomain PREPROD MONITOR AT ${DATE}" export... (5 Replies)
Discussion started by: visitsany
5 Replies

10. Shell Programming and Scripting

sed a multiple line pattern that has newlines followed by text and r

Here is the text that I was to run sed on. In this text I want to insert a semi colon ';' before 'select a13.STORE_TYPE STORE_TYPE,' and after 'from ZZMR00 pa11' Input text: insert into ZZMQ01 select pa11.STATE_NBR STATE_NBR, pa11.STORE_TYPE STORE_TYPE, ... (9 Replies)
Discussion started by: v_vineeta11
9 Replies
platform::shell(n)					       Tcl Bundled Packages						platform::shell(n)

__________________________________________________________________________________________________________________________________________________

NAME
platform::shell - System identification support code and utilities SYNOPSIS
package require platform::shell ?1.1.4? platform::shell::generic shell platform::shell::identify shell platform::shell::platform shell _________________________________________________________________ DESCRIPTION
The platform::shell package provides several utility commands useful for the identification of the architecture of a specific Tcl shell. This package allows the identification of the architecture of a specific Tcl shell different from the shell running the package. The only requirement is that the other shell (identified by its path), is actually executable on the current machine. While for most platform this means that the architecture of the interrogated shell is identical to the architecture of the running shell this is not generally true. A counter example are all platforms which have 32 and 64 bit variants and where a 64bit system is able to run 32bit code. For these running and interrogated shell may have different 32/64 bit settings and thus different identifiers. For applications like a code repository it is important to identify the architecture of the shell which will actually run the installed packages, versus the architecture of the shell running the repository software. COMMANDS
platform::shell::identify shell This command does the same identification as platform::identify, for the specified Tcl shell, in contrast to the running shell. platform::shell::generic shell This command does the same identification as platform::generic, for the specified Tcl shell, in contrast to the running shell. platform::shell::platform shell This command returns the contents of tcl_platform(platform) for the specified Tcl shell. KEYWORDS
operating system, cpu architecture, platform, architecture platform::shell 1.1.4 platform::shell(n)
All times are GMT -4. The time now is 06:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy