2

I am trying to delete the first row of each of my .csv files in my DIR folder using the sed:

DIR=/home/results
for filename in "$DIR"; do
        sed 1d filename.csv
done

However, this doesn't work. I am new to bash scripting and would be thankful if anyone tells me how to fix this.

rehm20
  • 23
  • 1
  • 3
  • 3
    Possible duplicate of [How can I remove the first line of a text file using bash/sed script?](http://stackoverflow.com/questions/339483/how-can-i-remove-the-first-line-of-a-text-file-using-bash-sed-script) – Inian Jul 04 '16 at 14:32

1 Answers1

3

Just do:

for f in /home/results/*.csv; do sed -i '1 d' "$f"; done

The glob pattern /home/results/*.csv matches all .csv files in /home/results/ directory and then the for construct iterate over the files, sed does the in place removal of the first row from each file.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • Thank you so much! This works and now I want to have a new row as a first line of this which I am trying to do by echo "col1, col2, col3" > all.csv but it doesnt work. Do you have suggestions for this? – rehm20 Jul 04 '16 at 14:35
  • @rehm20 You could do that using `sed`: `for f in /home/results/*.csv; do sed -i '1 s/.*/col1, col2, col3/' "$f"; done` – heemayl Jul 04 '16 at 14:37