LinuxQuestions.org
Review your favorite Linux distribution.
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 03-25-2005, 11:26 PM   #1
eremit
LQ Newbie
 
Registered: Mar 2005
Location: Berlin
Distribution: xandros 2.04
Posts: 7

Rep: Reputation: 0
awk newbie


Hi,

my first post- woohoo!

I'm kinda new to awk scripting, but find it interesting and really powerfull when it comes to textstrings. But now I have encountered some problems; I'm building a database from a really old forum (html-files), and I try to separate posts from eachother. I know that every post starts with: From <username> <email>. Is there some way to get this process automated with awk? I would like every post to be saved in a separate file (eg. post1, post2, post3 etc.)

I have come this far:

awk -f <filename> /From/ { print $0 }

Also, I encounter some problems when the email field is present, it says illegal character in expression...

eremit
 
Old 03-26-2005, 02:39 AM   #2
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
This is my first chance to welcome a first timer, so ... WELCOME

I'm no awk junkie, but what the heck... I tried by creating a sample master file (with incomplete records, empty posts thrown in for good measure)

Code:
[/home/soumen/tmp] $ cat post.txt
12349 234 askasd
12-3kasdweqj

From : a@b.com
asdfasfasf
 sadfasfas
 asdfasf

asdfasf

From : c@d.com
xsdfxsfxsf
 sxdfxsfxs
 xsdfxsf
xsdfxsf

From : e@f.com
From : g@h.com
The awk script is :

Code:
[/home/soumen/tmp] $ cat psplit.sh
#!/bin/bash

awk 'BEGIN {i=0; system("rm post?.txt") } 
        { if (/^From/) {
                ++i
        } else {
                fname="post"i".txt"
                print $0 >> fname
                close(fname)
        }}' post.txt
[/home/soumen/tmp] $
Output:

Code:
[/home/soumen/tmp] $ ./psplit.sh
[/home/soumen/tmp] $ for i in post?.txt ; do echo "--------$i--------"; cat $i; done
--------post0.txt--------
12349 234 askasd
12-3kasdweqj

--------post1.txt--------
asdfasfasf
 sadfasfas
 asdfasf

asdfasf

--------post2.txt--------
xsdfxsfxsf
 sxdfxsfxs
 xsdfxsf
xsdfxsf

[/home/soumen/tmp] $
I'm sure the awk gurus would have a more elegant awk script

HTH
 
Old 03-27-2005, 07:08 AM   #3
eremit
LQ Newbie
 
Registered: Mar 2005
Location: Berlin
Distribution: xandros 2.04
Posts: 7

Original Poster
Rep: Reputation: 0
Great stuff!

Just needed some minor tweaks to fit into my script

Thanks for giving me a helping hand

eremit
 
  


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
....How to use awk to sort.... nisson Linux - General 12 11-02-2012 03:51 PM
About awk script sachin_keluskar Linux - Software 2 06-24-2005 03:19 AM
Newbie playing with Awk and Grep Helene Programming 6 04-15-2004 02:08 AM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM

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

All times are GMT -5. The time now is 11:54 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