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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-01-2007
Registered User
 

Join Date: Jul 2006
Posts: 203
delete a field along with delimiter in the whole file

I have file with 20 fields and its pipe delimiter. I need to remove the 18th field along with pipe delimiter that seperates 17th and 18th field. In turn that means i want to make it now a file with only 19 fields. Can some body let me know how ican remove the 18th field from the whole file?
Sponsored Links
  #2 (permalink)  
Old 11-01-2007
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,210

Code:
awk '$18 = $19 FS $20 { 
	NF = 18 
}                  
1' FS="|" OFS="|"

For this use nawk on Solaris.
  #3 (permalink)  
Old 11-01-2007
Registered User
 

Join Date: Jul 2006
Posts: 203
Radolv,

can you please explain the code if u dont mind?
  #4 (permalink)  
Old 11-01-2007
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,210
Assign the value $19 FS $20 to field 18,
so filed 18 becomes field 19 | field 20.
Then (this works with some awks like GNU Awk and New Awk)
make the record 18 fields (NF = 18, number of fields is 18).
  #5 (permalink)  
Old 11-01-2007
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,210
Of, course,
you can write it like this,
if you wish:


Code:
awk '{
             for (i = 1; i <= NF; i++) {
                     if (i != 18) { 
                             printf (i > 1) ? ("|" ($i)) : $i
                        }
                }
             print ""
        }' FS="|" filename


Last edited by radoulov; 11-01-2007 at 08:19 PM.. Reason: OFS adjusted
  #6 (permalink)  
Old 11-02-2007
Technorati Master
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 3,007
based on radoulov's code


Code:
awk -F \| '{ $17 = $17 FS $19 FS $20; NF=17 }1' OFS="|" t1

Sponsored Links
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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Cutting a tab delimiter file vinod.thayil Shell Programming and Scripting 4 11-30-2007 03:38 PM
Set a variable field delimiter using awk Klashxx Shell Programming and Scripting 2 03-24-2006 09:10 AM
splitting file with more than one delimiter braindrain Shell Programming and Scripting 4 10-06-2005 09:42 PM
How to parse a text file with \034 as field and \035 as end of message delimiter? indianya Shell Programming and Scripting 1 08-26-2005 10:20 PM
field delimiter with a space or more uphamtn UNIX for Dummies Questions & Answers 3 05-15-2003 05:22 PM



All times are GMT -4. The time now is 06:52 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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0