Created
October 21, 2022 15:48
-
-
Save mehdichaouch/225a5da5e0277c704668ff665b67251f to your computer and use it in GitHub Desktop.
Command shell CSV transformation - Remove columns / drop a column / delete first column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Memo to found it quicker in future... | |
# | |
# Q: How to delete a column/columns of a CSV file which has cell values with a string enclosed in " , "? | |
# https://www.unix.com/302824377-post1.html | |
# | |
# A: https://www.unix.com/302825517-post6.html | |
# Dataset in: en_EN-export-sample.csv | |
# | |
#"Are you sure you want to do this?","Are you sure you want to do this?",module,Custom_Module | |
#"Duplicate Content","Duplicate Content",module,Custom_Module | |
#Reset,Reset,module,Custom_Module | |
#"Save and Continue Edit","Save and Continue Edit",module,Custom_Module | |
#"Save, Content","Save, Content",module,Custom_Module | |
awk 'FNR==1 {CNT=split(COLS, CNo, ",")} | |
{for (i=2; i<=NF; i+=2) { | |
gsub (/,/, "\001", $i) | |
} | |
split ($0, TMP, ",") | |
$0="" | |
for (i=1; i<=CNT; i++) $0=$0 ($0?",":"") TMP[CNo[i]] | |
gsub ("\001", ",") | |
} | |
1 | |
' FS="\"" OFS="\"" COLS=1,2 en_EN-export-sample.csv | |
# same in one line (almost) | |
awk 'FNR==1 {CNT=split(COLS, CNo, ",")} {for (i=2; i<=NF; i+=2) {gsub (/,/, "\001", $i)} split ($0, TMP, ",") | |
$0="" | |
for (i=1; i<=CNT; i++) $0=$0 ($0?",":"") TMP[CNo[i]] | |
gsub ("\001", ",")} 1' FS="\"" OFS="\"" COLS=1,2 en_EN-export-sample.csv | |
# return: | |
#"Are you sure you want to do this?","Are you sure you want to do this?" | |
#"Duplicate Content","Duplicate Content" | |
#Reset,Reset | |
#"Save and Continue Edit","Save and Continue Edit" | |
#"Save, Content","Save, Content" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before ➡️ After
