|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 ;-) |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 ;-) |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Thnaks a lot everybody ! you people are super ! |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
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( "," ) |
|
![]() |
| Outils de la discussion | |
|
|