[Solved] Help with grep script

Hi,
I'm having trouble with a script to copy one line out of multiple files in a directory and copy to a file called test. I've tried the code below but it copies one line out of the first file multiple times not one line out of all the files. Would someone help? I'm very new to all this.

Many thanks,
Bob

#!/bin/bash
for i in *.CSV
do
    sed -n '10p' *.CSV >> test
done
#!/bin/bash
for i in *.CSV
do
    sed -n '10p' "${i}" >> test
done

Thank you but I still get the same result. One line out of the first file multiple times.

It should work, remove the file: test and rerun the script.

These are two of the files in the directory I want to copy line 10 from and copy to file "test":

574 McClain.0101.CSV 574 McClain.0103.CSV

when I run the script with the ${i} included, the file "test" is generated but is empty and Unix responds with:

sed: 574: No such file or directory
sed: McClain.0101.CSV: No such file or directory
sed: 574: No such file or directory
sed: McClain.0103.CSV: No such file or directory

Do you think this is a grep issue?

Many thanks for your help.
Bob

Did you wrap variable ${i} around double quotes like I posted above?

sed -n '10p' "${i}" >> test

It worked with the quotes!! but it copies each file twice. Any thoughts on why?
I can live with this if there are no fixes.

Thanks you so much.

It should not extract line number: 10 twice. Remove temporary file: test in the beginning so that you will not append data to existing content.

#!/bin/bash
rm -f test
for i in *.CSV
do
    sed -n '10p' "${i}" >> test
done
1 Like

Yes, I removed file "test" and ran the script. There are 12 files in the sandbox directory. When the file "test" is generated again from the script the results are 24 lines:

SUBJECT ID:b2425 c57 2-6mg low Fe
SUBJECT ID:b2428 c57 2-6mg low Fe
SUBJECT ID:b2389 c57 2g hi Fe
SUBJECT ID:b2390 c57 2g hi Fe
SUBJECT ID:b2391 c57 2g hi Fe
SUBJECT ID:b2392 c57 2g hi Fe
SUBJECT ID:b2431 c57 2-6 mg low Fe
SUBJECT ID:b2432 c57 2-6mg low Fe
SUBJECT ID:b2393 c57 2g/kg hi Fe
SUBJECT ID:b2394 c57 2g/kg hi Fe
SUBJECT ID:b2433 c57 2-6 mg low Fe
SUBJECT ID:b2434 c57 2-6 mg low Fe
SUBJECT ID:b2425 c57 2-6mg low Fe
SUBJECT ID:b2428 c57 2-6mg low Fe
SUBJECT ID:b2389 c57 2g hi Fe
SUBJECT ID:b2390 c57 2g hi Fe
SUBJECT ID:b2391 c57 2g hi Fe
SUBJECT ID:b2392 c57 2g hi Fe
SUBJECT ID:b2431 c57 2-6 mg low Fe
SUBJECT ID:b2432 c57 2-6mg low Fe
SUBJECT ID:b2393 c57 2g/kg hi Fe
SUBJECT ID:b2394 c57 2g/kg hi Fe
SUBJECT ID:b2433 c57 2-6 mg low Fe
SUBJECT ID:b2434 c57 2-6 mg low Fe

Thanks again,
Bob

---------- Post updated at 01:22 PM ---------- Previous update was at 01:19 PM ----------

I figured it out. It was a typo on my part. The problem is solved.
Thanks for all your help and patients with a "nubee"

Bob

1 Like