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?
# 8  
Old 03-24-2014
Quote:
Originally Posted by Lucas_0418
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.
The while-read loops are unnecessary. They may also be buggy, since the goal is to convert whitespace and with the default value of IFS, the shell will discard leading and trailing whitespace before assigning a value to $line.

The echo statements are also unnecessary and are also problematic, because, if the line that's read looks like a command line option, it may alter the value of $line or even generate a syntax error. When it's necessary (here it's not), the best way to echo arbitrary text is to use printf '%s\n' "$line".

I don't see the point of the trap. - means stdin; stdin may not be a terminal. If the trap is meant as a way to give a human user a way to abort and continue to the next file in the for-loop list, it's redundant; ^D (control-d) will do the job by sending EOF (end-of-file).

The same goes for the quit|QUIT check in the case-statement. stdin may not be a terminal, even if there is no file argument.

Finally, forking a shell and exec'ing an instance of tr for every line read is very inefficient.

The simplest, most efficient solution is to redirect stdin using exec before invoking tr.

Regards,
Alister

Last edited by alister; 03-24-2014 at 04:04 AM..
# 9  
Old 03-24-2014
Hi Alister,
Thanks for your reply, let me realize that the while - read loop is totally unnecessary, I made the problem more complicated, since it is so simple. and you are very right that without set the IFS, the read line would trim the leading spaces.
I think this is a simple solution, thanks again, thanks.
Code:
#!/bin/sh
if [ $# -eq 0 ];then
	tr '[[:blank:]]' '\n'
else
	for i in $@
	do
		if [ "$i" = '-' ];then
			tr '[[:blank:]]' '\n'
		else
			tr '[[:blank:]]' '\n'<$i
		fi
	done
fi

This User Gave Thanks to Lucas_0418 For This Post:
# 10  
Old 03-24-2014
Quote:
Originally Posted by Lucas_0418
Hi Alister,
Thanks for your reply, let me realize that the while - read loop is totally unnecessary, I made the problem more complicated, since it is so simple. and you are very right that without set the IFS, the read line would trim the leading spaces.
I think this is a simple solution, thanks again, thanks.
Code:
#!/bin/sh
if [ $# -eq 0 ];then
	tr '[[:blank:]]' '\n'
else
	for i in $@
	do
		if [ "$i" = '-' ];then
			tr '[[:blank:]]' '\n'
		else
			tr '[[:blank:]]' '\n'<$i
		fi
	done
fi

This worked perfectly, thank you.

---------- Post updated 03-25-14 at 11:16 AM ---------- Previous update was 03-24-14 at 10:03 PM ----------

Quote:
Originally Posted by Lucas_0418
Hi Alister,
Thanks for your reply, let me realize that the while - read loop is totally unnecessary, I made the problem more complicated, since it is so simple. and you are very right that without set the IFS, the read line would trim the leading spaces.
I think this is a simple solution, thanks again, thanks.
Code:
#!/bin/sh
if [ $# -eq 0 ];then
	tr '[[:blank:]]' '\n'
else
	for i in $@
	do
		if [ "$i" = '-' ];then
			tr '[[:blank:]]' '\n'
		else
			tr '[[:blank:]]' '\n'<$i
		fi
	done
fi

Hello, sorry, but actually, what is this line of code for?

Code:
if [ "$i" = '-' ];then

because if I try

./function file.txt

it returns this line first
Code:
./function: line 8: [file.txt=-]: command not found

and then it is followed with the correct output. Why is that? How do I get rid of the first line?

Last edited by fozilla; 03-24-2014 at 09:53 PM..
# 11  
Old 03-24-2014
You didn't type the code correctly. Whitespace matters to the shell. Read your shell's documentation to learn how it parses and executes commands.

Regards,
Alister
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