Adding fields to a 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.

Adding fields to a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-18-2012
Arun Mishra Arun Mishra is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 June 2014, 4:30 AM EDT
Posts: 51
Thanks: 4
Thanked 0 Times in 0 Posts
BSD Adding fields to a file

Hi All,

I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output.


Code:
Sample Input:
A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West|US HEADQUARTERS
A0010001|Akron OH A 1|A0010954|Cleveland OH|A0010997|Central|US HEADQUARTERS
A0010002|Alaska AK A 1|A0010991|Seattle WA|A0010998|West|US HEADQUARTERS

Sample Output Required:
A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West||US HEADQUARTERS|
A0010001|Akron OH A 1|A0010954|Cleveland OH|A0010997|Central||US HEADQUARTERS|
A0010002|Alaska AK A 1|A0010991|Seattle WA|A0010998|West||US HEADQUARTERS|

Sponsored Links
    #2  
Old 12-18-2012
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 September 2014, 7:13 AM EDT
Location: Zürich
Posts: 8,483
Thanks: 332
Thanked 1,047 Times in 914 Posts

Code:
$ awk 'BEGIN{ OFS=FS="|" } $NF = FS $NF FS' file
A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West||US HEADQUARTERS|
A0010001|Akron OH A 1|A0010954|Cleveland OH|A0010997|Central||US HEADQUARTERS|
A0010002|Alaska AK A 1|A0010991|Seattle WA|A0010998|West||US HEADQUARTERS|

Sponsored Links
    #3  
Old 12-18-2012
Arun Mishra Arun Mishra is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 June 2014, 4:30 AM EDT
Posts: 51
Thanks: 4
Thanked 0 Times in 0 Posts
Hi @Scott,

Sorry about the Subject not being descriptive. And thanks very much for the reply i was testing the code with my file and it really works fine. Just wanted to know if awk is fastest or we do have any other utility which is faster than awk as i am dealing with millions of records.
    #4  
Old 12-18-2012
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 September 2014, 7:13 AM EDT
Location: Zürich
Posts: 8,483
Thanks: 332
Thanked 1,047 Times in 914 Posts
It's OK. Just try to use a subject title that you yourself would type into a search engine and expect to find an answer.

I don't know what would be fastest. You could try sed


Code:
$ sed "s/|[^|]*$/|&|/" file
A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West||US HEADQUARTERS|
A0010001|Akron OH A 1|A0010954|Cleveland OH|A0010997|Central||US HEADQUARTERS|
A0010002|Alaska AK A 1|A0010991|Seattle WA|A0010998|West||US HEADQUARTERS|

Sponsored Links
    #5  
Old 12-18-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 September 2014, 6:41 AM EDT
Location: Amsterdam
Posts: 9,468
Thanks: 279
Thanked 2,389 Times in 2,141 Posts
You could also try this to see if it is faster:

Code:
sed 's/|/||/6; s/$/|/' file


Last edited by Scrutinizer; 12-18-2012 at 06:26 AM.. Reason: removed quote
Sponsored Links
    #6  
Old 12-18-2012
Arun Mishra Arun Mishra is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 June 2014, 4:30 AM EDT
Posts: 51
Thanks: 4
Thanked 0 Times in 0 Posts
Thanks for the reply @scrutinizer but your code only adds blank field before the last field. i needed to add before and after. i can modify it and it can work but takes the same time.

---------- Post updated at 04:34 PM ---------- Previous update was at 04:32 PM ----------

sorry my mistake your code is fine
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
Adding Fields to the file Arun Mishra Shell Programming and Scripting 25 12-20-2012 07:00 AM
Adding fields to file Arun Mishra Shell Programming and Scripting 0 12-18-2012 05:35 AM
regarding adding fields to DSR protocol in ns2.34 khubalkar Programming 1 05-22-2012 06:05 PM
Adding field to file and moving the last 2 fields dsravan Shell Programming and Scripting 8 01-27-2010 12:13 AM
Adding new fields to an existing layout mr_manii Shell Programming and Scripting 3 10-14-2009 07:22 AM



All times are GMT -4. The time now is 08:20 AM.