shell / awk doubt

Hi all,

I have a ascii text file like this :

$cat a.txt

abc |12343 |xyakdfj |
x |323 ||
kr1 |13434343|234343 |

Is it possible to to get the file eleminating blank spaces?

output file :

abc|12343|xyakdfj|
x|323||
kr1|13434343|234343|

using korn shell or awk

thanks in advance

Krishna

cat a.txt | sed -e "s/ //g"
cat a.txt | awk 'gsub(" ", "")'

That awk solution, with gsub as a pattern match, will print only lines where gsub changes the line, so you can lose lines.

To retain all your lines:

awk '{gsub(" ","");print}' a.txt

I'm not trying to be a jerk here, but there is not reason to be using "cat" in any of the above example.

Instead of
cat a.txt | sed -e "s/ //g"
you can do:
sed 's/ //g' a.txt
and for this one:
cat a.txt | awk 'gsub(" ", "")'
you can do:
awk 'gsub(" ","")' a.txt

You can always pipe to those commands, but there's no need to use cat to dump a file.

Yep, agree with that (and notice that my suggested correction dropped the cat).

Sure did... :slight_smile:

I can't remember the URL to a page that has various awards for using cat too much... If I find it, I'll post back.

Try this if you don't want to use awk

cat <filename> | tr -d '" "'

Please try this

sed 's/ *//g' file

(Ensure that there are two spaces before * )
This will work to remove all spaces in file.