PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > need a one liner command in unix
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

need a one liner command in unix

Réponse
 
LinkBack Outils de la discussion
Vieux 19/07/2007, 20h23   #1
romy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut need a one liner command in unix

Hello,

I have a file which contains data in this format :

1
2
3
4
5

I need a one line command in sed or awk to read the input file and
produce output in this format '1','2','3','4','5'

Thanks.

  Réponse avec citation
Vieux 19/07/2007, 21h09   #2
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

romy wrote:
> Hello,
>
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'
>
> Thanks.
>


awk '{printf S Q "%s" Q,$0; S=","} Q=\'

and if you mind the missing EOL:

awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 19/07/2007, 22h00   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

Michael Tosch wrote:
> romy wrote:
>> Hello,
>>
>> I have a file which contains data in this format :
>>
>> 1
>> 2
>> 3
>> 4
>> 5
>>
>> I need a one line command in sed or awk to read the input file and
>> produce output in this format '1','2','3','4','5'
>>
>> Thanks.
>>

>
> awk '{printf S Q "%s" Q,$0; S=","} Q=\'
>
> and if you mind the missing EOL:
>
> awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'
>


or with sed:

sed -n 's/^/'\''/;s/$/'\''/;1h;2,$H;$g;$s/\n/,/g;$p'


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 19/07/2007, 22h00   #4
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

Michael Tosch wrote:
> romy wrote:
>> Hello,
>>
>> I have a file which contains data in this format :
>>
>> 1
>> 2
>> 3
>> 4
>> 5
>>
>> I need a one line command in sed or awk to read the input file and
>> produce output in this format '1','2','3','4','5'
>>
>> Thanks.
>>

>
> awk '{printf S Q "%s" Q,$0; S=","} Q=\'
>
> and if you mind the missing EOL:
>
> awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'
>


or with sed:

sed -n 's/^/'\''/;s/$/'\''/;1h;2,$H;$g;$s/\n/,/g;$p'


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 19/07/2007, 22h17   #5
loki harfagr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

On Thu, 19 Jul 2007 19:23:58 +0000, romy wrote:

> Hello,
>
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'


A simple track in sed

$ seq 5 | sed "s/^\(.*\)/\'\1\'/"
'1'
'2'
'3'
'4'
'5'

same with awk as an additional filter

$ seq 5 | sed "s/^\(.*\)/\'\1\'/" | awk '1' ORS=,
'1','2','3','4','5',

now, you know how to fly, just make room and make room ;-)
  Réponse avec citation
Vieux 19/07/2007, 22h17   #6
loki harfagr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

On Thu, 19 Jul 2007 19:23:58 +0000, romy wrote:

> Hello,
>
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'


A simple track in sed

$ seq 5 | sed "s/^\(.*\)/\'\1\'/"
'1'
'2'
'3'
'4'
'5'

same with awk as an additional filter

$ seq 5 | sed "s/^\(.*\)/\'\1\'/" | awk '1' ORS=,
'1','2','3','4','5',

now, you know how to fly, just make room and make room ;-)
  Réponse avec citation
Vieux 20/07/2007, 18h28   #7
romy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix



Thnaks a lot everybody ! you people are super !

  Réponse avec citation
Vieux 20/07/2007, 23h51   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

Michael Tosch wrote:
> Michael Tosch wrote:
>
>> romy wrote:
>>
>>> Hello,
>>>
>>> I have a file which contains data in this format :
>>>
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>>
>>> I need a one line command in sed or awk to read the input file and
>>> produce output in this format '1','2','3','4','5'
>>>
>>> Thanks.
>>>

>>
>> awk '{printf S Q "%s" Q,$0; S=","} Q=\'
>>
>> and if you mind the missing EOL:
>>
>> awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'
>>


That would print the final record from the file after all the desired
output. ITYM:

awk '{printf S Q "%s" Q,$0; S=","} END {print ""}' Q=\'

I'd probably just go with this though:

awk '{o=o S Q $0 Q; S=","} END {print o}' Q=\'

There's also a "cute" awk solution:

awk -v q=\' 'BEGIN{RS="";ORS=q"\n";FS="\n";OFS=q","q}$1=q$1'

but that's more of a comprehension exercise.....

Ed.
  Réponse avec citation
Vieux 21/07/2007, 00h18   #9
johngnub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

On Jul 19, 12:23 pm, romy <rav...@gmail.com> wrote:
> Hello,
>
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'
>
> Thanks.


Just my 2 cents:
Given a file,
more one
1
2
3
4
5
Now to change the CR to a comma,
grep . one |tr '\012' ','
1,2,3,4,5,
But that is a little messy and lacking control,
I bit more control, if we put the info to a var,
var=$(grep . one |tr '\012' ',')
then we print the var when we like or even use printf ,

echo "$var"
1,2,3,4,5,
printf "DATA:: %s\n" $var

Just for fun,,,, JB



  Réponse avec citation
Vieux 21/07/2007, 15h57   #10
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

