Shreya
Shreya

Reputation: 649

Remove specific word from field

I have file which has some data as below

cell   input   out   type   fun
AI20   A1,A2   Z     comb   0 ,((A1,A2))

So when ever there is starting 0 in fun column I want to remove that 0 , So my output as

cell   input   out   type   fun
AI20   A1,A2   Z     comb   ((A1,A2))

I tried code

awk '$5~/^0/ {sub(/0 ,/,"",$5)} 1' file 

But this didn't worked.

Upvotes: 2

Views: 714

Answers (4)

stack0114106
stack0114106

Reputation: 8711

You tried

awk '$5~/^0/ {sub(/0 ,/,"",$5)} 1' file 

Here $5 is just 0, the comma after that is stored in $6, so you need to remove that as well

Try this.

$ awk '$5~/^0/ {sub(/^0/,"",$5); sub(/^,/,"",$6) } 1' neha1.txt | column -t
cell  input  out  type  fun
AI20  A1,A2  Z    comb  ((A1,A2))

Upvotes: 0

Carlos Pascual
Carlos Pascual

Reputation: 1126

Tested in GNU awk. it's possible to set FS for removing that 0 , you want and divide the row in two parts, and print it.

awk -v FS='.[[:digit:]][[:blank:]]+,'  ' {print $1, $2} '  file
cell   input   out   type   fun
AI20   A1,A2   Z     comb   ((A1,A2))

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133458

With shown samples could you please try following.

awk '
match($0,/0 ,\(\(/){
  val=substr($0,RSTART,RLENGTH)
  sub(/.*,/,"",val)
  print substr($0,1,RSTART-1) val substr($0,RSTART+RLENGTH)
  val=""
  next
}
1
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                               ##Starting awk program from here.
match($0,/0 ,\(\(/){                ##Using match function to match 0 space (( in line.
  val=substr($0,RSTART,RLENGTH)     ##creating val which has sub string of matched regex.
  sub(/.*,/,"",val)                 ##Substituting everything till comma with NULL in val.
  print substr($0,1,RSTART-1) val substr($0,RSTART+RLENGTH)  ##Printing sub string val and rest of line sub string here.
  val=""                            ##Nullifying val here.
  next                              ##next will skip all statements from here.
}
1                                   ##will print the current line here.
' Input_file                        ##Mentioning Input_file name here.

Upvotes: 1

anubhava
anubhava

Reputation: 784998

You may use this awk:

awk -F '[[:blank:]]{3,}' 'NR > 1 { sub(/^0+[, ]*/, "", $NF) } 1' file |
column -t
cell  input  out  type  fun
AI20  A1,A2  Z    comb  ((A1,A2))

Upvotes: 1

Related Questions