Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Data in file:

1234567-5678907
3456789-1234563
3456789-1234567
.
.
.
.
n

Now I want the output in the below format:

1234567-5678907,3456789-1234563,3456789-1234563....n

I have used below command but I am getting an extra comma in starting:

awk -F"-" ' {printf ",%s-%s",$1,$1} END{print""}'

Please share the command that can be used in a shell script.

share|improve this question
    
If you don't need to use awk: paste -s -d, file –  glenn jackman 21 hours ago
    
With printf: awk 'NR==1{printf "%s", $0};NR>1{printf ",%s", $0};END{print ""}' –  don_crissti 20 hours ago

2 Answers 2

Using Awk's output record separator, ORS:

awk 'BEGIN{ORS=",";}1' infile
share|improve this answer
2  
Close, but not quite. This awk solution is lacking a final line terminator, and instead has a spurious ,. (See Glenn's solution that is clean in those respects.) –  Janis 21 hours ago
    
@Janis Spurious? I prefer to think of it as vestigial... :) –  jasonwryan 21 hours ago

There's already a solution based on paste in the comment above. But if you prefer awk, here's an awk-solution:

awk 'BEGIN{RS="";OFS=","};$1=$1'

It doesn't create a spurious comma at the end and properly terminates the output by a newline.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.