Skip to content

Instantly share code, notes, and snippets.

@magikcypress
Last active February 21, 2021 15:48
Show Gist options
  • Save magikcypress/cfd85a7ea11ff210a5f2afae6379f333 to your computer and use it in GitHub Desktop.
Save magikcypress/cfd85a7ea11ff210a5f2afae6379f333 to your computer and use it in GitHub Desktop.
Replace field into column csv file bash script

Replace field into column csv file bash script

First command is more faster than second

Exemple file

    1613667679,TXT,google.com,8.8.8.8,4,0,4,0,N/A,2,N/A
    1613667679,TXT,google.com,,8.8.8.8,4,0,4,0,N/A,2,N/A
    1613667679,TXT,google.com,,8.8.8.8,4,0,4,0,N/A,2,N/A

Replace timestamp to date first column

    sed -r 's#(([^0-9][0-9]{0,9})*)(\b[0-9]{10}\b)(([0-9]{0,9}[^0-9])*)#printf "%s%s%s" "\1" $(date -d@\3 "+%Y-%m-%d %H:%M:%S") "\4";#ge' /mnt/backup/file.csv > file.csv.t
    mv file.csv{.t,}

Replace timestamp to date first column

    IFS=,
    while read -ra a; do
            # delete double quote
            a="${a[0]//\"}"
            newDate=$(date +"%Y-%m-%d %H:%M:%S" -d @$a)
            a=$(echo "${a[0]}" | sed "s/${a[0]}/$newDate/");
            echo "${a[*]}";
    done < file.csv.csv > file.csv.t
    mv file.csv{.t,}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment