I have a csv file that looks like:
data/train/4/36280.png,four
data/train/2/10317.png,two
data/train/2/57890.png,two
data/train/1/53448.png,one
data/train/8/58233.png,eight
data/train/4/23599.png,four
data/train/2/35051.png,two
data/train/1/12323.png,one
data/train/9/18562.png,nine
data/train/8/46629.png,eight
data/train/7/1746.png,seven
Where first column is path and second is class. I would like to change the second column conditionally. In pseudo code I want something like:
If second column "four" change it to the next row's class
So far I've tried just to see if I can change ne class to another, but without success:
awk '{ if ($2 == "zero") $2="one"; print $0 }' train.csv > new_file.csv
gives me the same csv.
Basically I would like a script that gives me following output csv:
data/train/4/36280.png,seven
data/train/2/10317.png,four
data/train/2/57890.png,two
data/train/1/53448.png,two
data/train/8/58233.png,one
data/train/4/23599.png,eight
data/train/2/35051.png,four
data/train/1/12323.png,two
data/train/9/18562.png,one
data/train/8/46629.png,nine
data/train/7/1746.png,eight
bash
solution:Equivalent
awk
solution:Both solutions first read the lines into arrays
filename
andclass
. Only difference is that thebash
array starts with0
, theawk
array with1
.Then, we loop over the arrays and print the desired output. In the
awk
solution, we need to treat the first line separately as, unlike inbash
,array[-1]
does not return the last element.The
awk
solution is a bit faster.