View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-01-2005, 05:36 PM   #1
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)

Old 12-01-2005, 06:38 PM   #2
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, 07:33 PM   #3
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

Original Poster
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, 07:46 PM   #4
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 137Reputation: 137
You could use sed

sed 's/\(<\|>\)/\\\1/g' your_file >
My bad, I didn't read carefully

Last edited by keefaz; 12-01-2005 at 07:48 PM.
Old 12-02-2005, 03:38 PM   #5
Registered: Aug 2003
Distribution: Slackware 10.0
Posts: 94
Blog Entries: 1

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:54 PM
how to creata file through bash script? varala_kanth Linux - Software 2 04-29-2004 03:52 PM
bash script, parsing email addresses kepler Programming 6 01-26-2004 07:47 AM
File Parsing using a Shell Script yasir15 Programming 5 08-22-2003 01:17 PM
file editing in a bash script Harpune Programming 4 11-23-2002 12:35 AM

All times are GMT -5. The time now is 12:24 PM.

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