Skip to content
Advertisement

Remove specific value in R or Linux

Hi I have a file (tab sep) in terminal that has several columns as below. You can see last column has a comma in between followed by one or more characters.

1 100 Japan Na pa,cd

2 120 India Ca pa,ces

5 110 Japan Ap pa,cres

1 540 China Sn pa,cd

1 111 Nepal Le pa,b

I want to keep last column values before the comma so the file can look like

2 120 India Ca pa

5 110 Japan Ap pa

1 540 China Sn pa

1 111 Nepal Le pa

I have looked for sed but I cannot find a way to exclude them

Regards

Advertisement

Answer

In R you can read the file with a tab-separator and remove the values after comma.

result <- transform(read.table('file1.txt', sep = 't'), V5 = sub(',.*', '', V5))

V5 is used assuming it is the 5th column that you want to change the value.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement