Splitting a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Splitting a line
# 1  
Old 07-04-2007
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?
# 2  
Old 07-04-2007
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
# 3  
Old 07-04-2007
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
# 4  
Old 07-04-2007
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
# 5  
Old 07-05-2007
Thanks, I'm trying this, but I'm getting the error message

nl: data1: No such file or directory

Simon
# 6  
Old 07-05-2007
And I'm fooling around with the sed string outside of the script, and it's not actually working, period.
s/==Document\n/==\nDocument/
# 7  
Old 07-05-2007
Hi, Simon.
Quote:
Originally Posted by spindoctor
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 05:53 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a line in two variables

Hello. The file /etc/fstab contains UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 acl,user_xattr 1 1I fill a variable SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 )I want PART1=>>>>>UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 ... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Splitting one line into multiple lines

Hi, I have one problem scenorio as below. my source file is : cat input_file. "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou." my output should be like. "hi","there","how","are","you?", "It","was","great","working","with",... (7 Replies)
Discussion started by: abhilash_nakka
7 Replies

3. Shell Programming and Scripting

Splitting Single line into multiple line

Hi All, I am reading a line from a file and writing it to other file. Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7... (2 Replies)
Discussion started by: diehard
2 Replies

4. Shell Programming and Scripting

Splitting file based on line numbers

Hello friends, Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc. f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3 I need to split into fixed number of file like 4 files... (2 Replies)
Discussion started by: Rizzu155
2 Replies

5. Shell Programming and Scripting

Splitting a line with pattern in Sed - Unix

I have a file with single line, that line contains just like the following sample 00200100293^30^1^bla bla ...._______To: zabell00200100293^30^3^aSub00200100293^30^4^ellaCc: Sanders,De on my desk__________00200100293^30^4^___________________________________00A00ABC0293^30^1^something___To: some... (10 Replies)
Discussion started by: Vasan
10 Replies

6. Shell Programming and Scripting

Splitting 12M line file

I have a 12M line file that I need to split into smaller files but I'm getting a csplit: virtual memory exhausted error How can I quickly split this file? This is sze -r-xr-xr-x+ 1 VERGE Domain Users 1158288000 May 4 16:31 inputfile This is the command I've tried... csplit... (4 Replies)
Discussion started by: verge
4 Replies

7. Shell Programming and Scripting

Need help in splitting a line into fields in shell scripting

I have a line of more than 3000 bytes which will contain & as fields separator..I am using following awk command ..Its working but its not accepting the line more than 3000 bytes...Anyother alternate solution even in othe shell command also fine... awk -F '&' '{for( i=1; i<=NF; i++ ) print $i}'... (2 Replies)
Discussion started by: punithavel
2 Replies

8. Shell Programming and Scripting

Splitting the line in multiple lines

Hi Guys, I need a help. I am pretty new to the shell level programing. I was trying to split a long lines to a multiple lines with few conditions. I goggle for the code and found some snippets and tried to modified it but I got few strange problems too. I have to split the lines if line is ... (3 Replies)
Discussion started by: dd_sh
3 Replies

9. UNIX for Dummies Questions & Answers

Line Splitting

Hi, I want to make a new line between for eg: 0102030405 to make it look like: 01 02 03 04 05 by using sed commands. I'm just wondering how would I do this? p.s i'm new to this unix programming. (7 Replies)
Discussion started by: evoGage
7 Replies

10. UNIX for Dummies Questions & Answers

Splitting a line up

9600012301F TF02FT T03FFTF04TF 05T FF06TTTT I have to split this line up into this 96000123 01F TF 02FT T 03FFTF 04TF 05T FF 06TTTT Can anyone please Help?? (1 Reply)
Discussion started by: lilas
1 Replies
Login or Register to Ask a Question