Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Location
    Schaumburg, IL
    Posts
    79

    Unanswered: Reformatting a file

    Hello,

    I have a file like shown below:

    456
    1
    2
    756
    349

    I want it to be reformatted like this:

    456, 1, 2, 756, 349

    How can I do this in a UNIX shell script? Is it possible in awk? If not, how can I do it in korn shell?
    Naveen Urs
    DBA Manager
    IBM Certified Solutions Expert - DB2 LUW V7, V9

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    sed '2,$s/^/, /' myfile | tr -d '\n'

  3. #3
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    awk '{if (NR==1) {printf $1} else {printf ", " $1}}' myfile

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    awk '{printf x $1; x=", "}' myfile

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Korn shell.

    Code:
    #!/bin/ksh
    separator=""
    while read a
    do
    echo -e "$separator,$a\c"
    separator=", "
    done<myfile
    echo -e "\n"
    Depending on your version of echo, you may or may not need the "-e" option.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •