LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-01-2007, 08:00 AM   #1
tnik
LQ Newbie
 
Registered: Apr 2004
Distribution: Gentoo / winXP
Posts: 2

Rep: Reputation: 0
how to use grep/sed


I have a text file I'm trying to split up into multiple files.. the format is like this:

Code:
O1234 (filename.ext)
(text here)
X123.23 Y023.
G42 X2234. Y0.
M30

01235 (diffilename.ext)
(text here)
X123.23 Y023.
G42 X2234. Y0.
X123.23 Y023.
M30

01236
(text here)
X123.23 Y023.
G42 X2234. Y0.
X123.23 Y023.
M30
Now what I would like to do is parse the file, if (filename.ext) exists after the O##### then name the file that, and put everything from O### to M30 in it. If it doesn't exist, just use the O##### as the filename.

I'm not even sure where to start on this.. Any help would be greatly appreciated.

Thanks

-Tom
 
Old 11-01-2007, 11:43 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
It's not sed but

Code:
filename=
cat file | while read line
do    
        x1=$(echo $line | cut -f1 -d" ")
        echo $x1 | grep -q '^0[0-9]*'
        if [ $? = 0 ]
        then
           filename=$x1
           x2=$(echo $line | cut -f2 -d" ")
           [ -n $x2 ] && filename=$(echo $x2 | tr -d '()')
        fi
        echo "$line" >>$filename
done
 
Old 11-04-2007, 12:18 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
And an awk-version ... :}
Code:
BEGIN{
  RS=ORS="\n\n";FS=OFS="\n"
}
{
  file=$1
  if( $1 ~ /\(/ ){
    file=gensub( /.+ \(([^\)]+)\)/ , "\\1", 1, $1)
  }
  print $0 > file
}

Cheers,
Tink
 
Old 11-06-2007, 07:51 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,647
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
There are four great tools in the Unix/Linux world that are terrific for handling problems like these. I'll introduce them individually....

sed ("stream editor") is very useful when you have a single file that you want to do something to, to produce another single file as output. (In Linux/Unix-land, this idea is often applied as a "filter" when "piping" things ... but that's another story...)

grep is a great tool for finding which files contain a particular string. It grows on you... For example, when I needed to find all of the files in a great-big directory (which contains over 3,500 files in various subdirectories) which contained the word "arp" as a whole-word (that is to say, surrounded on both sides by a character that is not a letter), regardless of UPPer or LoWeR CAse, I "merely" typed: grep -rilw arp ~/projects/* Nothin' to it...

awk is probably the tool that you want in this case. The file that you need to process has certain definitely-identifiable characteristics, such as:
  • There's one "record" per "line," and it seems that "fields" in each "record" are separated by "one or more spaces."
  • "A line that begins with 'O' followed by one-or-more 'digits'" marks the beginning of "something I am interested in," and when I see such a record, "the second field" (filename) "is interesting."
  • After I have seen a record like that, zero-or-more records contain useful text...
  • "But when I see a record starting with 'X' followed by zero-or-more 'digits'" I want to...
Well, you get the idea. awk is a tool that's designed for things like that.

For the truly adventurous, the programming language perl was actually designed by a person who started his quest by "extending awk" and ... well ... "one thing lead to another," as things in our peculiar industry so-often do.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 08:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
How can I awk/sed/grep the IPs from the maillog? abefroman Programming 7 03-09-2006 10:22 AM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 06:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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