| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

09-03-04, 10:00
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 46
|
|
|
Trim Function ??
|
|
Hi !
Is there any cmd to trim the leading and trailing spaces in fields in a file seperated with commas
if i have
1, 3 , 45 , ert , mj
then output should be
1,3,45,ert,mj
thanks
Mark.
|
|

09-03-04, 10:33
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
nawk -f mark.awk myFile.txt
here's mark.awk:
Code:
BEGIN {
FS=OFS=","
}
function trim(str)
{
sub("^[ ]*", "", str);
sub("[ ]*$", "", str);
return str
}
{
for(i=1; i <= NF; i++)
printf("%s%s", trim($i), (i==NF) ? "\n" : OFS);
}
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
|
|

09-03-04, 10:43
|
|
Registered User
|
|
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
|
|
|
|
manualy you can do it like this:
ex file.txt <<EOF
%s/ ,/,/g <<EOF
wq <<EOF
Or inside of a script you can use sed like this:
sed -e '%s/ ,/,/g' file.txt >file.tmp
mv file.tmp file.txt
bye bye,
ducasio 
|
|

09-03-04, 11:10
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
or
Code:
sed -e 's/[ ][ ]*,/,/g;s/,[ ][ ]*/,/g' myFile.txt
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
|
|

09-03-04, 12:26
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 46
|
|
Thanks,
but for in the sed command, if my input contains strings as below then it won't work
1 , ' jason ', 3
output
1,'jason',3
i can do this with awk , but with the sed cmd is there any way to do the above
Thanks again for your answers
thanks
Mark
|
|

09-03-04, 13:17
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
I think if you try my sed solution, it should give you what I [bold]think[/bold] you want.
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|