LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial 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 03-21-2013, 10:29 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Rep: Reputation: 287Reputation: 287Reputation: 287
CAT giving unexpected results


Have: a character matrix such as ...
Code:
abcde
fghij
klmno
pqrst
Have: a bash variable, a character string such as ...
Code:
NewRow='NEWROW'
Want: to attach the string as a new row at the top of the matrix.
This code ...
Code:
echo; echo; echo "Add a row to the top of a matrix."
echo "Input ..."; cat $InFile
echo; echo "Result ..."
NewRow='NEWROW'
 cat <<< $NewRow \
|cat - $InFile
... produces this result ...
Code:
Add a row to the top of a matrix.
Input ...
abcde
fghij
klmno
pqrst

Result ...
NEWROW
abcde
fghij
klmno
pqrst
... as expected. All good.


Now, a variation on the theme.....

Have: the same matrix and the same character string.

This code (which appears to be the inverse of that shown above) ...
Code:
echo "Add a row to the bottom of a matrix."
echo "Input ..."; cat $InFile
echo; echo "Result ..."
NewRow='NEWROW'
 cat $InFile  \
|cat - <<< $NewRow
... produces this unexpected result ...
Code:
Add a row to the bottom of a matrix.
Input ...
abcde
fghij
klmno
pqrst

Result ...
NEWROW
Please explain.

Note 1) There are other ways to "tack on" a row to the top or bottom of a matrix, such as sed.
I'm not looking for alternate solutions, but only an explanation for why cat failed.

Note 2) Please don't chide me for a "useless" cat. That's not the point of the question.

Daniel B. Martin
 
Old 03-21-2013, 11:00 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
Its late so have not thought this all the way through, however, the issue is not cat but rather your use of re-directions and which order they are being processed in.

I'll have another look in the morning in case no one else elaborates or you don't find the solution using my information
 
Old 03-21-2013, 11:54 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
Quote:
Originally Posted by danielbmartin View Post
Code:
 cat $InFile  \
|cat - <<< $NewRow
There is only a single stdin stream, you are trying to connect it to 2 things. The same problem as:
Code:
cat < file1 < file2
 
1 members found this post helpful.
Old 03-21-2013, 12:10 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
And, to get the result you want, with minimal modification to your logic, use
Code:
echo; echo; echo "Add a row to the top of a matrix."
echo "Input ..."; cat $InFile
echo; echo "Result ..."
NewRow='NEWROW'
 tac <<< $NewRow | tac - $InFile | tac
 
Old 03-21-2013, 12:47 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 374

Rep: Reputation: 150Reputation: 150
Uh… What's wrong with:
Code:
printf '%s\n' "$NewRow" | cat - "$InFile"
Also, remember to quote your variables. If you aren't sure if you have to, quote.
 
1 members found this post helpful.
Old 03-21-2013, 02:45 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287
Quote:
Originally Posted by mina86 View Post
Uh… What's wrong with:
Code:
printf '%s\n' "$NewRow" | cat - "$InFile"
Your code plants the NewRow at the top of the matrix, not the bottom.

Repeating from the original post:
Quote:
I'm not looking for alternate solutions, but only an explanation for why cat failed.
 
Old 03-21-2013, 02:52 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287
Quote:
Originally Posted by PTrenholme View Post
And, to get the result you want, with minimal modification to your logic, use
Code:
echo; echo; echo "Add a row to the top of a matrix."
echo "Input ..."; cat $InFile
echo; echo "Result ..."
NewRow='NEWROW'
 tac <<< $NewRow | tac - $InFile | tac
Your code works but I hesitate to use tac three times when one sed does the same job.

Repeating from the original post:
Quote:
I'm not looking for alternate solutions, but only an explanation for why cat failed.
Daniel B. Martin
 
Old 03-21-2013, 04:03 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,490

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Indeed, as ntubski explained, the problem is the double redirection of the standard input from the pipe and from the here string. The reason is that the last one supersedes the preceding one. In this case the cat command doesn't receive input from the pipe anymore, but only from the here string.
 
Old 03-21-2013, 06:53 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
Quote:
Originally Posted by danielbmartin View Post
Your code plants the NewRow at the top of the matrix, not the bottom.
Oh, I missed that you were trying to do that too.

Quote:
Note 2) Please don't chide me for a "useless" cat. That's not the point of the question.
Without the distraction of the redundant cat it's easier to see what the correct inverse is:
Code:
# stdin ("NEWROW") then $InFile
cat - "$InFile" <<< "$NewRow"
# $InFile then stdin
cat "$InFile" - <<< "$NewRow"
 
2 members found this post helpful.
Old 03-21-2013, 08:56 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287
Quote:
Originally Posted by ntubski View Post
Without the distraction of the redundant cat it's easier to see what the correct inverse is:
Code:
# stdin ("NEWROW") then $InFile
cat - "$InFile" <<< "$NewRow"
# $InFile then stdin
cat "$InFile" - <<< "$NewRow"
Excellent! Thank you for these crisp and instructive gems!

Thank you also to everyone who contributed to this thread.

SOLVED

Daniel B. Martin
 
Old 03-21-2013, 09:33 PM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Original Poster
Rep: Reputation: 287Reputation: 287Reputation: 287
Quote:
Originally Posted by mina86 View Post
... remember to quote your variables. If you aren't sure if you have to, quote.
Thank you, this is good advice.
Misplaced or missing quotes have caused many a stumble.

Daniel B. Martin
 
  


Reply

Tags
cat


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
[SOLVED] unexpected results with --include-from= for rsync help please. lleb Linux - Newbie 11 02-25-2013 09:48 PM
[SOLVED] sed loop gives unexpected results jgombos Programming 13 09-10-2011 12:33 AM
find -mtime gives unexpected results amchargue Linux - Newbie 3 08-12-2010 04:19 PM
[SOLVED] Sort generates unexpected results danielbmartin Linux - Newbie 5 06-08-2010 02:39 AM
find command displays unexpected results helptonewbie Linux - Newbie 5 08-12-2008 02:25 AM


All times are GMT -5. The time now is 07:50 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