LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2021, 06:15 AM   #1
Chittu1
LQ Newbie
 
Registered: Feb 2021
Posts: 1

Rep: Reputation: Disabled
data shorting many rows into single columns


Hi, inside the refence.tx file the data arrangement are dived into three sections. Each sections contains many rows. I have to arrange all the rows from from all rows from each section into single colums. . Finally it will be three columns from each three sections. My row data in the txt file like below:

-317 -324 -303 -315 -319 -319 01
-310 -326 -331 -322 -327 -307 02
-330 -316 -317 -323 -318 -321 03
-320 -315 -307 -326 -341 -307 04
-323 -327 -333 -330 -303 -304 05
-308 -322
ABH R N118 18 1 18 6 44 40.744 250.00 737 4
-304 -300 -308 -307 -324 -329 -325
-321 -304 -302 -309 -320 -317 -296
-304 -330 -328 -298 -283 -312 -335
-330 -324 -312 -298 -301 -321 -326
-297 -287 -317 -336 -324 -297 -288
-301 -321
ABH R E118 18 1 18 6 44 40.744 250.00 737 4
79 87 117 103 81 92 97
88 96 103 97 99 105 99
94 98 95 90 89 107 99
65 107 143 87 62 99 112
85 74 97 118 115 95 98
97 81

After shorting it should be like below shown :


-317 -304 79
-324 -300 87
-303 -308 117
-315 -307 103
-319 -324 81
-319 -329 92
-01 -325 97
-310 -321 88
-326 -304 96
-331 -302 103
-322 -309 97
-327 -320 99
-307 -317 105
-02 -296 99
-330 -304 94
-316 -330 98
-317 -328 95
-323 -298 90
-318 -283 89
-321 -312 107
-03 -335 99
-320 -330 65
-315 -324 107
-307 -312 143
-326 -298 87
-341 -301 62
-307 -321 99
-04 -326 112
-323 -297 85
-327 -287 74
-333 -317 97
-330 -336 118
-303 -324 115
-304 -297 95
-05 -288 98
-308 -301 97
-322 -321 81

I can arrange the for single section but, for simultaneously for other two section how it can be done. I am using shell script for single section

for num in $(cat reference.tx)
do
echo $num >>3.txt
done


If any someone can help it will be great help to me. Thank you well in advance.
 
Old 02-15-2021, 07:13 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
1) Please edit your post and wrap the code and the data into [code] tags.

2) An easy way to achieve what you want is to split the input data on section boundaries into separate files with csplit, convert each file, then use paste to combine them together.
Code:
#!/bin/sh
tmpdir=$(mktemp -d) || { echo Cannot create temp dir >&2;exit 1;}
trap "rm -fr -- '$tmpdir'" EXIT
fileptn='[0-9][0-9]'

csplit -sf"$tmpdir/" --suppress-matched reference.tx /^A/ '{*}'
sed -Ei 's/\s+//;s//\n/g' "$tmpdir"/$fileptn
paste "$tmpdir"/$fileptn
Provided there are always exactly three sections of the same length, another option would be
Code:
awk '/^[ \t]/{for(i=1;i<=NF;i++)print$i}' reference.tx|pr -3JT
More idiomatic awk doing the same
Code:
awk -vOFS=\\n '!/^A/,$1=$1' reference.tx|pr -3JT
Or replace awk with sed if you prefer
Code:
sed '/^A/d;s/\s\+//;s//\n/g' reference.tx|pr -3JT

Last edited by shruggy; 02-15-2021 at 05:32 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 10:31 AM
[SOLVED] How to convert the rows of data into the columns? w1k0 Programming 101 02-06-2014 09:24 PM
Convert columns of data into rows jaufer Linux - Software 3 06-29-2010 05:19 PM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM
text data conversion: rows into columns frankie_DJ Programming 6 06-03-2006 06:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration