Anand Gnanasekar
Anand Gnanasekar

Reputation: 77

How to change a file format from rows to columns?

file format change : top to bottom <> left to right

input file format:

100
150
200
300
500

output file format should be:

100,150,200,300,500

I need to apply this in reverse, too.

Upvotes: 2

Views: 1303

Answers (2)

Kalanidhi
Kalanidhi

Reputation: 5092

 #!/bin/sh
   i=0
   while read line ; do
        i=`expr $i + 1`
        if [ $i -eq 1 ] ; then
            echo -e "$line\c"
        else
            echo  -e ",$line\c"
        fi
   done  < filename
   echo 

Use this shell script to convert the \n as , The drawback in the tr command is end of the line one comma will be there to overcome that use this script.

Upvotes: 0

Arne Burmeister
Arne Burmeister

Reputation: 20594

Just replace the linefeeds by comma:

$ tr '\n' ',' < input.txt > output.txt

and reverse

$ tr ',' '\n' < input.txt > output.txt

Upvotes: 5

Related Questions