use sed to replace whitespace with a carriage return


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers use sed to replace whitespace with a carriage return
# 1  
Old 06-15-2009
use sed to replace whitespace with a carriage return

Greetings

I need to replace "whitespace" in a file with the newline character aka carriage return

My command is either wrong or not interpreted properly by me shell

Code:
 
sed s/" "/\\n" "/g nets > nets1

or

Code:
 
sed s/" "/\n" "/g nets > nets1


nets (input file)
13MHZ_IN U1.AC21 Y2.1;
13MHZ_OUT R19.2 U1.AD21;
32KHZ_IN 46.1 C8.2 U1.AA22;

nets1 (output file)
13MHZ_INn U1.AC21n Y2.1;
13MHZ_OUTn R19.2n U1.AD21;
32KHZ_INn 46.1n C8.2n U1.AA22;

This is the result that I want
13MHZ_IN
U1.AC21
Y2.1;
13MHZ_OUT
R19.2
U1.AD21;
32KHZ_IN
46.1
C8.2
U1.AA22;
# 2  
Old 06-15-2009
redoubtable@Tsunami ~ $ sed 's/\s/\n/g' nets
13MHZ_IN
U1.AC21
Y2.1;
13MHZ_OUT
R19.2
U1.AD21;
32KHZ_IN
46.1
C8.2
U1.AA22;
redoubtable@Tsunami ~ $

The commands you provided work in bash 3.2.29
# 3  
Old 06-15-2009
Would using tr(1) instead of sed(1) be ok?
Code:
$ cat newinputfile
13MHZ_IN U1.AC21 Y2.1;
13MHZ_OUT R19.2 U1.AD21;
32KHZ_IN 46.1 C8.2 U1.AA22;
$ tr ' ' '\012' < newinputfile
13MHZ_IN
U1.AC21
Y2.1;
13MHZ_OUT
R19.2
U1.AD21;
32KHZ_IN
46.1
C8.2
U1.AA22;
$

# 4  
Old 06-15-2009
Thanks, TonyFullerMalv tr did the trick

Thanks a bunch TonyFullerMalv translate (tr) worked like a charmSmilie
I know that \012 is CR what is ' ' ' ? single quoted spaces?

I needed to redirect the STD OUT to a new file, as the file "netlist" is over 4000 lines.

nets1
13MHZ_IN U1.AC21 Y2.1;
13MHZ_OUT R19.2 U1.AD21;
32KHZ_IN 46.1 C8.2 U1.AA22;
5V 380.1 C100.1 C321.1 C322.1 L10.1 L56.2 Q1.4 R88.1 R260.2 R262.1;

Code:
 
tr ' ' '\012' < nets1 > netlist

netlist
13MHZ_IN
U1.AC21
Y2.1;
13MHZ_OUT
R19.2
U1.AD21;
32KHZ_IN
46.1
C8.2
U1.AA22;
5V
380.1
C100.1
C321.1
C322.1
L10.1
L56.2
Q1.4
R88.1
R260.2
R262.1;
# 5  
Old 06-15-2009
The ' ' is literally just a space the character you wish to translate to a newline, so if you also wanted to translate other white spaces to newlines also, they would need to be added to the list, e.g. to translate a null or a space or a tab to a newline it would be:
Code:
tr `\000 \011` '\012'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove carriage return

I need to remove the carriage return comes inbetween the record. Need to have CR only at the end. I used the below command. tr -d '\n' < filewithcarriagereturns > filewithoutcarriagereturns But its removing all the CR and giving one line output. Input File: 12345 abcdegh... (11 Replies)
Discussion started by: srvn_saru
11 Replies

2. Shell Programming and Scripting

Substitute \n with carriage return

Hello all, I've a flat file in the following format: AB\001\CDED\001\ABC\001\nEG\001\HIJF\001\EFG\001\nHI\003\HIUL\003\HIJ\003 And I want to substitute \n with the carriage return. Any help is appreciated! Regards, - Seth (8 Replies)
Discussion started by: sethmj
8 Replies

3. Shell Programming and Scripting

Create Carriage Return using SED

I am hoping someone can help me with a solution to this problem using SED. My issue is actually two-fold. First, I have an order file that is sent to us from another system, however, there has been a change in current processes that requires a carriage return added to the end of each order. The... (5 Replies)
Discussion started by: chino_1
5 Replies

4. Shell Programming and Scripting

sed replace return code

Hi i have multiple sed replacements happening in my script and on those sed replacements, my requirement is to print a message if actually a replacement happened for eg : if sed 's/abc/xyz/g' if ; then print "replacement happended from abc to xyz" do we have any return codes like the... (4 Replies)
Discussion started by: midhun19
4 Replies

5. Shell Programming and Scripting

Replace carriage return with colon on specific column

Hi, I have to process four source text files with possibility to contain carriage return on the particular column. Thus, i need to replace the carriage return with 3 colons <:::> The particular column position in the source files is not fix but the name is fixed. That is, say for example,... (4 Replies)
Discussion started by: agathaeleanor
4 Replies

6. Shell Programming and Scripting

Why sed command deletes last line in a file if no carriage return?

Hi I am using sed command to make SCORE=somevalue to SCORE=blank in a file. Please see the attached lastline.txt file. After executing the below command on the file, it removes the last line. cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt Why does sed command remove the... (3 Replies)
Discussion started by: ashok.k
3 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

Delete carriage return in SED

Hi everybody! I'm working in one script with sed, I have file with the next content: <voms.db.type value="changeme"/> <voms.db.host value="changeme"/> <voms.admin.smtp.host value="changeme"/> <voms.mysql.admin.password value="changeme"/> <glite.installer.verbose value="true"/> ... (3 Replies)
Discussion started by: juedsivi
3 Replies

9. Shell Programming and Scripting

How to delete carriage return in SED

Could someone tell me how to do the below without opening the file? (eg in sed or awk) I have a file with the contenst below: $ more file1.txt 10 AAA; 200 BBB; 3 CCC; I want to delete the carriage return of one line above the line which has ";" at the end to get the... (3 Replies)
Discussion started by: stevefox
3 Replies

10. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies
Login or Register to Ask a Question