The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 2 03-17-2009 01:17 AM
To call/execute a shell script from a shell script konark UNIX for Dummies Questions & Answers 1 10-26-2007 05:16 PM
How to pass a parameter from one Shell-script to another Shell-script subodhbansal Shell Programming and Scripting 2 09-22-2007 05:19 AM
How to Run a shell script from Perl script in Parent shell? hifake Shell Programming and Scripting 16 08-28-2007 08:42 PM
Have a shell script call another shell script and exit heprox Shell Programming and Scripting 2 11-20-2006 08:17 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-12-2006
misenkiser misenkiser is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Help in shell script

Hi,

I have a requirement in which i have strings in a flat file separated by pipe character as delimiter (|)
All strings are enclosed in double quotes

This delimiter can also be within the data string
I need to remove the double quotes from those data strings having the same delimiter within itself as the actual delimiter

For eg If the flat file has strings like
"abc"|"xyz"|"def|ghi"

I want to keep the last string ie ""def|ghi" intact
and remove the double quotes from remaining strings in the file which do not have a delimiter in between them
so my o/p will now look like

abc|xyz|"def|ghi"



Thnx in advance
  #2 (permalink)  
Old 09-12-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Try
Code:
#! /bin/sh
awk 'BEGIN { FS="|"; OFS="|" }
{
gsub( /"/ , ""  , $1 ); gsub( /"/, "" , $2 )
print $1, $2, $3, $4
}' testfile
  #3 (permalink)  
Old 09-12-2006
misenkiser misenkiser is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Hi shereenmotor,


My FF has n number of strings so how do I generalise it ?
  #4 (permalink)  
Old 09-12-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Try this:
Code:
#! /bin/sh

awk 'BEGIN { FS="|" }
{
col1=length($1); col2=length($2)
wid=col1+col2+1
col=substr($0,1,wid)
gsub( /"/, "", col )
print col, substr($0,wid+1)
}' testfile
  #5 (permalink)  
Old 09-12-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
awk '
{
n = split($0,parts,"\"")
for (k = 1; k <= n; k++)
{
	if( match ( parts[k] , "|" ) && length( parts[k] ) >1 )
		printf("\"%s\"",parts[k]);
	else
		printf("%s",parts[k]);
}
printf("\n");
}' file
  #6 (permalink)  
Old 09-12-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Quote:
Originally Posted by misenkiser
Hi,

I have a requirement in which i have strings in a flat file separated by pipe character as delimiter (|)
All strings are enclosed in double quotes

This delimiter can also be within the data string
I need to remove the double quotes from those data strings having the same delimiter within itself as the actual delimiter

For eg If the flat file has strings like
"abc"|"xyz"|"def|ghi"

I want to keep the last string ie ""def|ghi" intact
and remove the double quotes from remaining strings in the file which do not have a delimiter in between them
so my o/p will now look like

abc|xyz|"def|ghi"



Thnx in advance


Just an alternative , in Python:
Code:
all = open("input.txt").read()
t = all.split('"')
t.pop(0) #remove first element
t.pop(-1) #remove last element
for num,i in enumerate(t):
 	if "|" in i and i != "|":
 		t[num] = '"' + i + '"'
print ''.join(t)

Input.txt:
"abc"|"xyz"|"def|ghi"
"abc"|"def|opq"|"def|xyz"


Output:
Code:
c:\> python test.py
abc|xyz|"def|ghi"
abc|"def|opq"|"def|xyz"
  #7 (permalink)  
Old 09-13-2006
misenkiser misenkiser is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Thanx anbu23 your solution works perfectly fine
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0