Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove whitespace from filenames

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
Hi,
I have done a search on previous submissions and found some help on this but my example doesn't quite work.


I want to replace from all filenames in a directory all commas and 1 or more whitespace with a single underscore.
If the file name has a " in it, i want to just remove it.

Here's my awk script so far:

{
if (FNR==1) {
if (NR>1) close(fn)
if (match($0,/[ ,]/)) {gsub(/[ ,]/, "_",$0)
if (match($0,/["]/)) {gsub(/["]/, "",$0) }
}
}



and here's a test of file names:
file1 record 1.dat
file1,record 2.dat
file1 ,"record"3.dat


I want the output to become:
file1_record_1.dat
file1_record_2.dat
file1__record3.dat
 
{
gsub(/([,]+|[ ]+)/, "_");
gsub(/"/, "");
print;
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,
Thanks for help.

I tried this with the following:
{
if (FNR==1) {
if (NR>1) close(fn)
gsub(/([,]+|[ ]+)/, &quot;_&quot;);
gsub(/&quot;/, &quot;&quot;);
print;
}
}

and ran it like gawk -f script *.dat

I got an output of
bbb
aaaaaaaa

It did not substitute the space between filenames in my directory.

Am I incorporating it correctly?
Thanks
 
why do you have this?

> if (FNR==1) {
> if (NR>1) close(fn)

Drop the outter 'if' and try the solution as posted.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top