LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2005, 04:36 PM   #1
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Rep: Reputation: 15
Parsing a File in a Bash Script


(BTW: if you are too lazy to read all of the following, then skip to the last paragraph)

Hello Guys. I'm learning how to write shell scripts in bash, and for my first project I am making a small "survey" program.

I need to make use of the following:

# This is a Comment
<name> ... </name>
<description> ...</description>
<q type=mc> ... </q>
<q type=fr> ... </q>
<op> ... </op>

I plan to use the \ character to escape the < and > characters.


Clearly, I need to parse each character individually and keep track of when I close and open different sections.


The problem is that... off the top of my head, I don't know any basic UNIX command that will let me parse the characters in a line!



Here are the following restrictions:
1. The shell script MUST be in bash. Yes, I know something else such as C, Perl, Python, or whatever is much better suited, but that is not what I'm trying to learn
2. I will NOT write a portion of the program (ex: the parser) in C and just use it in the shell script (although honestly, I might reconsider)
3. The command has to be a fairly common and simple command that can be found just about anywhere.


However, I am interested in making use out of either sed or awk- as I am yet to learn those


Question is: What simple, basic UNIX command can I use to parse the characters in a line? the command will be used in a bash shell script, and can also include sed or awk (given that I don't know howto use them- just tell me that they're "capable" of doing so and I will look into it)


Thanks
 
Old 12-01-2005, 05:38 PM   #2
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
sed and/or gawk are the best ways to go about that, however as Im not that fluent with them, ill present an option.. A lousy one, but it can bo done without them.. (;

Just a tad of looping, 'cat foo | head -n $i | tail -n 1' and 'cut -d "<" -f 2' and variations of those should give you a set of functions to build upon.. Ofcourse it wont be as fast or neat as gawk / sed, but it can be done without.. (:
 
Old 12-01-2005, 06:33 PM   #3
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Looks like gawk will be the best solution. Sed works on lines and words, but not characters (at least from what I have seen and heard). Gawk, on the other handle, can be set so that each character is a seperate field... which is exactly what I need


I'll still make use of sed in my program though, but at least I found my solution


Thanks for your opinion
 
Old 12-01-2005, 06:46 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could use sed

Code:
sed 's/\(<\|>\)/\\\1/g' your_file > your_file.new
[sorry]
My bad, I didn't read carefully

Last edited by keefaz; 12-01-2005 at 06:48 PM.
 
Old 12-02-2005, 02:38 PM   #5
TGWDNGHN
Member
 
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94

Original Poster
Blog Entries: 1

Rep: Reputation: 15
...I don't even know what that does lol
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script File: Parsing command sent to shell cheema Programming 1 07-01-2005 12:54 PM
how to creata file through bash script? varala_kanth Linux - Software 2 04-29-2004 02:52 PM
bash script, parsing email addresses kepler Programming 6 01-26-2004 06:47 AM
File Parsing using a Shell Script yasir15 Programming 5 08-22-2003 12:17 PM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration