LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-27-2010, 02:31 PM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
concat files in numerical order using cat and sort


i am trying to concat lots of text files from the current working folder. the only problem is the text file names are enumerated. for example:

file file.0 file.1 file.2 file.3 ... file.99 etc...

how can i do this?

i have come up with the scheme below, but a) doesn't work and b) seems a little long winded:

ls file.* -l | sed -e 's/ +/ /g' | cut -d" " -f8 > output

the idea here is 'ls file.* -l' lists the files as following (from my previous example):

-rw------- 1 user user 1000 2010-01-27 17:42 file
-rw------- 1 user user 10000 2010-01-27 17:42 file.0
-rw------- 1 user user 10000 2010-01-27 17:42 file.1
-rw------- 1 user user 10000 2010-01-27 17:42 file.2
-rw------- 1 user user 10000 2010-01-27 17:42 file.3
-rw------- 1 user user 10000 2010-01-27 17:42 file.99

as you can see these are space delimited (number of spaces variable - see first line has two spaces infront of 1000). so i use 'sed' to attempt to convert any number of spaces to a single space:

sed -e 's/ +/ /g'

this doesn't work any help here please?

finally i 'cut' to get the eighth field:

cut -d" " -f8 > output

this would work if 'sed' had worked previously.

Last edited by DEF.; 01-27-2010 at 03:47 PM.
 
Old 01-27-2010, 02:36 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Maybe I'm missing something very obvious, but: all you want to do is cat all the files, into one new file?

Code:
cat ./* > newfile
Does this do the trick?
 
Old 01-27-2010, 02:46 PM   #3
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
that cat does work, but perhaps i forgot to mention i need the concat in numerical order.

the problem is the 'ls -l' command returns the files it the following order and i think that cat does the same:

file
file.0
file.1
file.10
file.11
file.12
file.13
file.14
file.15
file.16
file.17
file.18
file.19
file.2
file.20
etc...

i want this order:

file
file.0
file.1
file.2
file.10
file.11
file.12
file.13
file.14
file.15
file.16
file.17
file.18
file.19
file.20
etc...

Last edited by DEF.; 01-27-2010 at 03:43 PM.
 
Old 01-27-2010, 02:52 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
cat $(find ./ -name "file*" | sort -V) > newfile
Aha! I understand. Try the above instead.

Sasha

Last edited by GrapefruiTgirl; 01-27-2010 at 02:53 PM. Reason: changed >> to >
 
Old 01-27-2010, 03:22 PM   #5
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
fantastic, almost perfect!

i like what you've done there, but i am puzzled to why i get two of every file concated in the output? e.g.

file
file
file.0
file.0
file.1
file.1
etc...

although the ordering is spot-on. Thanks!

Last edited by DEF.; 01-27-2010 at 03:44 PM.
 
Old 01-27-2010, 03:23 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I'm puzzled too.. It works great for me, producing only one of each file. Umm..

Show us the exact command you are executing.
 
Old 01-27-2010, 03:25 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
sasha@reactor: echo "this is file1" > file.1
sasha@reactor: echo "this is file2" > file.2
sasha@reactor: echo "this is file3" > file.3
sasha@reactor: cat $(find ./ -name "file.*" | sort -V) > newfile
sasha@reactor: cat newfile
this is file1
this is file2
this is file3
sasha@reactor:
 
Old 01-27-2010, 03:28 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
sasha@reactor: find ./ -name "file.*" | sort -V | xargs cat > newfile2
sasha@reactor: cat newfile2
this is file1
this is file2
this is file3
sasha@reactor:
For the sake of learning and experimenting, here's a slightly different method.

Last edited by GrapefruiTgirl; 01-27-2010 at 03:31 PM.
 
Old 01-27-2010, 03:29 PM   #9
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
wait there a minute, if your puzzled then i must be doing something wrong... (so he (me that is) goes and has a good look at his own stupidity! )

lol

your command is correct.

my command is correct.

this is funny; I decided to open the files in the folder in gedit to add markers so i could prove the ordering. well, doing that meant gedit saved a backup of each called file~, thus two lots of the same files in the folder

i am very silly

well done, and for me a smacked hand
 
Old 01-27-2010, 03:30 PM   #10
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
LOL -- I had been wondering if you had two of the same-named file in the directory, but of course, discounted that because it would be impossible. I neglected to think of BACKUP files

Cheers!
Sasha
 
1 members found this post helpful.
Old 01-27-2010, 03:30 PM   #11
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
perhaps Upton Sinclair should have shortened his phrase too "It's difficult to get a man to understand"

anyway, so that's done.

but, what was wrong with my 'sed' command?
 
Old 01-27-2010, 03:32 PM   #12
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
What's wrong with your sed command? Beats me! LOL, what do you need it for?

Seriously though, what were you trying to do with sed, and what was going wrong?
 
Old 01-27-2010, 03:33 PM   #13
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
ls -l | awk '{ print $8 }' | sort -V

Perhaps this will do what you were trying to do with sed and cut and whatever else was mixed up in there?

 
Old 01-27-2010, 03:35 PM   #14
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
that's great too!

cheers
 
Old 01-27-2010, 03:36 PM   #15
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Cool -- well, maybe you can edit this thread title a tiny bit, to make it more reflective of what's going on in here and then, if you are happy with the results, you can mark it [solved] using "Thread Tools" atop the page.

Best regards!
Sasha
 
  


Reply


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
sed help - PLEASE! itzfritz Linux - Software 2 09-02-2008 03:50 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

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

All times are GMT -5. The time now is 08:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration