0

I have a file that has hundreds of lines and each line has a set of characters with the delimiter ~ like below.

T01~T02~T03~T04~T05~T06~T07~T08~T09~T10~T11~~T13
.
.
.
.

I need to remove T02,T11 and T12 using awk. If you see the above string the T12 is null but still i need to empty that position and my output should look like :

T01~T03~T04~T05~T06~T07~T08~T09~T13

I have tried the following awk command

awk -F~ '{$2=$11=$12="";print $0}'

but it's giving the output like

T01 T02 T03 T04 T05 T06 T07 T08 T09 T10 T11  T13

Can anyone please let me know if I missed anything..

1
  • cut -d~ -f 1,3-9,13 Commented Sep 25, 2013 at 10:49

3 Answers 3

2

You need the OFS, and you can change print $0 to 1

awk -F~ '{$2=$11=$12=""} gsub(/~+/,"~")' OFS="~"
T01~T03~T04~T05~T06~T07~T08~T09~T10~T13
Sign up to request clarification or add additional context in comments.

1 Comment

Not enought. He wants to get rid of separators between those removed fields. Try with gsub(), like: gsub(/~+/, "~")
1

You could use cut:

$ echo "T01~T02~T03~T04~T05~T06~T07~T08~T09~T10~T11~~T13" | cut -d~ -f1,3-10,13
T01~T03~T04~T05~T06~T07~T08~T09~T10~T13

Comments

0

You also need to change the output field separator:

awk  -F~ '{$2=$11=$12="";print $0}' OFS="~"

1 Comment

This leaves several separators between those fields removed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.