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
can Awk split my field on the . oly_r Shell Programming and Scripting 6 10-26-2007 07:16 PM
split varibles and store fields into shell varible array gratus Shell Programming and Scripting 3 10-11-2007 02:50 PM
returning split fields mervin2006 Shell Programming and Scripting 3 05-11-2007 01:21 PM
Create a calculated field from existing fields atchleykl UNIX for Dummies Questions & Answers 1 04-19-2007 11:13 AM
Split a field in awk script CamTu Shell Programming and Scripting 4 03-21-2005 04:03 PM

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 02-20-2008
vbrown vbrown is offline
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
  #2 (permalink)  
Old 02-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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
  #3 (permalink)  
Old 02-20-2008
bobbygsk bobbygsk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 90
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.
  #4 (permalink)  
Old 02-21-2008
vbrown vbrown is offline
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
  #5 (permalink)  
Old 02-21-2008
vbrown vbrown is offline
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
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 03:42 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