Split line before the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split line before the pattern
# 1  
Old 10-15-2008
Split line before the pattern

Hi,

I am trying to use awk to split line before the pattern:

abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890 abcde 12345 67890

I need it to be like this:

abcde 12345 67890
abcde 12345 67890
abcde 12345 67890
abcde 12345 67890
abcde 12345 67890

What the easiest way to do it?

Thanks,
DJ
# 2  
Old 10-15-2008
Code:
sed 's/abcde/~abcde/g' < inputfilename | tr -s '~' '\n' > newfile

one way.
# 3  
Old 10-15-2008
This is what I needed. Thanks!
# 4  
Old 10-17-2008
Code:
cat filename | tr 'abcde' '\nabcde'

# 5  
Old 10-17-2008
Quote:
Originally Posted by summer_cherry
Code:
cat filename | tr 'abcde' '\nabcde'

tr is not replacing the string, the above command will change all the occurance of
'a' with '\n'
'b' with 'a'
'c' with 'd'
'e' with ''
# 6  
Old 10-17-2008
Code:
cat filename | tr 'abcde' '\nabcde'

Quote:
Originally Posted by ranjithpr
tr is not replacing the string, the above command will change all the occurance of
'a' with '\n'
'b' with 'a'
'c' with 'd'
'e' with ''
the above code works fine here..
# 7  
Old 10-17-2008
Or:
(use /usr/xpg4/bin/sed on Solaris)

Code:
sed 's/\([^ ]* \)\{3\}/&\         
/g' infile


If your sed implementation does not support re-interval:

Code:
sed 's/\([^ ]* [^ ]* [^ ]* \)/&\
/g' infile

or:

Code:
printf "%s %s %s\n" $(<infile)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Search for a pattern in a file and split the line into two lines

Hi All, Greetings everyone !!! I have a file which has many lines, out of which one line is as below. I need to search for pattern "varchar(30) Select" and if exists, then split the line as below. I am trying to achieve this in ksh. Can anyone help me on this. (8 Replies)
Discussion started by: Pradhikshan
8 Replies

4. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. UNIX for Dummies Questions & Answers

split record without pattern

Hi , I have file with all records in one line, which needs to split it to have a fixed length.Am trying to execute the below script for the same FILENAME="$1" while line LINE do echo $LINE | awk 'BEGIN{n=1}{while(substr($0,n,10)){print substr($0,n,10);n+=10}}' done < $FILENAME it... (4 Replies)
Discussion started by: nishantrk
4 Replies

7. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

8. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

9. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question