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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping lines 3

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
Hi,

Could someone suggest a solution?

Basically I want to take each line in a file, compress multiple spaces to single space in each line and write them to outfile. I have this,

var=`cat infile`
var=$(echo "$var" | sed 's!! !g')
echo "$var\n" >> $outfile

Thanks
 
Have you tried this ?
sed 's! *! !g' infile > outfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
TheDash,

Code:
#!/usr/bin/ksh

>outfile
while read line
do sed 's/  */  /g' $file >> outfile
#sed 's/(2 spaces)*/(1 space)/g' $file >> outfile
done<infile

Hope this helps.

John
 
PHV, many thanks for the answer. I am also having another problem.

Code:
300080154  06/18/2004  14:35:56  LINK  CHANGE  Detected
500080149  06/18/2004  15:49:56  LINE  CHANGE  Detected
700080151  06/21/2004  09:06:45  POINT  CHANGE  Detected
200080154  06/21/2004  09:49:00  LINK  CHANGE  Detected
700080154  06/21/2004  10:41:08  POINT  CHANGE  Detected

Could it be formatted as below? Thanks again.

Code:
300080154  06/18/2004  14:35:56  LINK  CHANGE  Detected
500080149  06/18/2004  15:49:56  LINE  CHANGE  Detected
700080151  06/21/2004  09:06:45  POINT CHANGE  Detected
200080154  06/21/2004  09:49:00  LINK  CHANGE  Detected
700080154  06/21/2004  10:41:08  POINT CHANGE  Detected
 
man awk
take a look at the printf function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
don't know if this is correct but it seems to work o.k.

Code:
awk '{ print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 }' filetoawk.txt


Kind Regards
Duncan
 
Thanks,

Does it have syntax? Find out the max length of $previous from all lines, start $next for all lines after a space?
 
in Perl...

Code:
#!/usr/bin/perl

while (<DATA>) {
      
  ($num, $date, $time, $LLP, $change, $detected) = split (/\s+/, $_);
  
  print "$num  $date  $time  $LLP";
  print " " x (7 - (length $LLP));
  print "$change  $detected\n";

}

__DATA__
300080154  06/18/2004  14:35:56  LINK  CHANGE  Detected
500080149  06/18/2004  15:49:56  LINE  CHANGE  Detected
700080151  06/21/2004  09:06:45  POINT  CHANGE  Detected
200080154  06/21/2004  09:49:00  LINK  CHANGE  Detected
700080154  06/21/2004  10:41:08  POINT  CHANGE  Detected

outputs:-

Code:
[red]300080154  06/18/2004  14:35:56  LINK   CHANGE  Detected
500080149  06/18/2004  15:49:56  LINE   CHANGE  Detected
700080151  06/21/2004  09:06:45  POINT  CHANGE  Detected
200080154  06/21/2004  09:49:00  LINK   CHANGE  Detected
700080154  06/21/2004  10:41:08  POINT  CHANGE  Detected[/red]


Kind Regards
Duncan
 
Hi,

This is what I finaly got and it works

Code:
MAX=`awk 'BEGIN{max=0} {{len=length($4)} if(len > max) max=len} END{print max}' infile`

cat infile | head -20 | sed 's!  *!  !g' | awk '{printf("%0s %0s %0s %"'"$MAX"'"s %0s\n", $1, $2, $3, $4, substr($0,index($0,$5)))}' >> outfile

Here $4 is aligned to right, so $5 .... will be aligned straight
 
thedash
you are not alone
a lot of people do still NOT realize
head,sed,awk,grep ..... are able to open files
so (try it) ignore this stupid CAT
in your script:

MAX=`awk 'BEGIN{max=0} {{len=length($4)} if(len > max) max=len} END{print max}' infile`

cat infile | head -20 | sed 's! *! !g' | awk '{printf("%0s %0s %0s %"'"$MAX"'"s %0s\n", $1, $2, $3, $4, substr($0,index($0,$5)))}' >> outfile

--- ok it's maybe just cosmetics,
--- but show, you understand it

awk does NOT need: BEGIN{max=0}
awk kindly initializes all vars to NULL
cat|head|sed|awk ... is this not TOO much?
awk can all this in ONE step
forget the sed, awk can split
forget the head, awk knows NR
forget the cat, awk can read 'infile'
what's that: %0s (fortunately, awk can handle it)

don't forget, RTFMP :) guggach
 
Guggach,

1) Thanks for the input. I could improve it only to this:

Code:
cat infile | sed 's!  *!  !g' | awk 'NR<21{printf("%0s %0s %0s %-"'"$MAX"'"s %0s\n", $1, $2, $3, $4, substr($0,index($0,$5)))}' >> outfile

I would appreciate if you can tell how to remove "cat" and "sed" from above.


2) Next, I am trying to print 4 columns as shown above

Code:
TOTALCOUNT=$(wc $TMP | awk '{print $1}')
   MAX=`awk 'BEGIN{max=0} {{len=length($4)} if(len > max) max=len} END{print max}' $TMP`
   spaces=""
   i=0
   while [ $i -le ${MAX}-9 ]
   do
      spaces="$spaces "
      i=`expr $i + 1`
   done

The MAX value is used to put enough spaces for alignment.

Code:
300080154  06/18/2004  14:35:56  LINK   CHANGE  Detected
500080149  06/18/2004  15:49:56  LINE   CHANGES  Detected
700080151  06/21/2004  09:06:45  POINT  CHANGE  Detected
200080154  06/21/2004  09:49:00  LINK   CHANGES  Detected
700080154  06/21/2004  10:41:08  POINT  CHANGE  Detected

