Issue
I have a file in below format : inputfile.csv id,flag 1,Y 2,N 3,Y
and a variable as
key="x2"
I want a shell script which can give output file as :
output.csv
id,flag,file_nm
1,Y,1_x2.pdf
2,N,UNK
3,Y,3_x2.pdf
so basically here I want is file_nm , if flag=Y then file_nm = $id"_"$key".pdf
I tried awk command as below in .sh or shell script
key="x2" ## key can change
awk -F"," 'NR==1{$3="PDF_FILE_NM";print;next} $2 == "Y" {$3=$1"_"$key".pdf"}; $2 == "N" {$3="UNK"}1' OFS="," ./inputfile.csv > ouput.csv
But this does not work and gives me weird output. I am not good in unix whatever code is given here is by research only.
thanking in advance
Solution
Like this, using any awk
:
awk -v suf=x2.pdf 'BEGIN{FS=OFS=","} $2=="Y"{$3=NR"_"suf} $2=="N"{$3="UNK"} 1' file
1,Y,1_x2.pdf
2,N,UNK
3,Y,3_x2.pdf
Answered By - Gilles Quénot Answer Checked By - Willingham (WPSolving Volunteer)