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
Replacing a string in nth line maxmave Shell Programming and Scripting 1 06-04-2008 02:32 PM
need help with replacing a certain field... shennanigan83 Shell Programming and Scripting 5 04-08-2008 10:00 PM
replacing a nul field with text DarkHound Shell Programming and Scripting 3 09-04-2007 11:34 AM
Replacing more than 1 pattern in a line Manan Shell Programming and Scripting 6 12-27-2006 10:58 PM
Replacing certain field charbel Shell Programming and Scripting 1 04-26-2006 01:00 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-18-2005
Registered User
 

Join Date: Aug 2005
Posts: 2
Replacing the last field of a line.

Hi,
I wrote a script which extracts data from 2 tables (joining the tables together) and outputs the fields to a csv file.

the output may look something like

scenario 1: a,b,c,d,1,2,3,4 or
scenario 2: a,b,c,d,,,,

now, in the second scenario, there are some empty fields at the end of the line. Basically what I need to do is insert a "0" in the last field if it comes back empty after the database extraction, and i'm not sure how I can do this.

Any help?

thanks,
Reply With Quote
Forum Sponsor
  #2  
Old 08-18-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
One way:
file:
Code:
a,b,c,d,,,,
a,b,c,d,1,2,3,4
output
Code:
 
a,b,c,d,,,,0
a,b,c,d,1,2,3,4
code
Code:
awk ' {
    if(substr($0,length($0))==",")
    {   print $0 "0"	}
    else
    {   print $0 }
    }'  filename
Reply With Quote
  #3  
Old 08-18-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Another way...
Code:
sed 's/,$/,0/' file1 > file2
Reply With Quote
  #4  
Old 08-19-2005
Registered User
 

Join Date: Aug 2005
Posts: 2
Thanks a lot...that did the job.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:55 AM.


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

Content Relevant URLs by vBSEO 3.2.0