Column to row and position data in a text file

Hi everyone..

I have a list of values in a file...

a,
b,
c,
1,
2,
3,
aaaa,
bbbbb,

I am interested in converting this column to a row..

"text",aaaa,
bbbb
a,1
b,2
c,3

can anyone pls help?? I am a newbie..

Thanks..

Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Being a newbie, you might want to learn to "write" a decent specification (on top of the remarks here above) with due care, avoiding or, resp., answering questions before they arise. Here:

  • you seem to need more than a single line, not - as you write - a row.
  • in your desired output, there's a "text" - where does that come from?
  • how and why (by which rules) have 5 "b"s been converted to 4 "b"s?
  • is there any underlying structure to be obeyed - I can't see why lines 7 and 8 are being appended to a "text" or nothing and transferred to the start of file, then lines 4 - 6 being appended to lines 1 - 3?

Why should anyone care more for your request than you do?

You are right, i try to integrate with more info. I would like to use awk or sed to sort the contents of a text file from row to column. I would like to convert:

a (text value),
b (text value),
c (text value),
1 (numeric value),
2 (numeric value),
3 (numeric value),
xxxxx (text value), yyyyy (text value)

in

"Zzzz", xxxxx, yyyyy
a, 1
b, 2
c, 3

Now I used

awk '{printf "% s% s", $ 0, NR% 4? "": "\ n"; } 'infile

but the result is:

a, b, c
1,2,3
xxxxx, yyyyy

Thanks

Again, there are some bits missing, and both input (I understand the explicatory text, but the space remains) and desired output are different from post#1.

  • Still, you didn't answer where the "Zzzz" comes from, and this can't be derived from your code. Is it a string constant?
  • Is that file ALWAYS 7 lines, and the "rules" would be "bring line 7 to the top, adding a constant, then print lines 1 and 4, 2 & 5, and 3 & 6, each separated by comma and a space, but the trailing comma needs to be removed"?
  • Do we need to check for text or numeric?

"ZZZZ" is a fixed text to insert
the file is always 7 lines and the rule is to move line 7 to the top and then print lines 1 and 4, 2 and 5 and 3 and 6 comma-separated without space. No text or numeric controls are required

RudiC thanks for your time

Try

awk '
NR < 4  {T[NR] = $0
         next
        }

NR < 7  {sub (/,$/, _)
         T[NR-3] = T[NR-3] FS $0
         next
        }

        {print "\"Zzzz\",", $0
        }

END     {for (i=1; i<=3; i++) print T
        }
' file

Thank you! It works!

Just another idea for fun...

set -- $( xargs -n2 <yourfile | sed 's/ .*//' | xargs )
echo -e "$7,$8,ZZZZZZ\n$1,$4\n$2,$5\n$3,$6\n"

(to be adapted )