LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 05-26-2011, 12:57 AM   #1
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Rep: Reputation: 50
call awk from bash script behaves differently to awk from CLI = missing newlines


So, (yes this is tangentially related homework, but I think this question is legal, because it isn't directly related to the assignment.)

I have a file, I want to append the 5th fields (aka 5th column) of that file into another file:

from the CLI:
Code:
awk '{print $5}' fileone >> file2 
cat file2
output:
word
word
word

(yes, this is what I want.)

so, I put that line in a bash file

Code:
awk '{print $5}' fileone >> file2
echo `cat file2`
this will output
word word word
(this is not what I want)

why is this doing this?
Code:
me $ bash --version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
The code has to run on these machines, so is there a work around to inserting a newline after each word?
 
Old 05-26-2011, 01:35 AM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
What's the point of echo `cat file2`? Don't you think cat file2 would be good enough?

I think what may be happening is that the transition from STDOUT of cat to argument of echo causes the field separator to be munged.

/me tests

Yeah, that's definitely it. Try it on the command line and see what happens.

Code:
foo$ cat infile
ab
a b
a  b
a--b
a   b
a    b
aaabbb
foo$ echo `cat infile`
ab a b a b a--b a b a b aaabbb
foo$

Last edited by Telengard; 05-26-2011 at 01:37 AM.
 
1 members found this post helpful.
Old 05-26-2011, 02:36 AM   #3
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
EDIT. Didn't work.
EDIT EDIT: this is because, as I was trying to work out what was going wrong, I make awk printf rather than print.

heh. *me slaps head*

Thanks!

I think I got into my head that echo = C's printf. (tsk tsk)



TG

Last edited by titanium_geek; 05-26-2011 at 02:46 AM.
 
Old 05-26-2011, 03:30 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by titanium_geek View Post
I think I got into my head that echo = C's printf. (tsk tsk)
Nope. Bash has its own printf, if you need formatted output.

But that is not what I mean. Here's what you were doing:

Code:
echo `cat file2`
It is pointless to pass STDOUT from cat as an argument list to echo, which then sends it to STDOUT. That is what is changing your newlines into blanks.

All you really need to do what I think you want is:

Code:
cat file2
No need for echo, no need for command substitution. Either way the contents of file2 are sent to STDOUT.

My recommendation is to eliminate the bug you described by eliminating the unneeded echo and command substitution.
 
1 members found this post helpful.
Old 05-26-2011, 09:06 PM   #5
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
Thanks Telengard, I did remove the superfluous echos, wondered why it didn't work, then realised I needed to undo some of the poking I'd done to try and fix it before I found out that the echo was the problem.

TG
 
  


Reply

Tags
awk, bash, cat, command substitution, echo, script


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
bash script and awk phonebooth Programming 8 03-27-2010 12:51 AM
bash script using sed or awk? houms Linux - Newbie 10 06-21-2009 09:14 AM
Help with BASH script and AWK and SED NickJH Linux - Newbie 11 03-07-2009 04:08 PM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 05:19 AM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 08:03 PM


All times are GMT -5. The time now is 09:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration