How to replace a whitespace with line break?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a whitespace with line break?
# 1  
Old 03-23-2014
Linux How to replace a whitespace with line break?

Hello, I am a beginner in Unix and I'm learning how to program by myself.

I am trying to create a shell script that is able to take in either a file containing input or by standard input, and replacing every whitespace with a new line break.

so basically the input would be

This <SP> is <SP> a <SP> line. # where <SP> is a space

and the output would return

This
is
a
line.

so far i have understood the concept of using
Code:
while read line

, and as such has came up with the following code so far

Code:
#!/bin/bash

while read line; do
echo "$line"
done

but this only outputs correctly when there is a real line break instead of a whitespace in the file/stdin.

How should I modify the code to break every whitespace as a new line? Regards.
# 2  
Old 03-23-2014
Try:
Code:
tr ' ' '\n' < file

# 3  
Old 03-23-2014
Quote:
Originally Posted by bartus11
Try:
Code:
tr ' ' '\n' < file

That kinda worked. However, when I tried using a test case with a tab or with more than one space included, it stopped working.

Input:

This <SP> is <TAB> <SP> a <SP> <SP> <SP> line.

and it would only give the output:

This
is
a


line.

instead of the desired output:

This
is

a



line.

What is going on? Could I get an explanation?
# 4  
Old 03-23-2014
TAB is a different character from space. To replace spaces and TABs you can try:
Code:
tr ' \t' '\n' < file

# 5  
Old 03-23-2014
Technically, whitespace is more than just the space character. It includes a blank space, form feed, vertical and horizontal tabs and newline.

Code:
tr '[:space:]' '\n' < file

This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 03-24-2014
Quote:
Originally Posted by fpmurphy
Technically, whitespace is more than just the space character. It includes a blank space, form feed, vertical and horizontal tabs and newline.

Code:
tr '[:space:]' '\n' < file

If I understand correctly, the < file at the end is feeding the file into the script, right? But right now I'm trying to do it in a way so that its like:

./test.sh FILENAME #file input

and

./test.sh #this reads from standard input

So how do I approach this?
# 7  
Old 03-24-2014
Just do some check for the arguments
Code:
$ cat test.sh
#!/bin/sh
if [ $# -eq 0 ];then
    while read -r line
    do
        case "$line" in
            quit|Quit)
                exit 0;;
            *)
                echo "$line"|tr '[[:space:]]' '\n';;
        esac
    done
else
    for i in $@
    do
        if [ "$i" = '-' ];then
            trap '"break"' INT
            while read -r line
            do
                echo "$line"|tr '[[:space:]]' '\n'
            done
        else
            while read -r line
            do
                echo "$line"|tr '[[:space:]]' '\n'
            done<"$i"
        fi
    done
fi

cheer up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

How to break the line to the one above?

Hello everyone! I'm trying to make the below file1 look like file2, can anyone help? Basically I just hit backspace on every line that starts with a number. Thanks! file1: THIS#IS-IT1 4 THIS#IS-IT2 3 THIS#IS-IT3 2 THIS#IS-IT4 1 Result > file2: (4 Replies)
Discussion started by: demmel
4 Replies

3. UNIX for Dummies Questions & Answers

insert whitespace (tab) at start of each line

Hi all, I have this: begin data; dimensions nind=168 nloci=6; info BDT001.4 ( 1 , 1 ) ( 1 , 12 ) BDT003.4 ( 1 , 1 ) ( 12 , 12 ) BDT007.4 ( 1 , 1 ) ( 12 , 12 ) BDT009.4 ( 1 , 32 ) ( 12 , 22 ) etc, etc And need this: begin data; dimensions nind=168 nloci=6; info ... (2 Replies)
Discussion started by: MDeBiasse
2 Replies

4. UNIX for Dummies Questions & Answers

delete trailing whitespace from end of each line in column 1 only

Hi All. How can I convert this: ABC_1_1 ABC_1_2 ABC_1_3 into this: ABC_1 1 ABC_1 2 ABC_1 3 I tried this command but it is not working: awk '{sub(/+$/,"\t", $1)}{print}' Any suggestions on how to fix this? Thank you :wall: Please use code tags when posting data and... (3 Replies)
Discussion started by: danieladna
3 Replies

5. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

6. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

7. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

8. UNIX for Dummies Questions & Answers

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 sed s/" "/\\n" "/g nets > nets1 or sed s/" "/\n" "/g nets > nets1 nets (input file) 13MHZ_IN... (4 Replies)
Discussion started by: awk_sed_hello
4 Replies

9. Shell Programming and Scripting

Trim whitespace and add line break

All, I'm a newbie at shell scripting and regular expressions and I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but... (7 Replies)
Discussion started by: moose1
7 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question