Print a newline after first match in line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print a newline after first match in line
# 1  
Old 04-04-2013
Print a newline after first match in line

Hi everyone
I have a file where CP occurs both within each line and at the very end:

dwer 17 knsdask= * CP hwla 17 h'wopie un CP

I would like to separate the line on the first CP to get:

dwer 17 knsdask= * CP
hwla 17 h'wopie un CP


What I have so far is:
Code:
awk '{for (x=1; x<NF; x++)
             {if ($x~/CP/)
                {printf "%s\n"} }}' file

Reagrds

Last edited by radoulov; 04-04-2013 at 10:07 AM..
# 2  
Old 04-04-2013
One way:

Code:
awk '{sub(/CP/,"CP\n");}1' file

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 04-04-2013
Thank you so much that works. But one problem is a space at the beginning after the newline.

---------- Post updated at 12:38 PM ---------- Previous update was at 12:27 PM ----------

Ok turns out it's pretty easy to remove white space at the beginning of lines:
Code:
sed 's/^ *//' file


Last edited by radoulov; 04-04-2013 at 10:07 AM..
# 4  
Old 04-04-2013
Quote:
Originally Posted by meet77
Thank you so much that works. But one problem is a space at the beginning after the newline.

---------- Post updated at 12:38 PM ---------- Previous update was at 12:27 PM ----------

Ok turns out it's pretty easy to remove white space at the beginning of lines:
sed 's/^ *//' file
I guess that's the continuation of ur original input.

Try
Code:
awk '{sub("CP ","CP\n");}1' file

This User Gave Thanks to PikK45 For This Post:
# 5  
Old 04-04-2013
Quote:
Originally Posted by meet77
Ok turns out it's pretty easy to remove white space at the beginning of lines:
Code:
sed 's/^ *//' file

Hope this helps

Code:
awk '{sub(/CP$/,"CP\n");sub(/^ */,"")}1' file

# 6  
Old 04-04-2013
Code:
sed 's/CP /&\
/' file

The \ followed by a new line does the trick. The & re-inserts the match i.e. "CP "
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

2. Shell Programming and Scripting

Match string from two files and print line

Hi, I have been trying to find help with my issue and I'm thinking awk may be able to do it. I have two files eg file1.txt STRING1 230 400 0.36 STRING2 400 230 -0.13 STRING3 130 349 1 file2.txt CUFFFLINKS 1 1394 93932 . + STRING1 CUFFFLINKS ... (9 Replies)
Discussion started by: zward
9 Replies

3. AIX

Print nth previous line after match

Please help me print nth line after match awk or sed one line command. (3 Replies)
Discussion started by: sushma123
3 Replies

4. Shell Programming and Scripting

How to match the first word and print only that line in UNIX?

Below is the file DISK-A 109063.2 49 31 40.79 DISK-B 110058.5 49 44 57.07 DISK-c 4402.4 2 1 2.14 from the file, i want to search for 'DISK-A' and print only that line with the first word matching to DISK-A and the output should skip DISK-A. Desired Output: (If i'm... (2 Replies)
Discussion started by: web2moha
2 Replies

5. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies

6. UNIX for Dummies Questions & Answers

MATCH A PATTERN AND PRINT A LINE ABOVE AND BELOW

Dear All, Hv a very specific requirement. I have a very large text file and in which I have to match a pattern and insert a line above and below. Eg: My file cat test date1 date2 date3 date4 I need to match 'date3' and insert "Reminder1" above date3 and insert 'reminder2'... (4 Replies)
Discussion started by: gokulj
4 Replies

7. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

8. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

9. Shell Programming and Scripting

match a pattern and print the line once

Hi, I have a xml file <cisco:name> <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm> <cisco:meNm>10.1.100.19</cisco:meNm> <cisco:ehNm>/shelf=1</cisco:ehNm> <cisco:subname> <cisco:meNm>10.1.100.19</cisco:meNm> <cisco:sptp>Cisco PortA Series</cisco:sptp> ... (11 Replies)
Discussion started by: bhagirathi
11 Replies

10. Shell Programming and Scripting

match a pattern, print it and the next line

I have a file nbu_faq.txt (Question/answer) which looks like this What I am trying to do is write out each question in a file1.txt and than the question/answer in a file2.txt like this file1.txt Q: What is nbu? Q: What is blablabla...? Q: Why ....? file2.txt Q: What is nbu? A:... (4 Replies)
Discussion started by: nymus7
4 Replies
Login or Register to Ask a Question