How can I split a long text string with "\n" with simple shell/bash commands, and then write it to a file?
-
The objective is using only bash/ash syntax and very simple commands ( no sed/awk ). The scenario is the following, we have a long text in a shell variable with "\n" delimiters, like the one generated by the following loop: #!/bin/sh messages="" for index in $(seq 1 "${1}"); do messages="${messages}\n""trial ${index}" done Now if i redirect this variable to a text file with echo, i get an error ( The maximum length of arguments for a new process ), this can be replicated by: /bin/echo -e "${lmessages}" >> /tmp/test.log when ${1} is greater than 40'000 . So, how can i write this large text variable to a file, splitting in in the process, without hitting the limit of "max arguments"? This, if possible, avoiding awk/sed (why? Because i would like to solve it without using other common turing complete interpreters) and arrays. Update1 To be clearer in what i mean, example with a string LITERAL that contains simple "\n" identifier, while my problem involves a variable! (because if i try to echo the variable, i get an error due to the text too large to be passed as an argument) root@meh# echo "bla\nbla" | while read -r line; do echo $line; done -- output bla\nbla I want root@meh# echo "bla\nbla" | <some code> -- output bla bla endUpdate sister question: http://unix.stackexchange.com/questions/148357/write-variable-containing-large-text-with-n-to-a-file-with-common-shell-interpr
-
Answer:
I'm not 100% sure I understand what you ask but you could try something like while read -r line; do echo $line foo done $ cat long.txt a b sdkfjalsjfk skfjalksfjd d $ ./test.sh < long.txt a foo b foo sdkfjalsjfk skfjalksfjd foo d foo Or something similar. This will read the input (which can be a variable just as well) line by line.
Marko Poutiainen at Quora Visit the source
Other answers
Are you looking for echo -e? sh-3.2$ echo -e "bla\nbla" | while read -r line; do echo $line; done bla bla
Reshmi VS
Related Q & A:
- How can I send a .mht file as blat attachment?Best solution by Stack Overflow
- How can I do a resource in PDF file?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- How can I get a text alert when I get an email?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.