LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-11-2008, 10:36 AM   #1
starplex
LQ Newbie
 
Registered: Jul 2007
Posts: 2

Rep: Reputation: 0
awk or sed programming question


I have a word list in which every word is on a new line. I need to add text to it. a little example of what i want to do.
Word List:

apple
computer
beer

Text to be inserted for each word:
Code:
<pardef n="THE WORD__n">
      <e>
        <p>
          <l/>
          <r><s n="noun"/><s n="singular"/></r>
        </p>
      </e>
      <e>
        <p>
          <l>s</l>
          <r><s n="noun"/><s n="plural"/></r>
        </p>
      </e>
    </pardef>
How do I do this in sed or awk. It seems to me like every line/word needs to be turned into a variable and than use a for loop or something.

Thank you in advance.
 
Old 07-11-2008, 10:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using awk you don't need a loop, since awk parses a text file line by line. You simply have to give your world list as argument to awk and write the proper print statements, inserting $0 in place of THE WORD. Otherwise you can do a simple shell script which reads the world list and echoes the proper lines, like:
Code:
while read word
do
  echo "<pardef n=\"${word}__n\">"   >> output_file
  echo "      <e>"                   >> output_file
  ....
done < <(cat world_list)
just escaping the literal double quotes, and eventually other special characters. Or you can create a little text file as a template, containing the lines you want to insert and a pattern to be substituted by sed, for example THE_WORD. Then again in a loop:
Code:
while read word
do
  sed s/THE_WORD/$word/g template_file   >> output_file
done < <(cat world_list)
 
Old 07-11-2008, 11:24 AM   #3
starplex
LQ Newbie
 
Registered: Jul 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Than you very much. I owe you a beer.
 
  


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
Sed or Awk question, looking for parsing help rwartell Linux - Software 2 05-17-2006 11:59 PM
Sed or Awk question, looking for parsing help rwartell Programming 1 05-17-2006 04:42 PM
I have a question about awk or sed sqp1982 Programming 9 03-28-2006 05:37 AM
Difficult sed/awk question 3saul Linux - Software 2 03-04-2006 02:49 AM
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM

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

All times are GMT -5. The time now is 02:09 PM.

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