Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm new to all awk and cygwin so stuck on something probably very simple.
Awk will print multiple lines, but when I output to a file everything is stuck on just the 1 line. How do I make it output to more than 1 line?
How would I do something simple like this...echo "hello world" (Somehow put a new line here) "this is a test" > test.txt
To output
hello world
this is a test

First, using printf within your awk script allows controlling the output format.
But your question doesn't seem to have anything to do with awk. This echo works:
echo "hello world""\n""this is a test" > test.txt
# this is probably cleaner:
echo "hello world\nthis is a test" > test.txt

It's not working.
echo "hello world""\n""this is a test" > test.txt will produce a txt file containing;
hello world\nthis is a test
and echo "hello world\nthis is a test" > test.txt will dohelloworld\nthis is a test
Everything prints fine, it looks great but outputting it to a txt file results in 1 line!! :(

===> script: 1.sh
$ cat 1.sh
#!/bin/ksh
echo "hello world""\n""this is a test" > test.txt===> run 1.sh
$ ./1.sh
===> test.txt file:
hello world
this is a test---> if you run it directly on the command line:
$ echo "hello world""\n""this is a test" > test.txt
---> test.txt file:
hello world\nthis is a test
Luke Chi

Your platform is probably like linux, where echo requires the -e option to enable interpretation of backslashed characters.
echo -e "hello world\nthis is a test" > test.txt
Another way to easily get multi-line text to disk is using cat, which would be far superior if you have a lot of lines, and particularly for special alignment such as columnarized data because all of the lines appear one below the other. And of course, > will create or replace existing test.txt while >> would create or append to existing test.txt.
cat > test.txt << !
aaa aaa aaa
bbb bbb bbbccc ccc ccc
ddd ddd dddeee eee eee
fff fff fff
!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |