Home > database >  Using conditional to rename the values of a column with awk
Using conditional to rename the values of a column with awk

Time:05-30

I am using a dataset with a field called "Store" at the second column of the dataset.

"Store"
  A
  B
  C
  A

The following code is not working. What I want is to rename "A" values to "A_store"

awk -F, 'BEGIN {FS=","} {if (NR!=1 && $2=="A") $2="A_store"}' output.csv

The desired output:

"Store"
A_store
B_store
C_store
A_store

CodePudding user response:

Your goal isn't clear to me, but perhaps this will help:

cat test.txt
"Store", "something else", "another thing", "last thing"
A, 1, 5, q
B, 2, 6, r
C, 3, 7, s
A, 4, 8, t

awk -F"," '{if(NR>1) $1=$1"_store"; print}' test.txt
"Store", "something else", "another thing", "last thing"
A_store  1  5  q
B_store  2  6  r
C_store  3  7  s
A_store  4  8  t

awk -F"," '{if(NR>1 && $1=="A") $2=$1"_store"; print}' test.txt
"Store", "something else", "another thing", "last thing"
A A_store  5  q
B, 2, 6, r
C, 3, 7, s
A A_store  8  t

awk -F"," '{if(NR>1 && $1 == "A") $2=$1"_store"; else if(NR>1) $2=$1; print}' test.txt
"Store", "something else", "another thing", "last thing"
A A_store  5  q
B B  6  r
C C  7  s
A A_store  8  t

Also, if you have whitespace in your strings you will need to alter your code slightly, e.g.

cat test.txt
"Store", "something else", "another thing", "last thing"
  A, 1, 5, q
  B, 2, 6, r
  C, 3, 7, s
  A, 4, 8, t

awk -F"," '{if(NR>1 && $1 ~ "A") $1=$1"_store"; print}' test.txt
"Store", "something else", "another thing", "last thing"
  A_store  1  5  q
  B, 2, 6, r
  C, 3, 7, s
  A_store  4  8  t

Does that solve your problem?

CodePudding user response:

if OP wanted to retain commas (as hinted by "output.csv"), then something like

SAMPLE INPUT

"misc","Store", "something else", "another thing", "last thing"
  ,A, 1, 5, q
  ,B, 2, 6, r
  ,C, 3, 7, s
  ,A, 4, 8, t

CODE

{m,g}awk 'BEGIN{ _ =_^=FS=OFS="," } sub("^A$","&_store",$_)^!_' 

   or

{m,g}awk 'sub("^A$","&_store",$(!_ !_))^_' FS=, OFS=,

   or

{m,g}awk 'sub("^A$","&_store",$2)^_' FS=',' OFS=','

SAMPLE OUTPUT

"misc","Store", "something else", "another thing", "last thing"
  ,A_store, 1, 5, q
  ,B, 2, 6, r
  ,C, 3, 7, s
  ,A_store, 4, 8, t

CodePudding user response:

# data.csv
foo,"Store",bar
1,AA,1
2,B,1
3,C,1
4,A,1 
 
$ awk -F, -vOFS=, 'NR>1&&$2=="A"{$2=$2"_store"}1' data.csv
foo,"Store",bar
1,AA,1
2,B,1
3,C,1
4,A_store,1

trim whitespaces

# data.csv
foo,"Store",bar
1,AA,1
2,B,1
3,  A  ,1
4,A,1

$ awk -F, -vOFS=, 'NR>1 && $2 ~ /^[ ]*A[ ]*$/ {gsub(/^[ \t] /, "", $2); gsub(/[ \t] $/, "", $2); $2=$2"_store"}1' data.csv
foo,"Store",bar
1,AA,1
2,B,1
3,A_store,1
4,A_store,1
  • Related