Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Adding fields to a file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-18-2012
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 1 September 2015, 7:26 AM EDT
Location: Zürich
Posts: 8,575
Thanks: 337
Thanked 1,080 Times in 938 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 Unix and Linux 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 Unix and Linux 12-18-2012
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 1 September 2015, 7:26 AM EDT
Location: Zürich
Posts: 8,575
Thanks: 337
Thanked 1,080 Times in 938 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 Unix and Linux 12-18-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2015, 10:14 PM EDT
Location: Amsterdam
Posts: 10,432
Thanks: 359
Thanked 2,769 Times in 2,475 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10:25 PM.