LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2012, 09:34 PM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
awk print $0 delete extra spaces problem


My awk version is:GNU Awk 3.1.5

I have a data file named data1, here is the data of file data1:
Code:
hereis[     ]mydata
Ijustw[     ]ant_to_see_it_intact
between [ and ] there are 5 spaces.
when I use the bash code below, it works fine, it just prints the lines as the data file:
Code:
cat data1|awk '{print $0}'
But when I use this code, it deletes the extra spaces:
Code:
cat data1 | while read line
do
    newline=$(echo  ${line}|awk '{print $0 }' )
    echo "${newline}"
done
the output is below, only one space between [ and ], not 5 spaces:
Code:
hereis[ ]mydata
Ijustw[ ]ant_to_see_it_intact
Please help understanding the behavior, thanks in advance.

Last edited by fantasy1215; 07-08-2012 at 11:42 PM.
 
Old 07-08-2012, 10:59 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... I am unable to repeat your results using your data and example
 
Old 07-08-2012, 11:41 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by fantasy1215 View Post
Code:
cat data1 | while read line
do
    newline=$(echo  ${line}|awk '{print $0 }' )
    echo "${newline}"
done
In that echo command, you need to quote the ${line} argument, else the shell will perform word splitting on the result and present each word as a separate argument to echo.
Code:
newline=$(echo  "${line}"|awk '{print $0 }' )
 
1 members found this post helpful.
Old 07-08-2012, 11:45 PM   #4
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
In that echo command, you need to quote the ${line} argument, else the shell will perform word splitting on the result and present each word as a separate argument to echo.
Code:
newline=$(echo  "${line}"|awk '{print $0 }' )
Thanks very much, you unblock my mind. I've learned a lesson. Thanks again.
 
Old 07-09-2012, 03:42 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Good pick up rk ... I had to type it into my vm and must have been on autopilot as I had placed the variable in quotes
 
  


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
[SOLVED] ubuntu : delete the extra spaces Xanios Linux - Newbie 13 07-28-2010 11:15 PM
Removing whiteline spaces using awk kdelover Programming 3 12-11-2009 08:07 AM
[SOLVED] Remove extra spaces in a line vikas027 Programming 11 10-11-2008 08:20 AM
Inserting spaces using SED/AWK ? aravindts Programming 4 09-29-2006 05:28 AM
Html: Extra Spaces on Top mikeshn Programming 1 07-05-2003 03:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 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
Open Source Consulting | Domain Registration