Home > Back-end >  Removing quotes from file in Unix (or in R)
Removing quotes from file in Unix (or in R)

Time:01-31

Please find attached two files (files). Both look the same, with one variable containing two numbers separated by a space.

file1.txt was produced in excel as a test, and was saved as tab-separated txt file.

file2.txt was produced in R by uniting two variables with the tidyverse unite function and using sep = " " to get the empty space as the separator. the R object was then exported using write.table(......., sep = "\t")

When I read file1.txt in a terminal, it appears with no quotes, whereas file2.txt does have quotes for every row. For instance: 60 0 in file1 vs "60 0" in file2.

Quotes represent a problem for me at this point, but using R would help me creating files in a iterative manner.

Is there a way to either remove those quotes in Unix, or to create those files in R, but without the quotes?

Thanks a lot for your help. Best,

David PS: I have tried cp file1.txt input; sed -e 's/^"//' -e 's/"$//' <<<"$input", with no success.

CodePudding user response:

MAny ways; here is one (please mark as correct or post note)

Mac_3.2.57$cat ../../../Downloads/file2.txt | awk '{ gsub("\"","") }1' | head -n 10
TA_pos
60 0
72 0
102 0
188 0
246 0
333 0
360 0
426 0
448 0
Mac_3.2.57$cat ../../../Downloads/file2.txt | head -n 10
"TA_pos"
"60 0"
"72 0"
"102 0"
"188 0"
"246 0"
"333 0"
"360 0"
"426 0"
"448 0"
Mac_3.2.57$

CodePudding user response:

either FS | OFS or RS | ORS works :

{m,g,n}awk NF=NF FS='"' OFS=
{m,g,n}awk   8   RS='"' ORS=
TA_pos
60 0
72 0
102 0
188 0
246 0
333 0
360 0
426 0
448 0
  •  Tags:  
  • r
  • Related