Swaram Jagtap
Swaram Jagtap

Reputation: 572

How to delete a set of characters from a file in shell script

I want to delete a set of characters from a file. The sample file is as follows . I want to delete characters preceding the '-' (including '-')in the 8th column (separator=','). sample file:

aa,bb,cc,dd,ff,rr,dd,ff-gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,ff-gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,ff-gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,ff-gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,ff-gjgj,dd,dd,dd

The output file should look like as:

aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd

aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd

Upvotes: 1

Views: 143

Answers (1)

anubhava
anubhava

Reputation: 785266

You can use this awk command:

awk 'BEGIN{FS=OFS=","} {sub(/^[^-]*-/, "", $8)} 1' file
aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd
aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd
aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd
aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd
aa,bb,cc,dd,ff,rr,dd,gjgj,dd,dd,dd

Upvotes: 4

Related Questions