ls | grep -v "\.dat$" | cat > newfile
but cat just gives me the list of files again. Any way to force cat to consider the lines as
The output from ls is just a list of filenames, so that's all that can appear at the far end of the pipeline here.

What you need to be doing is to use a 'for' loop, which allows you to step through the list of filenames one at a time, and do some action with them.

The command style is :-

for <variablename> in list of names
do
        action using <variablename>
done

Anything that the shell can use to generate a list of names is acceptable on the 'for' line, you could try ...

for i in 1 2 3 4 5 6 7 8 9 10
do
        echo $i
done

or, to expensively simulate ls
for i in *
do
        ls $i
done

If the list of names that you want to use comes as the output of a chain of other commands, you can use the subshell quoting of $(command) - this is equivalent but better than the old traditional backticks `command` that you might see around.

So, if ' ls | grep -v "\.dat$"' fives you the list of file you need ...

for i in $(ls | grep -v "\.dat$")
do
        ls -l $i
done

That will just list out the files with ls -l, I know that's not what you want just yet!

Now, you want to concatenate the contents of all the non-.dat files into one new file, yes? There's a couple of things to consider. If the output file you want to create is supposed to be in the same directory as all the others, either it must end with .dat, or it must not exist when you do your initial ls - otherwise your command will include the output file in it's own input, which would be confusing to say the least!

And, you have to be careful with the redirection operator ">" that you used earlier. That does a create/overwrite on the target file - if you ran it 10 times, only the last output would be found in the file. You need to use the ">>" operator - create/append.

So, your command starts to look like this -

rm outputfile
for i in $(ls|grep -v '\.dat$')
do
        cat $i >> outputfile
done

-jim



Reply via email to