![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 05:59 PM |
| Splitting a single line into multiple lines | thanuman | Shell Programming and Scripting | 4 | 02-23-2005 12: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 08:34 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
||||
|
If you really want to prepend Document to the beginning of the following line, e.g
Code:
======Document foo Code:
====== Documentfoo 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
ZB |
|
||||
|
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
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
|
|
||||
|
Hi, Simon.
Quote:
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. |
||||
| Google The UNIX and Linux Forums |