Brian P
Brian P

Reputation: 1516

CSV manipulation with sed - apply sed to multiple lines

I know this question has been asked before but I haven't quite figured it out (command line noob). So, here is a sed command that deletes the first seven lines of a csv and then the last two lines.

cat test.csv | sed "1,7d" | sed 'N;$!P;$!D;$d' > test2.csv

Now, based on some other posts, I have been putting together something that will allow me to apply this script to all my csv files in a single folder. I have been piecing together lots of different code examples but it seems I have to have a single sed command rather than piping them together. Any assistance to help a noob would be greatly appreciated.

Upvotes: 1

Views: 132

Answers (1)

glenn jackman
glenn jackman

Reputation: 246764

"delete first 7 lines and then the last 2 for all files in a directory"

for file in *.csv; do 
    sed '1,7d' "$file" | tac | sed '1,2d' | tac > "$file.new"
done

Upvotes: 2

Related Questions