LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2020, 10:37 AM   #1
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Rep: Reputation: Disabled
how can we write multiple lines to a text from command line without using cat, touch etc?


I was asked for an exercise and I would ofcourse like to learn how can this be done?
 
Old 12-15-2020, 10:41 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,645

Rep: Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969
Quote:
Originally Posted by stratos20vt View Post
I was asked for an exercise and I would ofcourse like to learn how can this be done?
Since this is your homework, how do YOU think it can be done? Have you looked in your books? Asked your instructor?

And saying "etc" in this context rules out quite a bit, and you don't say what you're doing...for all we know, you have to write a program to do this, so doing it in C, Java, Perl, Python, etc., are all different and don't depend on cat, touch, 'etc'.
 
1 members found this post helpful.
Old 12-15-2020, 10:50 AM   #3
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Original Poster
Rep: Reputation: Disabled
we are working in linux bash.
we can't use cat, or touch, and I know its sth with echo
I tried echo ''a'' \ ''b'' \ ''c'' > a.txt
but it makes one line.
I want to put a, b , c in different lines of the text.
 
Old 12-15-2020, 11:18 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Suggest you try multiple lines in your script to echo to a file. Also look up the redirect which will create a new file versus the one which will append to an existing file. That will be important so that the first echo you can re-create the target file and subsequent echo lines you can concatenate to that file.
 
Old 12-15-2020, 11:21 AM   #5
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Original Poster
Rep: Reputation: Disabled
probably I didn't explain it well.
We want to write multiple lines at a text from command line without using cat, touch
how can this be done?
 
Old 12-15-2020, 11:23 AM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
According to the man page for echo, you have half an answer that could be tried:-
Code:
echo -e 'a\nb'
 
1 members found this post helpful.
Old 12-15-2020, 11:32 AM   #7
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Original Poster
Rep: Reputation: Disabled
that works, thanks!
 
Old 12-15-2020, 11:53 AM   #8
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Does
Code:
echo -e a\nb
work also?
 
Old 12-15-2020, 11:56 AM   #9
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Original Poster
Rep: Reputation: Disabled
no, it puts them in one line
 
Old 12-15-2020, 01:04 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,799

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Further, a standard shell takes a multi-line string.
Code:
echo "a
b
c"
Code:
echo 'a
b
c'
And there is printf that re-applies the format if there are extraneous arguments
Code:
printf "%s\n" a b c

Last edited by MadeInGermany; 12-15-2020 at 01:10 PM.
 
1 members found this post helpful.
Old 12-15-2020, 01:07 PM   #11
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Original Poster
Rep: Reputation: Disabled
we want to make it in one line command
 
Old 12-15-2020, 01:21 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,645

Rep: Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969
Quote:
Originally Posted by stratos20vt View Post
we want to make it in one line command
Great, so you should definitely do that.

You opened a second thread to ask the same question, while you still haven't shown any effort thus far, even though you were asked to. Have you read the man page on the echo command, as you were advised to? Have you asked your instructor or looked in your textbooks? This is *YOUR HOMEWORK*...meaning it was given to you to learn and think about, not for us to answer for you.

Suggest you look up redirects and pipes...any search engine will be able to give you a start.
 
1 members found this post helpful.
Old 12-15-2020, 04:22 PM   #13
stratos20vt
LQ Newbie
 
Registered: Dec 2020
Location: chania, crete, greece
Posts: 12

Original Poster
Rep: Reputation: Disabled
we join contents from many txt files in one using
more a.txt>c.txt ; more b.txt>>c.txt
is it correct?
 
Old 12-15-2020, 04:48 PM   #14
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Or even:-
Code:
rm c.txt; more *.txt >> c.txt
Won't always work...
 
2 members found this post helpful.
Old 12-15-2020, 07:08 PM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,645

Rep: Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969Reputation: 7969
Quote:
Originally Posted by stratos20vt View Post
we join contents from many txt files in one using
more a.txt>c.txt ; more b.txt>>c.txt
is it correct?
Yes, but:
  • *YOU* are the one taking the class; why can't you research what you've been told?
  • You could have just tried the above statement to see what it does
 
1 members found this post helpful.
  


Reply

Tags
shell



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] converting multiple lines of text to single line of text with comma separting them cogiz Programming 25 10-11-2020 10:11 AM
[SOLVED] Joining Three Lines of Text into One Line with Delimiters Between the Original Lines tronayne Slackware 25 03-18-2016 09:46 AM
[SOLVED] Replace text lines identified by leading text in line within multiple files juergen852 Linux - Newbie 9 09-21-2014 04:54 PM
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 06:09 AM
Using cat readline within cat readline while loop demxkn66 Programming 5 12-16-2007 05:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration