How to add new line after every data inserted to file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add new line after every data inserted to file?
# 1  
Old 04-08-2015
Ubuntu How to add new line after every data inserted to file?

Hi all,
I need help for solve my problem.

my problem is like this..
i want to add many word to file. but after I add 1 word, the second word should be in the under of the first word.

i have tried but the result is like this
Code:
word1word2word3

i want the result to be like this
Code:
word1
word2
word3


Last edited by Don Cragun; 04-08-2015 at 01:38 AM.. Reason: Add CODE tags.
# 2  
Old 04-08-2015
What have you tried?

Where are the "words" you are adding coming from?

What operating system are you using?

What shell are you using?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-08-2015
Code:
echo "word1\n" >> result.txt
echo "word2\n" >> result.txt
echo "word3\n" >> result.txt

i have execute that command.
the result in the result.txt file is like this
Code:
word1word2word3

.

I need to make the result like this
Code:
word1
word2
word3

.

thank you.

Last edited by Don Cragun; 04-08-2015 at 02:07 AM.. Reason: Add CODE tags, again.
# 4  
Old 04-08-2015
I repeat: What operating system are you using?

What shell are you using?

On many systems with some shells, the code you used would produced double spaced output, rather than putting output on a single line. I wasn't aware that any shell on any operating system would produce that output from those commands.

And, please use CODE tags instead of expecting UNIX and Linux Forum moderators to clean up your posts for you!

Try, just using:
Code:
echo "word1" >> result.txt
echo "word2" >> result.txt
echo "word3" >> result.txt

# 5  
Old 04-08-2015
I'm so sorry for make many confusing here. That is because I'm really new in this forum.

I'm using windows 7 with cygwin instaled Sir..
and I run that code in .sh file Sir

Last edited by weslyarfan; 04-08-2015 at 03:42 AM.. Reason: wrong grammar
# 6  
Old 04-08-2015
If you're trying to add lines to a DOS file instead of a UNIX format file, try:
Code:
printf '%s\r\n' "word1" >> result.txt
printf '%s\r\n' "word2" >> result.txt
printf '%s\r\n' "word3" >> result.txt

or more simply:
Code:
printf '%s\r\n' "word1" "word2" "word3" >> result.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

2. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

3. Shell Programming and Scripting

Help in adding a data after a particular line of data in a file.

Hi.. I'm into a bump after trying to solve this prob.. i've a file with contents like below. <blankline> 'pgmId' : 'UNIX', 'pgmData' : 'textfile', 'author' : 'admin', ....... Now i'm trying to insert a new data after pgmId. so the final output will be... (7 Replies)
Discussion started by: arjun_arippa
7 Replies

4. Shell Programming and Scripting

script to detect a file from inserted usb and puts into a Variable

There is a same named log file that I have on my 2 different android phones. When I plug it into my computer, it appears in the media folder, For example the first android phone: /media/F6BA-0AF5/folder/A.log I want to put that into a variable to be manipulated.... (3 Replies)
Discussion started by: tobenguyen
3 Replies

5. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

6. UNIX for Dummies Questions & Answers

Binary line being inserted while truncating a file

Hi All, I have a ln between two files (say ln a b), when i truncate file " b ", this truncates file " a " also, a binary line is being added as the first line of both the files. This is causing problems with other scripts which grep on the above files. (though i can work my way around... (4 Replies)
Discussion started by: akshay61286
4 Replies

7. Shell Programming and Scripting

Removing inserted newlines from a fileld of fixed width file.

Hi champs! I have a fixed width file in which the records appear like this 11111 <fixed spaces such as 6> description for 11111 <fixed spaces such as 6> some more field to the record of 11111 22222 <fixed spaces such as 6> description for 22222 <fixed spaces such as 6> some more field to the... (8 Replies)
Discussion started by: enigma_1
8 Replies

8. UNIX for Dummies Questions & Answers

Add line with data to existing file

i have a file called motors with 3 columns separated with tabs eg: car make reg i want to create a executable file that will add a line with data eg: car make reg benz s600 t35778 can you please show me how to do this? (7 Replies)
Discussion started by: fletcher
7 Replies

9. UNIX for Dummies Questions & Answers

add data from command line to end of file

how can I add data from command line to end of file? (3 Replies)
Discussion started by: bryan
3 Replies

10. Shell Programming and Scripting

Need to add a line of data to already existing file in Unix..

Hello.. I have a text file with 100 lines of data. I need to add 1 line of data to that already existing file at the first line( beginning of the file) , so that the already existing 100 lines will start from 2 nd line.Now the file will have 101 lines of data. Help me on how to add the line... (4 Replies)
Discussion started by: charan81
4 Replies
Login or Register to Ask a Question