![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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, |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
One way:
file: Code:
a,b,c,d,,,, a,b,c,d,1,2,3,4 Code:
a,b,c,d,,,,0 a,b,c,d,1,2,3,4 Code:
awk ' {
if(substr($0,length($0))==",")
{ print $0 "0" }
else
{ print $0 }
}' filename
|
|
#3
|
||||
|
||||
|
Another way...
Code:
sed 's/,$/,0/' file1 > file2 |
|
#4
|
|||
|
|||
|
Thanks a lot...that did the job.
|
|||
| Google The UNIX and Linux Forums |