0

I'm attempting to select certain data from numerous csv files using linux, making a new file containing just the data I require for processing.

I have started by using grep and awk commands to select the rows and columns I require from a file.

I then wish to add a column which gives the filename as an entry for all rows.

Finally I want to repeat this for a number of files in a folder and append the results into one file. Is there a way of doing this?

Thank you in advance for any help.

4
  • I'd just do it in a quick Perl script, personally. Loop in a file, spiit each line, and concat each column you want, plus the filename of the current file at the end of the line. Then loop over each file in the specified directory. Commented Feb 18, 2015 at 15:23
  • If you've already got the awk parts working (if you'd rather use another scripting language that's fine) for one file, you can use the shell for f in *.csv; do ... done or find ... | while read f; do ... done techniques to do many files; redirect the script output to get your one file out. Commented Feb 18, 2015 at 15:26
  • having bash and probably awk it far enough. Commented Feb 18, 2015 at 15:32
  • could you give me a quick example script so I can understand what you mean? thanks Commented Feb 18, 2015 at 15:52

1 Answer 1

1

Perhaps this will help?

grep gives you the filename of each file it finds a pattern.

You can pipe the output to sed and translate the ':' after each filename to a ',' as follows:

    grep FS *.awk  | sed 's/:/,/'
    empty.awk,BEGIN { FS = "\t" }
    join.awk,    OFS = sep = "\t"
    merge.awk,awk ' BEGIN { FS = "\t" }
    p11.awk,BEGIN { FS = "\t"   # make tab the field separator
    p42.awk,BEGIN                   { FS = OFS = "\t" }
    p43.awk,BEGIN   { FS = OFS = "\t" }
    p48.awk,BEGIN { FS = "\t" }
    p52.awk,BEGIN   { OFS = ":"; ORS = "\n\n" }
    p54.awk,BEGIN { FS = "\t" }
    smith.awk,BEGIN         { RS = ""; FS = "\n" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.