Concatenate the string with the newline character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate the string with the newline character
# 1  
Old 09-16-2008
Concatenate the string with the newline character

Hi , i want to Concatenate a string and use the following code


Code:
str="i"
str="$str am \n"
str="$str a \n"
str="$str boy \n"
echo $str

I want to ouput this
Code:
i 
am 
a 
boy

However it outputs

Code:
i am \n a \n boy \n

How can I Concatenate the string with the newline character?
# 2  
Old 09-16-2008
Quote:
Originally Posted by youareapkman
Hi , i want to Concatenate a string and use the following code


Code:
str="i"
str="$str am \n"
str="$str a \n"
str="$str boy \n"
echo $str

I want to ouput this
Code:
i 
am 
a 
boy

However it outputs

Code:
i am \n a \n boy \n

How can I Concatenate the string with the newline character?
Would this do
Code:
str="i
am
a
boy"
echo "$str"

# 3  
Old 09-16-2008
Or even

Code:
str="i
"
str="$str am
"
str="$str a
"
str="$str boy
"
echo "$str"

If your echo understands \n to be a newline, the end result will work even if the string contains literal \n sequences. In some shells, you use something like echo -e to activate backslash interpretation.
# 4  
Old 09-16-2008
Hammer & Screwdriver Another approach

Code:
> echo "I am a boy" | tr " " "\n"
I
am
a
boy

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

3. Programming

Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator. In C I could do it with:printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n"); but could not figure out the equivalent in C++: cout << ((j+1)%20)?... (4 Replies)
Discussion started by: yifangt
4 Replies

4. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

5. Shell Programming and Scripting

replacing by newline character

I have a file (pema)with a single long record which i have to break up into multiple lines Input s1aaaaaaaaaaaaaaaaaaaaaaas1bbbbbbbbbbs1cccccccccc Output s1aaaaaaaaaaaaaaaaaaaaaaa s1bbbbbbbbbb s1cccccccccc m planning to do it by replacing s1 by \ns1 \n is the new line character i... (5 Replies)
Discussion started by: pema.yozer
5 Replies

6. Shell Programming and Scripting

Inserting newline in front of multi-character string

I'm working with a large file with multiple records, each record begins with ISA. The issue is, sometimes ISA is at the start of the line, sometimes it's in the middle of the line. So before I can csplit my main file into multiple records, I have to get each record header onto its own line. ... (7 Replies)
Discussion started by: verge
7 Replies

7. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

8. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies

9. UNIX for Advanced & Expert Users

newline character, space and tab after a string

no problem (6 Replies)
Discussion started by: angelina
6 Replies

10. UNIX for Dummies Questions & Answers

How can I replace newline character?

Hi, I am trying to write a script to prepare some text for use as web content. What is happening is that all the newlines in the textfile are ignored, so I want to be able to replace/add a few characters so that for a file containg: This is line 1. This is line two. This is line four.... (1 Reply)
Discussion started by: ghoti
1 Replies
Login or Register to Ask a Question