How to get new line after certain character.?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get new line after certain character.?
# 1  
Old 11-03-2014
How to get new line after certain character.?

Hello,

I have a file like:-
Code:
My Name is ABCMy Name is XYZMy Name is PQR

I want the output like
Code:
My Name is ABC
My Name is XYZ
My Name is PQR

Please help. The length is fixed for each line.

Last edited by vbe; 11-03-2014 at 09:40 AM.. Reason: please use code tags, even for so little...
# 2  
Old 11-03-2014
Might not be the best way, but this seems to work for me:

Code:
 
> cat ff
My Name is ABCMy Name is XYZMy Name is PQR
> cat ff | sed "s/My Name/#My Name/g" | tr '#' '\012'
My Name is ABC
My Name is XYZ
My Name is PQR
>

Assumes no "#" character in the text. If you have one, just find some odd character you don't have in your text:
ie ■²√°≈÷⌡⌠≤≥±≡ ... etc
(those are all ASCII codes up in 240-250 area ... Smilie )
# 3  
Old 11-03-2014
Quote:
Originally Posted by Abhisrajput
Hello,

I have a file like:-
Code:
My Name is ABCMy Name is XYZMy Name is PQR

I want the output like
Code:
My Name is ABC
My Name is XYZ
My Name is PQR

Please help. The length is fixed for each line.
Hello Abhisrajput,

Please used codes tags as per forum rules. Following may help you in same.

Code:
echo "My Name is ABCMy Name is XYZMy Name is PQR" | awk '{gsub(/My/,"\nMy",$0);S=S?S ORS $0:$0} END{gsub(/^[[:space:]]/,X,S);print S}'

Output will be as follows.
Code:
My Name is ABC
My Name is XYZ
My Name is PQR

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-03-2014 at 10:53 AM..
# 4  
Old 11-03-2014
How about:
Code:
sed s,"My Name","\nMy Name",g -i test.txt

hth
# 5  
Old 11-03-2014
The statements you have provided are kind of ambiguous. On the one hand you want a new line *after* certain character (which character?), on the other hand you mention fixed line length (which line?).

Maybe this will suffice:
Code:
fold -w 14 file

# 6  
Old 11-03-2014
Quote:
Originally Posted by RavinderSingh13
Hello Abhisrajput,

Please used codes tags as per forum rules. Following may help you in same.

Code:
echo "My Name is ABCMy Name is XYZMy Name is PQR" | awk '{gsub(/My/,"\nMy",$0);S=S?S ORS $0:$0} END{gsub(/^[[:space:]]/,X,S);print S}'

Output will be as follows.
Code:
My Name is ABC
My Name is XYZ
My Name is PQR

Thanks,
R. Singh
What's the pre-requisite for that to work?
It just throws an error when I try:

Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

I'm on SunOS 5.10
So no bash, just basic unix/ksh as far as I know Smilie

Quote:
Originally Posted by sea
How about:
Code:
sed s,"My Name","\nMy Name",g -i test.txt

hth
doesn't work for me either

Code:
 
 > sed s,"My Name","\nMy Name",g -i test.txt
Can't open -i
nMy Name is ABCnMy Name is XYZnMy Name is PQR

again, just curious what the base requirements for that to work is ?
# 7  
Old 11-03-2014
>I'm on SunOS 5.10

Change awk to nawk and try again.

> doesn't work for me either (Can't open -i)

-i is an option for in place editing. It's a feature of GNU sed, thus it's not available in default installations of commercial Unixes, afaik.

Solution: sed ... file >file.tmp && mv file.tmp file

\n is another feature of GNU sed Smilie

Solution:
Code:
sed s,"My Name","\\
My Name",g file

Btw. the commands mentioned in post 2, 3 and 4 produce an empty line on the top of the output.
This User Gave Thanks to junior-helper For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

3. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

4. Shell Programming and Scripting

awk new line issue, saying string can't contain new line character

Hi , I am doing some enhancements in an existing shell script. There it used the awk command in a function as below : float_expr() { IFS=" " command eval 'awk " BEGIN { result = $* print result exit(result == 0) }"' } It calls the function float_expr to evaluate two values ,... (1 Reply)
Discussion started by: mady135
1 Replies

5. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

6. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

7. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

8. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies

9. UNIX for Dummies Questions & Answers

Need to serach if a new line character exists on the last line in a file

I have a file in which I need to search if a new line character exists on the last line in the file. Please let me know how can I achieve it using Unix commands? (10 Replies)
Discussion started by: sunilbm78
10 Replies

10. UNIX for Dummies Questions & Answers

delete a line based on first character of the line

Hi, I need to delete all lines in a file which starts with "|" character. Can some one assist me? Thanks (2 Replies)
Discussion started by: borncrazy
2 Replies
Login or Register to Ask a Question