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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-18-2005
Darek Darek is offline
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,
  #2 (permalink)  
Old 08-18-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,767
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
  #3 (permalink)  
Old 08-18-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Another way...
Code:
sed 's/,$/,0/' file1 > file2
  #4 (permalink)  
Old 08-19-2005
Darek Darek is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 2
Thanks a lot...that did the job.
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:12 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