Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shell script problem..

Status
Not open for further replies.

nomanoma

Programmer
Apr 6, 2006
24
BA
Helloo..

I have no idea where to start with this problem...

In my directory there are ~1000 files..

each ones name starts with number..

for example..

12file1
13file124
142file5
12file34
13file9
1file12

ok I sorted them but how to merge thoese with same digits numberst in their name..

to have just

1file
12file (12file1 + 12file34)
13file (13file124 + 13file9)
142file


thanks..any help in appretiated..
 
This will kind of work - but it doesn't sort the output properly ... I'm sure others will have better ways to do it.

sed 's/file/&]/' < data | sed 's/].*//' | sort | uniq

Chris
 
Sorry - my file containing the input data is called data.
 
Sorry (again) I didn't read your question properly - I think this is what you want?

for file in `cat filenames`
do
SHORT_NAME=`echo $file | sed 's/file/&]/' | sed 's/].*//'`
cat $file >> $SHORT_NAME
done

(my list of filenames is now called filenames)

Chris
 
Thanks...but I am not quiet sure for the meaning of

SHORT_NAME=`echo $file | sed 's/file/&]/' | sed 's/].*//'`
cat $file >> $SHORT_NAME


Also I hope that content of 12file will be 2 files

(12file1 + 12file34) etc.
can U just help me out with meaning of whose 2 sed s...everything elese is clear to me..

Thanks..
 
Hi

Probably the easiest way to figure out the code is run it at the command line bit by bit. (You'll have to use a real filename, not $file

echo $file ; echoes the file name

echo $file | sed 's/file/&]/' ; puts a ']' after the string 'file' in the filename - so 12file123 becomes 12file]123

echo $file | sed 's/file/&]/' | sed 's/].*//'` ; removes the ']' and the rest of the filename - so 12file]123 becomes 12file

then all files which begin 12file are put into a file called 12file. (This assumes that none of your files has a ']' in the filename)

Was this what you wanted?. If you test it by creating a few small files with names like the examples you gave hopefully you'll see it does what you want. (I've tested this but only on a small set of files - I'm assuming it works in all cases, I think it should)

Let me know if it's OK, Chris
 
sed 's/file/&]/' | sed 's/].*//'
Why not simply this ?
sed 's!file.*!file!'


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hmm...what to say..It is working..I do appreciate your help...

And thanks for explanation..but this PHV's code is not clear to me..

:)

Thanks it works

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top