简体   繁体   中英

Removing first line or row (header) and first column of multiple csv files

I have thousands of csv files, I want to remove the first row (header) and first column. This is my code, but it to overwrite the original files with no content:

for j in *.csv; do sed '1d' $j | cut -d, -f 1 > "$j"; done

Thanks

This is my code, but it to overwrite the original files with no content

That is because the redirection of output > "$j" causes the file to be truncated to zero size before its contents have been read. To avoid this, a temporary output file can be used.
A minor error in the code is that you specified the column to be removed instead of the columns to be kept in the cut command.

Corrected code:

for j in *.csv; do sed 1d $j | cut -d, -f2- >new$j && mv new$j $j; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM