Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 11-01-2007, 09:00 AM   #1
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:

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

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

(text here)
X123.23 Y023.
G42 X2234. Y0.
X123.23 Y023.
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.


Old 11-01-2007, 12:43 PM   #2
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
It's not sed but

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

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And an awk-version ... :}
  if( $1 ~ /\(/ ){
    file=gensub( /.+ \(([^\)]+)\)/ , "\\1", 1, $1)
  print $0 > file

Old 11-06-2007, 08:51 PM   #4
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,669
Blog Entries: 4

Rep: Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020Reputation: 3020
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.


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 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 01:34 AM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 09:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
How can I awk/sed/grep the IPs from the maillog? abefroman Programming 7 03-09-2006 11:22 AM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 07:30 AM > Forums > Linux Forums > Linux - General

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

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