Home > awk, bash > Quick Note – convert 1 to 4 as 1,2,3,4

Quick Note – convert 1 to 4 as 1,2,3,4

With GNU bash version 4.1.5

$seq 1 4 | tr '\n' ',' | sed 's/,$/\n/'
1,2,3,4

With awk (mawk version 1.3.3)

$seq 1 4 | awk -f transpose.awk | sed 's/^,//'
1,2,3,4

What transpose.awk looks like.

#!/usr/bin/awk
{
    for(i=1;i<=NF;i++)
      _[i]=sprintf("%s,%s",_[i],$i)
}
END{
    for(i in _)
        print _[i] 
} 

Source:
http://www.unix.com/shell-programming-scripting/193989-transpose-whole-file-specific-columns.html

Advertisements
Categories: awk, bash
  1. March 18, 2013 at 5:24 am

    Hey! Thanks for the follow! By the way, what are you using to format your code?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: