LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 04: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, 05:39 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 08: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, 09:03 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by rahularjun86 View Post
Thank you very much dear Druuna
You're welcome
 
Old 11-21-2011, 10:41 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 10:45 AM.
 
1 members found this post helpful.
Old 11-21-2011, 10:57 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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



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 02:20 PM
anyone know how to double space docs in Abiword? cool_arrow Linux - Software 2 12-06-2008 01:56 AM
LXer: Double Spacing In Awk, Perl and Shell on Linux and Unix LXer Syndicated Linux News 0 07-08-2008 06:41 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
Zipslack uses quiet the double space it should Corado Slackware 7 03-26-2004 02:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:47 PM.

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
Open Source Consulting | Domain Registration