The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting the line in multiple lines dd_sh Shell Programming and Scripting 3 03-31-2008 10:54 AM
Line Splitting evoGage UNIX for Dummies Questions & Answers 7 11-30-2005 06:59 PM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 01:56 AM
splitting the files sounder123 Shell Programming and Scripting 1 06-04-2004 04:03 AM
Splitting a line up lilas UNIX for Dummies Questions & Answers 1 03-12-2001 09:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-04-2007
Registered User
 

Join Date: May 2007
Posts: 31
Splitting a line

I have a series of .txt files, that contain lines of text separated by the following string ====================
In some of the .txt files, the string ends with the word Document, leaving the string ==================Document.

I would like to be able to split any such line and move the word "Document" to the next line, appending it to the beginning of the next line.
Any suggestions?
Reply With Quote
Forum Sponsor
  #2  
Old 07-04-2007
drl's Avatar
drl drl is online now
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 546
Hi.

Here is one solution:
Code:
#!/bin/sh

# @(#) s1       Demonstrate sed line split.

set -o nounset
echo " sh version: $BASH_VERSION" >&2
sed --version | head -1

FILE=${1-data1}

echo
echo " Input file:"
nl $FILE

echo
echo " Output"
sed 's/^\(.*====\)\(Document.*\)$/\1\n\2/' $FILE

exit 0
producing:
Code:
% ./s1
 sh version: 2.05b.0(1)-release
GNU sed version 4.1.2

 Input file:
     1  Beginning.
     2  This is a test set of data.
     3  ==============
     4  this is ok.
     5  ==============
     6  this is ok, too.
     7  =============Document
     8  this we want changed.
     9  =============
    10  this is ok again.
    11  End

 Output
Beginning.
This is a test set of data.
==============
this is ok.
==============
this is ok, too.
=============
Document
this we want changed.
=============
this is ok again.
End
cheers, drl
Reply With Quote
  #3  
Old 07-04-2007
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
If you really want to prepend Document to the beginning of the following line, e.g
Code:
======Document
foo
will become
Code:
======
Documentfoo
the following will work:
Code:
#!/bin/bash

vi <<-EOF >/dev/null 2>&1
        :r splitme.txt
        :%s/^\(=*\)\(Document\)\n\(.*\)$/\1\r\2\3/g
        :%s/^\(=*\)\(Document\)$/\1\r\2/g
        :w! splitme.new
        :q
EOF

exit 0
Cheers
ZB
Reply With Quote
  #4  
Old 07-04-2007
drl's Avatar
drl drl is online now
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 546
Hi.

Thanks to ZB for pointing out the part I apparently skipped over. If that is desired, then the sed becomes slightly more complex (or mysterious), and it is easier to build it into a separate sed script file:

% cat s3
Code:
#!/bin/sh

# @(#) s3       Demonstrate sed line split and merge.

set -o nounset
echo " sh version: $BASH_VERSION" >&2
sed --version | head -1

FILE=${1-data1}

echo
echo " Input file:"
nl $FILE

cat >script <<'EOF'
/==Document$/{
N
s/==Document\n/==\nDocument/
}
EOF

echo
echo " Output"
sed -f script $FILE |
nl

exit 0
producing:
Code:
% ./s3
 sh version: 2.05b.0(1)-release
GNU sed version 4.1.2

 Input file:
     1  Beginning.
     2  This is a test set of data.
     3  ==============
     4  this is ok.
     5  ==============
     6  this is ok, too.
     7  =============Document
     8  this we want changed.
     9  =============
    10  this is ok again.
    11  End

 Output
     1  Beginning.
     2  This is a test set of data.
     3  ==============
     4  this is ok.
     5  ==============
     6  this is ok, too.
     7  =============
     8  Documentthis we want changed.
     9  =============
    10  this is ok again.
    11  End
With help from O'Reilly sed & awk, 2nd, Chapter 6 ... cheers, drl
Reply With Quote
  #5  
Old 07-05-2007
Registered User
 

Join Date: May 2007
Posts: 31
Thanks, I'm trying this, but I'm getting the error message

nl: data1: No such file or directory

Simon
Reply With Quote
  #6  
Old 07-05-2007
Registered User
 

Join Date: May 2007
Posts: 31
And I'm fooling around with the sed string outside of the script, and it's not actually working, period.
s/==Document\n/==\nDocument/
Reply With Quote
  #7  
Old 07-05-2007
drl's Avatar
drl drl is online now
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 546
Hi, Simon.
Quote:
Originally Posted by spindoctor View Post
Thanks, I'm trying this, but I'm getting the error message

nl: data1: No such file or directory

Simon
You didn't post a data file so I made one up. It's listed in a few posts, but I'll post it below ... cheers, drl
Code:
Beginning.
This is a test set of data.
==============
this is ok.
==============
this is ok, too.
=============Document
this we want changed.
=============
this is ok again.
End

Last edited by drl; 07-05-2007 at 01:53 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0