Awk: modifying second column of a .csv table

I am trying to modify second column of a .csv table with the following command but its not working. Thanks for your help.

 awk '{gsub("mnt/data/code/analysis_tree/data/processed/16S/shi7_learning/fastqs_stitched","home/irshad",$2)}1' fastq_manifest.csv > manifest.csv

In what exact way "its not working"?
Any error messages?
What happens?
I see you're using gsub. Are you sure there's something to substitute you're specifying in gsub?
Are you sure your csv is tab-delimited?
Have tried specifying FS to awk as 'tab'?
Any "weird" chars in your csv file? (what does cat -vet myFile.csv say?)
Anything else you've tried to troubleshoot your issue?

1 Like
awk '{gsub("mnt/data/code/analysis_tree/data/processed/16S/shi7_learning/fastqs_stitched","/home/irshad/ihaq/rawdata")}1' fastq_manifest.csv > manifest.csv

This one worked but I didn't specify FS to the awk as 'tab'.

Good. I'll leave it up to you to figure out as to why!
Also pls start using markdown Code tags to properly post code/data samples.

Thanks for your help! I removed the ",$2" in the first command and it worked. With that I wanted to specify the column number instead of the whole file.

Thanks

Well, if you don't specify the third argument to gsub, it defaults to $0 (the whole record/line).
Something is not right with your input data. Or it's not what you think it's

1 Like

I edited my question. You are right its not a tab-delimited file. I was mixing it up with another one.
So, the input file has three columns:

sample-id,absolute-filepath,direction
I wanted to change the contents of "absolute-filepath" from "/mnt/data/code/analysis_tree/data/processed/16S/shi7_learning/fastqs_stitched/" to "/home/irshad/ihaq/rawdata"

I used the following command:

awk '{gsub("mnt/data/code/analysis_tree/data/processed/16S/shi7_learning/fastqs_stitched","/home/irshad/ihaq/rawdata")}1' fastq_manifest.csv > manifest.csv

Does it make sense now? Thank you,

Well, now you can put $2 back into the gsub AND specify your spanking new input (and output) field separatorS as,

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.