Column to row and position data in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column to row and position data in a text file
# 1  
Old 09-29-2017
Column to row and position data in a text file

Hi everyone..

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

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



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

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

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




Thanks..

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-29-2017 at 04:47 AM.. Reason: Added CODE tags.
# 2  
Old 09-29-2017
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?
# 3  
Old 09-29-2017
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:
Code:
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

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

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

but the result is:
Code:
a, b, c
1,2,3
xxxxx, yyyyy

Thanks

Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-29-2017 at 05:21 AM.. Reason: Added CODE tags.
# 4  
Old 09-29-2017
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?

Last edited by RudiC; 09-29-2017 at 05:37 AM.. Reason: Misread the input file; reduced No. of lines to 7.
# 5  
Old 09-29-2017
"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
# 6  
Old 09-29-2017
Try
Code:
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[i]
        }
' file

# 7  
Old 09-29-2017
Thank you! It works!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print every alternate column in row in a text file

Hi, I have a comma separated file. I would like to print every alternate columns into a new row. Example input file: Name : John, Age : 30, DOB : 30-Oct-2018 Example output: Name,Age,DOB John,30,30-Oct-2018 (3 Replies)
Discussion started by: Lini
3 Replies

2. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

3. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

6. UNIX for Dummies Questions & Answers

How do you delete cells from a space delimited text file given row and column number?

How do you delete cells from a space delimited text file given row and column number? Letś say the row number is r and the column number is c. Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

9. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question