Having issues with cat

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having issues with cat
# 1  
Old 08-03-2011
Having issues with cat

I am using cat to collect data from hundreds of files with the following code:
cat *.dis > intree2

All my files have the extension .dis and are numbered:
I came to realize that my code is not adding the files in consecutive order. I need to make sure the content from file Block1.dis is at the very beggining of the output file followed by File Block2.dis so on and so forth. Is there any way I can force cat to do that precisely?
# 2  
Old 08-03-2011
cat `ls *.dis` > intree2
# 3  
Old 08-03-2011
"*" sorts files alphabetically. It is a feature of the original Bourne shell. You can check it with
echo *.dis

If you need some other order you can sort files before you cat them. How to do this depends on whether you have spaces (and other non-alphanum chars, not count "_" "-" ".") in names of your files or not. And, of course, on what order do you need.

PS When I said "How to do this" I basically meant how you can concatenate your files after sorting.
---------- Post updated at 10:23 AM ---------- Previous update was at 10:20 AM ----------

In general this is a bad advise:
cat `ls *.dis` > intree2

See: BashPitfalls - Greg's Wiki

Last edited by yazu; 08-03-2011 at 02:02 AM..
# 4  
Old 08-03-2011
Sorting hint: sort -k1.6n

# 5  
Old 08-03-2011

The problem is that file block1.dis is not being recognized as the firts file:
ls -sort *.dis

4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:42 block2.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:42 block3.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:43 block4.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:43 block5.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:43 block6.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:44 block7.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:44 block8.dis
4 -rw-r--r-- 1 jiv9 1 2011-08-03 09:44 block9.dis
4 -rw-r--r-- 1 jiv9 2 2011-08-03 09:58 block10.dis
4 -rw-r--r-- 1 jiv9 3 2011-08-03 09:59 block1.dis
4 -rw-r--r-- 1 jiv9 7 2011-08-03 09:59 block11.dis
What should I do to force the code to recognize the file block1.dis to be the first one?
# 6  
Old 08-03-2011
GNU ls has "-v" options which sorts exactly as you want. Or you can use a filter like this (assuming names of your files are blockN.dis):
ls *.dis | sed 's/[0-9]/ &/' | sort -n -k2 | sed 's/ //'

cat `ls *.dis | sed 's/[0-9]/ &/' | sort -n -k2 | sed 's/ //'` >intree2

Or if your sort command can do it:
ls *.dis | sort -n -k1.6

Last edited by yazu; 08-03-2011 at 12:50 PM..
This User Gave Thanks to yazu For This Post:
# 7  
Old 08-03-2011
I guess I got it!

cat `ls *.dis | sort -n -k1.6 | sed -e 's/;/;\n/g'`

Thanks !!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cat with << >>

Hi, When I was analyzing the code I got below line. cat - << 'EOF' >> ${FILE PATH} I surfed net to understand but I couldn't get what is about. Please help me out. (2 Replies)
Discussion started by: stew
2 Replies

2. UNIX for Dummies Questions & Answers

Cat and variables

I've been having trouble with cat and variables. If I do the following: var1=filex cat $var1 I get the contents of the file named filex If I do var2="X-0101\ 2-10-2013.txt" cat "$var2" I get cat : cannot open X-0101\ 2-10-2013.txt. I have tried to do cat $var2 without quotes as... (8 Replies)
Discussion started by: newbie2010
8 Replies

3. UNIX for Dummies Questions & Answers

Cat Help needed

Hi, I am unable to use a basic script in cygwin. Please help me out. Script used : #!/bin/bash echo "Lucent MSC1" echo "===========" echo "Orig_MSCID Summary Top 10" cat k1.txt | grep -i orig_mscid | sort | uniq -c echo "Hourly Summary" cat k1.txt | grep "#" | cut -c 10-11 | uniq... (7 Replies)
Discussion started by: vanand420
7 Replies

4. UNIX for Dummies Questions & Answers

for cat help

Hi there, I have the following problem. I have a csv file which looks like 'AGI,ABJ,Y,Y,Y,None,EQUATION,ANY,ANY,None,' 'AGI,ABJ,Y,Y,Y,None,EQUATION HEAVY,ANY,ANY,None,' 'AGI,ABJ,Y,Y,Y,None,VARIATION,ANY,ANY,None,' but I do this for ab in $(cat test.csv); do echo $ab; done in the... (4 Replies)
Discussion started by: sickboy
4 Replies

5. 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

6. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

7. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

8. UNIX for Dummies Questions & Answers

newbie need help on cat

1. what do the following commands do (give logic and concise description) a. cat 1> file-A 2> file-B 0< file-C b. cat 2> file-B 0< file-C 1> file-A c. cat 1> file-A 0< file-C 2>&1 d. cat 0<file-C | sort 1> file-A 2> /dev/null e. cat 2> file-B 0< file-C | sort 1> file-A 2> /dev/null (1 Reply)
Discussion started by: chitti76
1 Replies

9. UNIX for Dummies Questions & Answers

Cat and vi query

Hi experts, I have a log file, i tried to see it with cat abc.log|pg. Since it has lacs of rows, I wanted to know how can i navigate to a particular date row. Also with vi how can I navigate. Thanks in advance (1 Reply)
Discussion started by: shaan_dmp
1 Replies
Login or Register to Ask a Question