Cat command not working as expected

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Cat command not working as expected
# 1  
Old 02-14-2009
Cat command not working as expected

I've been trying to figure this out since last night, and I'm just stumped. The last time I did any shell scripting was 8 years ago on a Unix box, and it was never my strong suit. I'm on a Mac running Leopard now. Here's my dilemma - hopefully someone can point me in the right direction.

I'm trying to process product datafeeds - plain text, thousands of lines, pipe delimited. My shell script prepares a directory, then downloads via curl a zip file. It unzips the file. It greps out lines containing a keyword and redirects into a new file. I use a perl command to find/replace my ID number in the file. Everything I've described works perfectly - but the next step is the problem.

I need to add the field names to the top of the file so I can put the file into WebMerge and process it. The field names are on a single line in a separate text file called FirstLine.txt. I use this:

cat FirstLine.txt kids.txt > readyToProcess.txt

to combine the files. What I expect is that if FirstLine.txt contains foo and kids.txt contains bar, then readyToProcess.txt should contain foo(new line)bar. What I'm getting instead is that the file contains foobar.

Is there a situation where the cat command wouldn't put the contents of the first file on a new line?

Thanks for any help you can offer - I'm rather stuck until I figure this out!

~Daniel
# 2  
Old 02-14-2009
Make sure that there is a proper line terminator in FirstLine.txt. cat doesn't put that in for you, it just does a 1:1 output.
# 3  
Old 02-14-2009
Ah-ha. Okay, I think I'm onto something... I spent some time researching the line terminator thing (have been since last night actually) but everything I came up with kept telling me that my line terminator was Unix-style. Just now though, I punched file FirstLine.txt into Terminal and it came back with FirstLine.txt: ASCII text, with no line terminators.

Great. So... how do I insert a line terminator? I found many, many resources that talk about the tr command and changing \r to \n but nothing in the way of adding a line terminator where none exists.
# 4  
Old 02-14-2009
Open the file in your preferred text editor, hit <End>, press <Enter>, save, close.
Or, on the command line, enter printf "<lt>" >> FirstLine.txt, where <lt> is one of the following:
  • DOS/Windows: \r\n
  • UNIX (including Linux, BSD, not OSX): \n
  • MacOS: \r
# 5  
Old 02-14-2009
I was hoping you wouldn't say that Smilie See, I figured there must be some trick or something, because when I simply add a new line like that (by opening in TextWrangler and hitting Enter at the end of the line), my output gets... weird.

This is a short example of what it was doing when I first posted.

FirstLine.txt:
Quote:
ProductID|Name|Price|Color
kids.txt:
Quote:
24234232|Thing|2.50|Red
24324242|Item|3.50|Blue
23423422|This|4.50|Black
cat FirstLine.txt kids.txt > readyToProcess.txt
Quote:
ProductID|Name|Price|Color24234232|Thing|2.50|Red
24324242|Item|3.50|Blue
23423422|This|4.50|Black
After adding the new line to FirstLine.txt, I try cat'ing again, but this time, the resulting readyToProcess.txt file looks like this:
Quote:
ProductID|Name|Price|Color
24234232|Thing|2.50|Red

24324242|Item|3.50|Blue

23423422|This|4.50|Black
So, it added the first line great - but why on earth would it throw an extra line break between all the rest of the lines in the file?
# 6  
Old 02-14-2009
Also - I ran printf "\n" >> FirstLine.txt and the result was the same as when I open the file in TextWrangler and add the new line manually. Oddly, when I cleared that new line and ran printf "\r" >> FirstLine.txt the output is almost correct. I get this:
Quote:
ProductID|Name|Price|Color
24234232|Thing|2.50|Red
[]24324242|Item|3.50|Blue
[]23423422|This|4.50|Black
There's an extra space at the beginning of every line starting with line 3 which I've noted with [] because vBulletin doesn't allow a space at the beginning of a line in a quote tag Smilie
# 7  
Old 02-14-2009
That's strange. You could try to edit it with VIm, adding an empty line and deleting it again, as it usually adds the appropriate EOL when saving a file. Also, if you enter ':set list', you'll see any special characters (eg. $ for EOL, ^I for tab, ...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

2. Shell Programming and Scripting

Cp command not working as expected in HPUX

Hi, I'm having trouble with a simple copy command in a script on HPUX. I am trying to copy a file and append date & time. The echo command prints out what I am expecting.. echo "Backing up $file to $file.$DATE.$FIXNUM" | tee -a $LOGFILE + echo 'Backing up... (4 Replies)
Discussion started by: Glennyp
4 Replies

3. Shell Programming and Scripting

Cat command not working to display Mac file in Ubuntu

Hi, Recently I got a .txt file from Mac user. when I try to open it in my Ubuntu machine using cat command it is not displaying any content of file however I can see the content using vi. Anyone know How to see its content using cat as I have to process it in my shell script. Thanks in... (4 Replies)
Discussion started by: diehard
4 Replies

4. UNIX for Dummies Questions & Answers

Nohup not working as expected

Hi. I am trying to start a script on my router that will execute even if i log off. To execute the script I write: nohup ./dslconnection > dslstat.out 2>&1 & It starts the job: 21968 admin 1604 S /bin/ash ./dslconnection The problem is that when I log back in the job has been... (6 Replies)
Discussion started by: sebcou
6 Replies

5. Shell Programming and Scripting

Read command not working as expected

I was trying to write a simple script which will read a text file and count the number of vowels in the file. My code is given below - #!/bin/bash file=$1 v=0 if then echo "$0 filename" exit 1 fi if then echo "$file not a file" exit 2 fi while read -n... (14 Replies)
Discussion started by: linux_learner
14 Replies

6. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

7. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

8. UNIX for Dummies Questions & Answers

Find command not working as expected

I have a script with a find command using xargs to copy the files found to another directory. The find command is finding the appropriate file, but it's not copying. I've checked permissions, and those are all O.K., so I'm not sure what I'm missing. Any help is greatly appreciated. This is... (2 Replies)
Discussion started by: mpflug
2 Replies

9. Shell Programming and Scripting

ls not working as expected within ksh

Hi, I use the command ls a\b\c\*.txt from the command line on HP UNIX and it works fine - It lists all files matching *.txt in the a\b\c directory When embeded in a ksh script `ls a\b\c\*.txt` it does not work - I get *.txt not found (even though there are files) I tried... (10 Replies)
Discussion started by: GNMIKE
10 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question