Ed Morton wrote:
> Michael Tosch wrote:
>> Michael Tosch wrote:
>>
>>> romy wrote:
>>>
>>>> Hello,
>>>>
>>>> I have a file which contains data in this format :
>>>>
>>>> 1
>>>> 2
>>>> 3
>>>> 4
>>>> 5
>>>>
>>>> I need a one line command in sed or awk to read the input file and
>>>> produce output in this format '1','2','3','4','5'
>>>>
>>>> Thanks.
>>>>
>>>
>>> awk '{printf S Q "%s" Q,$0; S=","} Q=\'
>>>
>>> and if you mind the missing EOL:
>>>
>>> awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'
>>>

>
> That would print the final record from the file after all the desired
> output. ITYM:
>
> awk '{printf S Q "%s" Q,$0; S=","} END {print ""}' Q=\'
>


You are right, GNU awk does not clear $0 in END so needs the "".


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 22/07/2007, 17h21   #11
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

On 2007-07-21, Michael Tosch wrote:
> Ed Morton wrote:
>> Michael Tosch wrote:
>>>>
>>>> awk '{printf S Q "%s" Q,$0; S=","} Q=\'
>>>>
>>>> and if you mind the missing EOL:
>>>>
>>>> awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'

>>
>> That would print the final record from the file after all the desired
>> output. ITYM:
>>
>> awk '{printf S Q "%s" Q,$0; S=","} END {print ""}' Q=\'

>
> You are right, GNU awk does not clear $0 in END so needs the "".


The only awk I have found which doesn't retain the value of $0 is
old (pre-1988) awk.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 23/07/2007, 08h41   #12
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

Chris F.A. Johnson wrote:
> On 2007-07-21, Michael Tosch wrote:
>> Ed Morton wrote:
>>> Michael Tosch wrote:
>>>>> awk '{printf S Q "%s" Q,$0; S=","} Q=\'
>>>>>
>>>>> and if you mind the missing EOL:
>>>>>
>>>>> awk '{printf S Q "%s" Q,$0; S=","} END {print}' Q=\'
>>> That would print the final record from the file after all the desired
>>> output. ITYM:
>>>
>>> awk '{printf S Q "%s" Q,$0; S=","} END {print ""}' Q=\'

>> You are right, GNU awk does not clear $0 in END so needs the "".

>
> The only awk I have found which doesn't retain the value of $0 is
> old (pre-1988) awk.
>


and HP-UX /usr/bin/awk
and Solaris /usr/xpg4/bin/awk.


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 24/07/2007, 19h29   #13
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

2007-07-19, 19:23(-00), romy:
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'


paste -d\' /dev/null file /dev/null | paste -sd, -

--
Stéphane
  Réponse avec citation
Vieux 24/07/2007, 19h29   #14
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

2007-07-19, 19:23(-00), romy:
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'


paste -d\' /dev/null file /dev/null | paste -sd, -

--
Stéphane
  Réponse avec citation
Vieux 25/07/2007, 02h51   #15
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

In article <slrnfach8k.676.stephane.chazelas@spam.is.invalid> ,
Stephane CHAZELAS <this.address@is.invalid> wrote:
>2007-07-19, 19:23(-00), romy:
>> I have a file which contains data in this format :
>>
>> 1
>> 2
>> 3
>> 4
>> 5
>>
>> I need a one line command in sed or awk to read the input file and
>> produce output in this format '1','2','3','4','5'

>
>paste -d\' /dev/null file /dev/null | paste -sd, -
>
>--
>Stéphane


What part of "in sed or awk" is causing you pain?

  Réponse avec citation
Vieux 25/07/2007, 03h38   #16
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

On 2007-07-25, Kenny McCormack wrote:
> In article <slrnfach8k.676.stephane.chazelas@spam.is.invalid> ,
> Stephane CHAZELAS <this.address@is.invalid> wrote:
>>2007-07-19, 19:23(-00), romy:
>>> I have a file which contains data in this format :
>>>
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>>
>>> I need a one line command in sed or awk to read the input file and
>>> produce output in this format '1','2','3','4','5'

>>
>>paste -d\' /dev/null file /dev/null | paste -sd, -

>
> What part of "in sed or awk" is causing you pain?


The pain of seeing a hammer driving a screw.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 25/07/2007, 13h27   #17
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix

Kenny McCormack wrote:
> In article <slrnfach8k.676.stephane.chazelas@spam.is.invalid> ,
> Stephane CHAZELAS <this.address@is.invalid> wrote:
>> 2007-07-19, 19:23(-00), romy:
>>> I have a file which contains data in this format :
>>>
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>>
>>> I need a one line command in sed or awk to read the input file and
>>> produce output in this format '1','2','3','4','5'

>> paste -d\' /dev/null file /dev/null | paste -sd, -
>>
>> --
>> Stéphane

>
> What part of "in sed or awk" is causing you pain?
>

The solution is excellent.

Kenny, do you want to talk about YOUR problem?

--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 28/07/2007, 22h50   #18
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: need a one liner command in unix


romy wrote:
> Hello,
>
> I have a file which contains data in this format :
>
> 1
> 2
> 3
> 4
> 5
>
> I need a one line command in sed or awk to read the input file and
> produce output in this format '1','2','3','4','5'


#!ruby
puts gets(nil).split.map{|x| "'#{x}'" }.join( "," )

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19793 seconds with 26 queries