The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
can Awk split my field on the . oly_r Shell Programming and Scripting 6 10-26-2007 04:16 PM
split varibles and store fields into shell varible array gratus Shell Programming and Scripting 3 10-11-2007 11:50 AM
returning split fields mervin2006 Shell Programming and Scripting 3 05-11-2007 10:21 AM
Create a calculated field from existing fields atchleykl UNIX for Dummies Questions & Answers 1 04-19-2007 08:13 AM
Split a field in awk script CamTu Shell Programming and Scripting 4 03-21-2005 12:03 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-20-2008
Registered User
 

Join Date: Feb 2008
Posts: 3
How to split a field into two fields?

Hi,

I have a comma delimited text file where character fields (as opposed to numeric and date fields) are always enclosed with double quotes. Records are separated by the newline character. In a shell script I would like to split a particular field into two separate fields (enclosed with double quotes). The field I would like to split always begins with <description> and ends with </description> and is always the 5th field in a record.

e.g. I would like to convert this:

18,"A",2008-02-11,"Y","<description> some long text </description>","N",1

to this:

18,"A",2008-02-11,"Y","<description> some lo","ng text </description>","N",1

I'm not bothered where in the field the split occurs - somewhere in the middle is optimal.

Really grateful for any help on this one.

Thanks, Vicky
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-20-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,491
try awk:
Code:
awk -F, '{
       for(i=1; i<NF; i++) {                                                
           if($i ~ /<description>/) { 
                   half=length($i)/2; 
                   printf("%s\",\"%s,", substr($i,1,half),
                                        substr($i,half+1))
           }
           else {
                   printf("%s,", $i)
           }
       }
       print $NF }' oldfile > newfile
Reply With Quote
  #3 (permalink)  
Old 02-20-2008
Registered User
 

Join Date: Oct 2007
Posts: 70
While I was trying to solve the issue it seems Jim has already solved it.
Even I'm new to scripting and I have tried to solve it.
Hope it helps
Quote:
awk -F"," '{print $1,",",$2,",",$3,",",$4,",",substr($5,1,length($5)/2),"\",\"",substr($5,(length($5)/2)+1),",",$6,",",$7,",",$8}' file1
But my script gives extra spaces at the delimiters.
Reply With Quote
  #4 (permalink)  
Old 02-21-2008
Registered User
 

Join Date: Feb 2008
Posts: 3
Thanks a lot for your help.

I should have said in my initial post that there may be text in between the double quotes which themselves are in double quotes and may contain commas,

e.g. 18,"<description><job_title value="some text, more text" /></description>",2008-02-19,"N"

I think this makes it a lot more complicated?

I'm also having to use nawk (I'm on Solaris) as each record is likely to be more than 3000 characters (max for awk), but I think the syntax is the same/similar to awk.

Any ideas?

Thanks again
Vicky
Reply With Quote
  #5 (permalink)  
Old 02-21-2008
Registered User
 

Join Date: Feb 2008
Posts: 3
Follow up:

Think I have managed to sort out what I want with only a minor modification to user "jim mcnamara" solution:

I used nawk and put </description> as the record delimiter instead of ,

Solution:
nawk -F "</description>" '{
for(i=1; i<NF; i++) {
if($i ~ /<description>/) {
half=length($i)/2;
printf("%s\",\"%s", substr($i,1,half), substr($i,half+1))
}
else {
printf("%s,", $i)
}
}
print $NF }' oldfile > newfile
fi

Thanks so much for your help.

Vicky
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0