How to ignore Pipe in Pipe delimited file? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to ignore Pipe in Pipe delimited file?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-08-2013
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 28 October 2014, 1:09 PM EDT
Posts: 114
Thanks: 34
Thanked 0 Times in 0 Posts
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:

Code:
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 representing as 3rd coulmn in that file
Sponsored Links
    #2  
Old 08-08-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
You can do this

Code:
awk -F'"' '{print $2}' file
xyz|zzz

Sponsored Links
    #3  
Old 08-08-2013
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 28 October 2014, 1:09 PM EDT
Posts: 114
Thanks: 34
Thanked 0 Times in 0 Posts
Hi ths would give me searching "" and printing but i dont want to avoid Pipe delimited as well my file is totally having columns with pipe dilimted so i want to cosider if "xyz|zzz" occurs it should take as single col


Code:
xx|yy|"xyz|zzz"|12 ...

$1 - xx
$2 - yy
$3 - xyz|zzz
$4 - 12

    #4  
Old 08-08-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 October 2014, 6:50 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
You could try something like:

Code:
sed 's#\("[^|]*\)\([|]\)\([^|]*"\)#\1,\3#g' file | awk -F\| '{gsub(/"/,x);sub(",","|",$3);print $3}'

The sed is substituting the pipe to comma so that awk can recognize it as one field.

Later awk substitute the comma back to pipe.
Sponsored Links
    #5  
Old 08-08-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
Found this ugly code using google so can not help you with it, only that its replace the | , to make it work
Some guru may be able to simplify it


Code:
sed -e 's/|/,/g' file | awk '{
$0=$0",";
while($0) {
  match($0,/[^,]*,| *"[^"]*" *,/);
  sf=f=substr($0,RSTART,RLENGTH);
  gsub(/^ *"?|"? *,$/,"",f);
  print "Field " ++c " is " f;
  sub(sf,"");
}}'
Field 1 is xx
Field 2 is yy
Field 3 is xyz,zzz
Field 4 is zzz
Field 5 is 12.

PS you may find some here: http://www.gnu.org/software/gawk/man...y-Content.html
Sponsored Links
    #6  
Old 08-08-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 31 October 2014, 12:14 PM EDT
Location: Aachen, Germany
Posts: 4,485
Thanks: 74
Thanked 1,099 Times in 1,035 Posts
Maybe you can adapt this post to fit your needs? It's for commas in comma delimited fields, but it should do the task after some fiddling...
Sponsored Links
    #7  
Old 08-09-2013
rohit_shinez rohit_shinez is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 28 October 2014, 1:09 PM EDT
Posts: 114
Thanks: 34
Thanked 0 Times in 0 Posts
Thanks Yoda i will try to apply this but wat i needed is
i am having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field
for eg:


Code:
file1.txt

Code:
xx|yy|xx|12

output file:

Code:
xx|yy||12

the above one i achieved with this below code using http://www.unix.com/shell-programmin...#post302841467

Code:
awk 'BEGIN {FS=OFS="|" } $3 ~ /[[:alnum:]]/ { $3="" }1' file

but wat i faced is if there is any column having pipe that should consider as single column

Code:
xx|yy|"xyz|xx"|AAA|12...

not i should achieve my requirement like this

Code:
xx|yy|"xyz|xx"||12

now AAA should replace with null considering as AAA as 4th column if use

Code:
awk 'BEGIN {FS=OFS="|" } $4 ~ /[[:alnum:]]/ { $4="" }1' file


Last edited by Franklin52; 08-09-2013 at 02:42 AM.. Reason: Please use code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with converting Pipe delimited file to Tab Delimited karumudi7 Shell Programming and Scripting 6 02-01-2012 04:55 AM
Reading a pipe delimited file mac4rfree UNIX for Dummies Questions & Answers 3 10-28-2011 05:12 AM
How to convert a space delimited file into a pipe delimited file using shellscript? nithins007 Shell Programming and Scripting 7 09-25-2011 05:33 AM
Delete last value from pipe delimited file relentl3ss UNIX for Dummies Questions & Answers 3 02-17-2010 05:27 AM
How to split pipe delimited file njgirl Shell Programming and Scripting 4 06-18-2008 05:15 PM



All times are GMT -4. The time now is 05:25 PM.