Cut cmd with delimiter as |#|

Hi All-

We have a file data as below with delimiter as |#|

10|#|20|#|ABC
13|#|23|#|PBC

If I want to cut the 2nd field out of this, below command is not working as multiple pipe is causing an issue , it seems

cut -f2 -d"|#|" <file_name>

can you please help to provide the correct command

Regards,
Suresh

Hello,

Please use the following code to resolve this issue. I have just saved your data in tete12 file for example.

 
$ cat tete12
10|#|20|#|ABC
13|#|23|#|PBC

$ cat tete12 | awk -F"|#|" '{print$2}'
20
23

 

Please let me know if this helps.

Thanks,
R. Singh

1 Like

Try using awk

>awk -F"|#|" '{print $2}' filename
1 Like

Thanks guys ..let me check these

---------- Post updated at 06:00 AM ---------- Previous update was at 05:10 AM ----------

Thanks all for help, it is working fine

1 Like

Thanks for the question, and the answers. I've learnt something too.

Robin

I am curious what awks you are using on what OS and version? It should be:

awk -F'[|]#[|]' '{print $2} file

I'm on AIX 5.3. Both the following commands work fine.

awk -F"|#|" '{print $2}' file
awk -F"[|]#[|]" '{print $2}' file

Thanks that is good to know. I did some more tests with several versions of awk on various platforms:

awk -F"|#|" '{print $2}' file

Indeed it works on AIX. It also works with /usr/xpg4/bin/awk on Solaris but not with nawk . It did not work with any of the other awks I tried on various platforms.

awk -F"[|]#[|]" '{print $2}' file

worked with every awk on every platform (except oawk on Solaris obviously)

I found this section:

9.4.3 ERE Special Characters

So that means -F'[|]#[|]' is correct and portable, whereas -F'|#|' is not.

1 Like