Computing.Net > Forums > Unix > Awk output to file help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Awk output to file help

Reply to Message Icon

Name: losttrack (by losttrackofit)
Date: September 14, 2006 at 12:05:36 Pacific
OS: XP
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 15, 2006 at 11:15:13 Pacific
Reply:

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


0

Response Number 2
Name: losttrack (by losttrackofit)
Date: September 16, 2006 at 10:49:25 Pacific
Reply:

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 do

helloworld\nthis is a test

Everything prints fine, it looks great but outputting it to a txt file results in 1 line!! :(



0

Response Number 3
Name: lchi2000g
Date: September 19, 2006 at 04:50:31 Pacific
Reply:

===> 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


0

Response Number 4
Name: James Boothe
Date: September 19, 2006 at 07:45:04 Pacific
Reply:

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 bbb

ccc ccc ccc
ddd ddd ddd

eee eee eee
fff fff fff
!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Awk output to file help

Adding file size & output to file www.computing.net/answers/unix/adding-file-size-amp-output-to-file/7330.html

Unix Find command output to file www.computing.net/answers/unix/unix-find-command-output-to-file/6677.html

sending output to printer www.computing.net/answers/unix/sending-output-to-printer/4066.html