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!

Putting "," after every item, except the last one

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
DK
Hi

I'm using this to get the destinct numbers in a list. this is a list from a logfile that I have to use in a SQL, so I have to put a , after every entery except the last one.

cat logfil.log|awk -F ";" '{print $1}'|grep -v QA |grep -v BAN|awk -F- '{++a[$1]}END{for(i in a)print i,a}'|awk '{print $1}'


It is easy to put it after every item '{print $1 ","}'
But I can't have it after the last one or the SQL will not work.

Does anyboddy have any ideers?

/Lhg
 
Brute force method:
... | awk '{print $1 ","}' | sed 's/,$//'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Something along the lines of
Code:
...|awk -F- 'NR == 1 { printf $1 } {printf "," $1}'


On the internet no one knows you're a dog

Columb Healy
 
what about this ?
awk -F';' '$1!~/(QA|BAN)/{print $1}' logfil.log | awk -F- '{++a[$1]}END{c="";for(i in a){print c i;c=","}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Of course, it's a piece of cake in perl
Code:
perl -e 'my @bits; # Array to store output
while (<>)
  {
  my $short = +(split /;/)[0]; #Take only the bit before the ';'
  $short =~ /QA|BAN/ and next; #Reject lines with QA or BAN
  push @bits, +(split /-/, $short); #Add the first bit up to the '-' to the array
  }
#Print the array joined by commas
print (join (",", @bits));' logfil.log


On the internet no one knows you're a dog

Columb Healy
 
Sorry for the typo:
awk -F';' '$1!~/(QA|BAN)/{print $1}' logfil.log | awk -F- '{++a[$1]}END{c="";for(i in a){print c i;c=","}}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If you use dummy-columns ('dummy') and dummy-where-conditions (1=1) you may easily add more columns (even zero) and conditions (even none).
Code:
select 'dummy'
( , nr)
( , datum)
( , stand)
from foo 
where 1=1
(and nr > 7)
(and datum=today)
(and stand >= 5000);
How to apply to your case is left as an excercise to the reader. :)

don't visit my homepage:
 
(stdin consisting of a list of numbers) | xargs echo | tr " " ",
 
Could you do it in 2 steps?

Step 1, put comma after every item.
Step 2, delete the last comma.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top