Pipe not recognize with gawk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pipe not recognize with gawk
# 1  
Old 07-29-2012
Pipe not recognize with gawk

Hi !

I am a newbie with Unix and I try to remove the "|" FS in this file:
Code:
1|2|3|4|5|6

to get:
Code:
123456

by using:
Code:
gawk 'BEGIN{FS=""} {gsub("|","",$0); print $0}' file

but nothing changes.

Or even when I try to change "|" by "%"with this command:
Code:
gawk 'BEGIN{FS=""} {gsub("|","%",$0) ;print $0}' file

I get:
Code:
%1%|%2%|%3%|%4%|%5%

I don't get it !
If someone could explain me, it would be great !
# 2  
Old 07-30-2012
Vertical bar is a special character in a regular expression. Use:

Code:
 gsub( "\\|", "", $0 );

This will escape it. Two escapes are necessary to ensure that one escape reaches the regex parser.
This User Gave Thanks to agama For This Post:
# 3  
Old 07-30-2012
Thanks agama !

I didn't know 2 escapes was necessary.
# 4  
Old 07-30-2012
Two levels of quoting are required only if a string literal is eventually used as a regular expression. The first argument to gsub is actually a regular expression, so there is never any reason to use a string literal in the function call. Do yourself a favor and spare yourself the hassle of two levels of quoting different sets of special characters.
Code:
 gsub( /\|/, "", $0 );

However, there are times when it's necessary to use a dynamically-assigned, string-valued variable as the first argument, but even then, the first argument to gsub isn't itself a string literal.

Regards,
Alister

Last edited by alister; 07-30-2012 at 01:21 AM..
This User Gave Thanks to alister For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to recognize the Shell?

Hello All I am working on Red Hat Enterprise Linux Server release 5.1 when I logged on to the shell and then write echo $SHELL it shows me the result /bin/bash and when I write csh, the prompt changes and I feel that I am now working upon C shell but when I do echo $SHELL it shows me the... (9 Replies)
Discussion started by: adisky123
9 Replies

2. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

3. Debian

CUPS Cannot recognize lp0

I am trying to print from Debian and receive the following message: "Unable to open device file"/dev/lp0": Permission Denied" The permissions for lp0 are 666. Advice and comments, please. :wall: Thanks (0 Replies)
Discussion started by: woofy613
0 Replies

4. UNIX for Advanced & Expert Users

recognize servers environment

Basically do you have any suggestions on how to advise syadmins to be more careful in accessing servers so that they would know at an instance that the server they logging in is production or not(perhaps naming convention etc, color terminal? We have recent incident of wrong restoration to a... (2 Replies)
Discussion started by: lhareigh890
2 Replies

5. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

6. Solaris

Solaris recognize HW Raid ??

Hardware: HP P2000 HP DL380 G7 with Solaris Software: Solaris 10 05/08 I had made a Hardware raid on P2000 and install solaris on G7, The raid card controller is working fine. How can I make the raid works on OS?? "raidclt" is getting nothing :wall::wall: Thanks (11 Replies)
Discussion started by: stanley1024
11 Replies

7. UNIX for Dummies Questions & Answers

how does cups recognize a new driver?

hi there, i am attempting to recognize a network printer on my red hat box. i know the IP, i have procured the correct driver, however, CUPS (nor the add printer utility) doesn't recognize the driver nor the printer type. i am assuming this has to do with the location of the driver. can anyone... (8 Replies)
Discussion started by: vickenyon
8 Replies

8. UNIX for Dummies Questions & Answers

Re: How do I recognize a zombie process?

Hey Guys, I am not really new to Unix/Linux however I was never taught how to spot a zombie process. I used top to check out the processes I was running and how the resources were looking and in the upper right it said 1 zombie, I have attached a jpeg of it. Thank you in advance for your... (4 Replies)
Discussion started by: pikecoguy
4 Replies

9. Solaris

OS not recognize the new disk while adding

Hi all, I tried to add the new disk into veritas control.but the OS/veritas is not recognize the disk.so how i can know disk status or the disk added or not into veritas?please help me out step by step procedure.i would really thankful to all. regards Krishna (4 Replies)
Discussion started by: murthy76
4 Replies

10. Programming

How to recognize that the server is currently unavailable?

hi all How to recognize that the server is currently unavailable? by programatically.give some example. am using fedora5 AMD cheers (2 Replies)
Discussion started by: munna_dude
2 Replies
Login or Register to Ask a Question