LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Double space a file using awk (https://www.linuxquestions.org/questions/linux-newbie-8/double-space-a-file-using-awk-914668/)

rahularjun86 11-21-2011 04:49 AM

Double space a file using awk
 
Dear all,

I want to explore the unix awk command. The following one liner will generate the double space in a file:

awk '1;{print " "}' < file.txt

Can anybody please explain the use of "1;" in this command. Thank you very much in advance.

druuna 11-21-2011 05:39 AM

Hi,

In this case the 1 prints the currently read line (try: awk '1' < file.txt).

You can also use either of these to accomplish the same:
Code:

awk '{print $0} ;{print " "}' < file.txt
or
awk '{print } ;{print " "}' < file.txt

Hope this helps.

rahularjun86 11-21-2011 08:48 AM

Thank you very much dear Druuna :)

grail 11-21-2011 09:03 AM

Probably easiest to just set record separator:
Code:

awk 'ORS="\n\n"' file

druuna 11-21-2011 09:08 AM

Quote:

Originally Posted by rahularjun86 (Post 4529721)
Thank you very much dear Druuna :)

You're welcome :)

David the H. 11-21-2011 10:41 AM

To be more specific, the default action for any true statement is to print the line. Since any number other than zero evaluates as being true, using "1" as a command acts as a quick shortcut for typing "print".

By the way, the command as given above doesn't just double-space the file. the "blank" lines actually have one space in them. You probably want to tell it to print nothing instead.

Code:

awk '1;{print ""}' file.txt
And for yet another way to write it:

Code:

awk '{print $0"\n"}' file.txt
Edit: Speaking of which, you might be interested in this:

http://www.catonmat.net/blog/awk-one...ined-part-one/

grail 11-21-2011 10:57 AM

Well seeing we are all having another look ... have we considered what happens with current solutions should the input line be blank?
I believe a better solution is:
Code:

awk 'ORS=(NF)?"\n\n":""' file


All times are GMT -5. The time now is 08:23 AM.