Awk edit only 1 column with regex

I have a CSV file with three columns:

id,text,date
123,hi 你好吗?,2016-01-01
246,this is stackoverflow 我需要帮忙,2016-02-01

I only want to edit column 2, where I delete only English characters and keep Chinese ones. The remaining columns remain intact.

The output I want:

id,text,date
123,你好吗?,2016-01-01
246,我需要帮忙,2016-02-01

Is there a better way to do this than this:

cat myfile.csv|cut -d, -f2|sed 's/[a-zA-Z]*//g' > tmp.csv
paste -d, myfile.csv tmp.csv|awk -F, '{OFS=",";print $1,$7,$3}' >tmp2.csv
+4
source share
4 answers
awk -F, 'BEGIN {OFS=","} { if (NR>1) {gsub(/[\x00-\x7F]/, "", $2)}; print }' test.txt
  • NR>1: do not work on the first line
  • gsub(/[\x00-\x7F]/, "", $2): get rid of ascii characters in column 2. doc
+3
source

If the script you posted at the bottom of your question works for you, it will:

awk 'BEGIN{FS=OFS=","} NR>1{gsub(/[a-zA-Z]/,"",$2)} 1' file

You said “characters,” though not “letters,” but YMMV.

+2
source
awk -F, '{ s=split($2,t," "); sub($2, t[s]); print }' file
id,text,date
123,你好吗?,2016-01-01
246,我需要帮忙,2016-02-01
0
awk 'NR==1{print;}NR>1{gsub(/[a-zA-Z ]+/,"");print;}' your_file
id,text,date
123,你好吗?,2016-01-01
246,我需要帮忙,2016-02-01
0

Source: https://habr.com/ru/post/1626455/


All Articles