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!

how to remove blank spaces of a file with awk?? 3

Status
Not open for further replies.

pabloli150

Technical User
Feb 26, 2009
1
hello

i´m trying

awk '{gsub(" ","",$0); print $0;}' filename.txt

but it answers

syntax error near line

first of all i did this for download from netbackup database jobs

privilege bpdbjobs -report -M sv88 -gdm -header |cut -c-1024 |awk -F, '{print $1, $4, $5, $7, $8, $11}' > filename.txt
 
If you just want to remove all spaces, this will do...
Code:
sed 's/  *//g' filename.txt


 

I'd take Sam's recommendation and use his sed solution.

The awk solution you posted is correct if you are using a POSIX compliant version of awk.

If you are using solaris or sun *nix, try using nawk.
 
Hi

olded said:
The awk solution you posted is correct if you are using a POSIX compliant version of awk.
Could you explain this ? pabloli150's code seem correct and portable to me. Thank you.

Just one thing to mention. Is faster to not use regular expression if not necessary.
Code:
tr -d ' ' < filename.txt

[gray]# or even faster[/gray]

tr -ds ' ' < filename.txt
But with this one indeed you can have problem on some Unix systems.

Feherke.
 
Could you explain this
With old awk you can't modify any $x variable.
 
awk's authors added the internal functions - such as gsub - last so some of the non-compliant versions like Solaris awk lack them.

feherke, I know you like to use gawk which also isn't really compliant; it's a superset. gawk has the -W switch which disables the GNU extensions. It's documented here:

 
Hi

olded said:
feherke, I know you like to use gawk which also isn't really compliant; it's a superset.
You are right, I like [tt]gawk[/tt]. And yes, it is heavily extended. So to get a different view I always test the code I post or discuss about with [tt]mawk[/tt] too. And occasionally with [tt]awk95[/tt]. ( [tt]mawk[/tt] has less extensions. Not sure about [tt]awk95[/tt], but I think it has none. )

Of course I tried pabloli150's code with all three implementations before my reply at 27 Feb 09 4:27. And it worked correctly.

In both [tt]gawk[/tt] and [tt]mawk[/tt] man pages are sections dedicated to compatibility and extensions. But neither the [tt]$x[/tt] limitation or the [tt]gsub()[/tt] is mentioned.
olded said:
gawk has the -W switch which disables the GNU extensions.
Yes, I know and use the -W posix ( --posix ) and -W traditional ( --traditional ) ( Same as -W compat ( --compat ) in the older version you referred to. ) switches.

Of course I tried pabloli150's code with all three switches before my reply at 27 Feb 09 4:27. And it worked correctly.

Thank you, I will keep this too in mind.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top