LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2011, 05:49 AM   #1
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Rep: Reputation: 0
Unhappy 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.
 
Old 11-21-2011, 06:39 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
1 members found this post helpful.
Old 11-21-2011, 09:48 AM   #3
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you very much dear Druuna
 
Old 11-21-2011, 10:03 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Probably easiest to just set record separator:
Code:
awk 'ORS="\n\n"' file
 
1 members found this post helpful.
Old 11-21-2011, 10:08 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by rahularjun86 View Post
Thank you very much dear Druuna
You're welcome
 
Old 11-21-2011, 11:41 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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/

Last edited by David the H.; 11-21-2011 at 11:45 AM.
 
1 members found this post helpful.
Old 11-21-2011, 11:57 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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
 
3 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 03:20 PM
anyone know how to double space docs in Abiword? cool_arrow Linux - Software 2 12-06-2008 02:56 AM
LXer: Double Spacing In Awk, Perl and Shell on Linux and Unix LXer Syndicated Linux News 0 07-08-2008 07:41 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 06:28 AM
Zipslack uses quiet the double space it should Corado Slackware 7 03-26-2004 03:17 AM


All times are GMT -5. The time now is 06:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration