I have a bunch of *.csv files that use headers. Right now the files have headers and the same number of lines of data. I need to erase the data in these files, so I figured I could just overwrite the header into the files. I have file A set up properly with the header. How can I cat A into all the other csv files?
Alternatively, how could I delete the last X number of lines from the files? The path for the files is */*.csv
2 Answers
Cat the header into each of the files. I am assuming that
Ais a file since you mentionedcat. If so, you can just copy:for file in */*.csv; do cp A "$file"; doneIf
Ais a variable, dofor file in */*.csv; do echo "A" > "$file"; doneAlternatively, if all you need is the first line, you can cat that into each file. That way, it will also work for different headers:
for file in */*.csv; do head -n "$file" > /tmp/foo && mv /tmp/foo "$file"; doneThere are many ways to delete the last X number of lines from the files. Here are three:
head, change X to however many lines you want to keep:for file in */*.csv; do head -n X $file > /tmp/foo && mv /tmp/foo "$file"; doneawk, change the123to however many lines you want to keep:for file in */*.csv; do awk 'NR<=123' "$file" > /tmp/foo && mv /tmp/foo "$file"; doneperl, change the123to however many lines you want to keep:for file in */*.csv; do perl -ne "next if $.>123;print' "$file" > /tmp/foo && mv /tmp/foo "$file"; done
You can use sed and some pipe creativity to convert a list of files (or anything) into a list of commands.
This is a test, it will just output the commands for you to see what is going to happen:
$ ls *.cvs | sed "s/.*/cat headerfile.cvs > &/"If you like it, do the same and pipe it to your favorite shell:
$ ls *.cvs | sed "s/.*/cat headerfile.cvs > &/" | bashOr for a recursive solution you can use find:
find . -name \*.cvs | sed "s/.*/cat headerfile.cvs > &/" | bash