How to remove spaces in the file?

hiii i have a file that contains spaces in the begining of a file till the middle the from there the txt would appear. hw can i remove those spaces and bring the text to the begining portion

file1

text starts from here

Pls show your tries to achieve ..

sed 's/ //g' 1.txt >2.txt

sed 's/^ //' filename > outputfile

hii Naresh thankyou for the reply but its not working though.

$ nawk '{$1=$1};1' infile

Anurupa,

tell me clearly, u want to remove spaces on starting of each line or a line having spaces(empty lines in a file)

hii jayan
this is the reply i am getting wen i run ur command

nawk '{$1=$1};1' myfile
-bash: $: command not found

Use awk (or) gawk command instead .. If not getting expected output, reply with what OS ..

i want to remove empty lines in the file

---------- Post updated at 01:55 AM ---------- Previous update was at 01:53 AM ----------

i am using Mac os tried with awk as well as gawk. but still returning the same error

here is the code to remove empty lines

grep -v ^$ inputfile >outputfile

or

sed 's/^$//g' inputfile > outputfile

or

awk -NF inputfile > outputfile

 
grep . filename.txt > output.txt

Note : grep followed by dot

grep ( dot )

---------- Post updated at 01:42 PM ---------- Previous update was at 01:41 PM ----------

 
perl -lane 'print $_ if (length($_)>0)' input.txt > output.txt

---------- Post updated at 01:45 PM ---------- Previous update was at 01:42 PM ----------

i blogged some more ways -- two years back.. worth to give a try

Remove empty lines from file | The Linux Tips

1) if it's all spaces :

cat file1 | sed "s/^ *//"

2) no if it's "empty lines" :

tr -s "\n" < file1

HTH

good luck, and success !
alexandre botao
(progsmith, polymath, ideator)