LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how can we write multiple lines to a text from command line without using cat, touch etc? (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-we-write-multiple-lines-to-a-text-from-command-line-without-using-cat-touch-etc-4175686886/)

stratos20vt 12-15-2020 10:37 AM

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?

TB0ne 12-15-2020 10:41 AM

Quote:

Originally Posted by stratos20vt (Post 6195853)
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'.

stratos20vt 12-15-2020 10:50 AM

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.

rtmistler 12-15-2020 11:18 AM

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.

stratos20vt 12-15-2020 11:21 AM

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?

JeremyBoden 12-15-2020 11:23 AM

According to the man page for echo, you have half an answer that could be tried:-
Code:

echo -e 'a\nb'

stratos20vt 12-15-2020 11:32 AM

that works, thanks!

JeremyBoden 12-15-2020 11:53 AM

Does
Code:

echo -e a\nb
work also?

stratos20vt 12-15-2020 11:56 AM

no, it puts them in one line

MadeInGermany 12-15-2020 01:04 PM

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

stratos20vt 12-15-2020 01:07 PM

we want to make it in one line command

TB0ne 12-15-2020 01:21 PM

Quote:

Originally Posted by stratos20vt (Post 6195922)
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.

stratos20vt 12-15-2020 04:22 PM

we join contents from many txt files in one using
more a.txt>c.txt ; more b.txt>>c.txt
is it correct?

JeremyBoden 12-15-2020 04:48 PM

Or even:-
Code:

rm c.txt; more *.txt >> c.txt
Won't always work...

TB0ne 12-15-2020 07:08 PM

Quote:

Originally Posted by stratos20vt (Post 6195979)
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


All times are GMT -5. The time now is 05:25 PM.