Operating System - HP-UX
1833758 Members
2722 Online
110063 Solutions
New Discussion

Re: speces to be deleted from a file : scripted file

 
SOLVED
Go to solution
Abhijit P.
Valued Contributor

speces to be deleted from a file : scripted file


I have one script the output of the script looks like something below in a file now I want to upload this file in database as per my dba but he wants the tabs to be deleted between the column and this is a automated process please gude me what i should use after getting this file to remove the tabs and keep only a single space in columns:
Please make a note that I am new to the scripts

9 UW ETAAXE 20050706
10 UW ETAAXE 20050707
16 UW ETAAXE 20050711
1 UW ETAAXE 20050713
1 UW ETAAXE 20050715
1 UW ETAAXE 20050716
2 UW ETAAXE 20050722
1 UW ETAAXE 20050723
Thanks in advance

Regards,
Abhijit Panse
10 REPLIES 10
Abhijit P.
Valued Contributor

Re: speces to be deleted from a file : scripted file

sorry the file looks like attached one
James R. Ferguson
Acclaimed Contributor

Re: speces to be deleted from a file : scripted file

Hi:

You can do this:

# perl -ple 's/\s+/ /g' /tmp/datafile

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: speces to be deleted from a file : scripted file

You can do it with tr:

cat your_file | tr -s [:space:] " " > new_file

The tr solution is useful because it collapses multiple occurances of Unix "white space" (tabs, spaces or combinations) into one space.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: speces to be deleted from a file : scripted file

Hi (again):

Note too, that the perl solution collapses one or more "whitespace" (space or tab) into a single space.

The \s+ notation refers to one or more whitespace characters.

's/\s+/ /g' means substitute (the first "s") one or more occurances of whitespace with one space. The forward slash characters bound the target of the match and the substitution to make for the match. The 'g' says do this (g)lobally for every occurance you find on a line.

The -ple options essentially tell perl to (p)rint what is processed by (e)xecuting the commandline script following. The input file is named last on command line.

If you wanted to make sure that your filtered file not only eliminated tab characters; reduced spaces between columns to one space; and eliminated any leading or trailing whitespace, do:

# perl -ple 's/^\s+//g;s/\s+$//;s/\s+/ /g' filein > fileout

Regards!

...JRF...
Muthukumar_5
Honored Contributor
Solution

Re: speces to be deleted from a file : scripted file

Try with awk simply as,

# awk '{ for (i=1;i<=NF;i++) { printf $i" " } printf "\n" }' /tmp/datafile
9 UW ETAAXE 20050706
10 UW ETAAXE 20050707
16 UW ETAAXE 20050711
1 UW ETAAXE 20050713
1 UW ETAAXE 20050715
1 UW ETAAXE 20050716
2 UW ETAAXE 20050722
1 UW ETAAXE 20050723
#

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: speces to be deleted from a file : scripted file

Use perl like,

# perl -e ' while(<>){ split; $,=" "; print @_; printf "\n"; }' /tmp/datafile
9 UW ETAAXE 20050706
10 UW ETAAXE 20050707
16 UW ETAAXE 20050711
1 UW ETAAXE 20050713
1 UW ETAAXE 20050715
1 UW ETAAXE 20050716
2 UW ETAAXE 20050722
1 UW ETAAXE 20050723

# If you want to into the same file then,

# perl -i -e ' while(<>){ split; $,=" "; print @_; printf "\n"; }' /tmp/datafile


hth.
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: speces to be deleted from a file : scripted file

Hi Abhijit,

this format your file as requested:
tr -s [:space:] ' 'output_file

HTH,
Art
Fabio Ettore
Honored Contributor

Re: speces to be deleted from a file : scripted file

Hi,

I think all suggestions by perl or tr are useful to obtain the goal....anyway I would use the classic vi commands too:

- open the file by vi and type the following:


:1,$s// /g


has to be substituted by press only one time the TAB space.

HTH.

Best regards,
Fabio


P.S.: Abhijit, please remember to let know us if the suggestions helped you...thanks.
WISH? IMPROVEMENT!
Fabio Ettore
Honored Contributor

Re: speces to be deleted from a file : scripted file

...reading my post I see that it could be not so clear the space:

:1,$s///g

Therefore
is the a normal TAB space
is just a single space

Just to be more exact...

Best regards,
Fabio
WISH? IMPROVEMENT!
Abhijit P.
Valued Contributor

Re: speces to be deleted from a file : scripted file

Dear All

Thanks for your replies exact solution was from muthukumar as I have to do it from the script and AWK I can put it in my original script.

Anyways other solutions are good.

Thanks Regards,
Abhijit Panse