I had to format another column and this is making things complicated. Let's say I want to do it for another column, could we do it better way? I can hardcode it but later if a new column has to be printed, the problem comes up again.
Any suggestions will be appreciated.
 
my 1.99 ¢:
replace multiple spaces with a tab.
Code:
sed 's/  */\t/g' DATA
(instead of:)
cat DATA | sed 's/ */ /g'

cat FILE | sed PATTERN
can be condensed to:
sed PATTERN FILE

I like sed, but don't like awk.
Don't know why.

seeking a job as java-programmer in Berlin:
 
sed 's/ */\t/g' DATA


How come \t is not printing tab? It is printing letter t
I also tried \\t and '\t'
 
TheDash,

You actually have to type a tab.

So it is... sed 's/ */ /g' filename

's/(two spaces)*/(tab)/g'

Hope this helps.

John
 
TheDash,

You actually have to type a tab (as far as I know).

So it is... sed 's/ */ /g' filename

's/(two spaces)*/(tab)/g'

Hope this helps.

John
 
This should work for any number of columns...
Code:
awk '{
   for (col=1; col<=NF; col++) {
      if (max[col] < length($col))
          max[col] = length($col)
      arr[NR, col] = $col
   }
}
END {
   for (row=1; row<=NR; row++) {
      for (col=1; col<=NF; col++)
         printf "%-" max[col] "s  ", arr[row,col]
      print ""
   }
}' file1
Tested...
[tt]
300080154 06/18/2004 14:35:56 LINK CHANGE Detected
500080149 06/18/2004 15:49:56 LINE CHANGES Detected
700080151 06/21/2004 09:06:45 POINT CHANGE Detected
200080154 06/21/2004 09:49:00 LINK CHANGES Detected
700080154 06/21/2004 10:41:08 POINT CHANGE Detected[/tt]
 
Ygor,

Thanks for the elegant solution. I tested it. it works.
However, my data is like this

Code:
0000000083942  07/07/2004 08:17:00 aaaaaaaaaa  2393286 ORA-02290: check constraint violated
0008015669997  07/07/2004 14:21:28 aaaaaaaaaaaaaaaaaaaaaaaaaaa 43434343  Primary Violation 
0000000149033  07/07/2004 14:21:28 aaaaaaaaaaaaaaaaa  2933101 ORA-00001: unique constraint violated
0008015696891  07/08/2004 08:23:40 aaaaaaaaaaa  3093754 Primary Violation

It gives:

Code:
0000000083942  07/07/2004  08:17:00  aaaaaaaaaa                   2393286   ORA-02290:  check      
0008015669997  07/07/2004  14:21:28  aaaaaaaaaaaaaaaaaaaaaaaaaaa  43434343  Primary     Violation  
0000000149033  07/07/2004  14:21:28  aaaaaaaaaaaaaaaaa            2933101   ORA-00001:  unique     
0008015696891  07/08/2004  08:23:40  aaaaaaaaaaa                  3093754   Primary     Violation


whereas,

this gives it correctly (unlike my data)

Code:
0000000083942  07/07/2004 08:17:00 aaaaaaaaaa  2393286 ORA-02290: check constraint violated
0008015669997  07/07/2004 14:21:28 aaaaaaaaaaaaaaaaaaaaaaaaaaa 43434343  Primary Violation constraint violated
0000000149033  07/07/2004 14:21:28 aaaaaaaaaaaaaaaaa  2933101 ORA-00001: unique constraint violated
0008015696891  07/08/2004 08:23:40 aaaaaaaaaaa  3093754 Primary Violation constraint violated

Code:
0000000083942  07/07/2004  08:17:00  aaaaaaaaaa                   2393286   ORA-02290:  check      constraint  violated  
0008015669997  07/07/2004  14:21:28  aaaaaaaaaaaaaaaaaaaaaaaaaaa  43434343  Primary     Violation  constraint  violated  
0000000149033  07/07/2004  14:21:28  aaaaaaaaaaaaaaaaa            2933101   ORA-00001:  unique     constraint  violated  
0008015696891  07/08/2004  08:23:40  aaaaaaaaaaa                  3093754   Primary     Violation  constraint  violated

 
sed 's/ */\t/g' DATA

How come \t is not printing tab? It is printing letter t
I also tried \\t and '\t'
Normally it doesn't work for me too, inserting the two characters '\t' for sed. I don't know which change made it work yesterday. (hey - it's still working!).

Inserting a tab on the commandline normally doesn't work, because of bash-commandcompletition.
Then you may write to a file 'blank2tab.sed':
Code:
s/  */\t/g
[code]
(replace the \t with a real tab).
and call:
[code]
sed -f blank2tab.sed DATA > DATA.out
[code]
An alternative is, to convert multi-blanks to one, and use 'tr' afterwards:
[code]
sed 's/  */ /g' DATA | tr " " "\t"
[code]

seeking a job as java-programmer in Berlin: [URL unfurl="true"]http://home.arcor.de/hirnstrom/bewerbung[/URL]
 
sorry - bad formatting
sed 's/ */\t/g' DATA
How come \t is not printing tab? It is printing letter t
I also tried \\t and '\t'

Normally it doesn't work for me too, inserting the two characters '\t' for sed. I don't know which change made it work yesterday. (hey - it's still working!).

Inserting a tab on the commandline normally doesn't work, because of bash-commandcompletition.
Then you may write to a file 'blank2tab.sed':
Code:
s/  */\t/g
(replace the \t with a real tab).
and call:
Code:
sed -f blank2tab.sed DATA > DATA.out
An alternative is, to convert multi-blanks to one, and use 'tr' afterwards:
Code:
sed 's/  */ /g' DATA | tr " " "\t"

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top