LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-08-2013, 01:48 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Thumbs up Print on terminal specifying columns and row?


Is this possible?
Lets say I have many txt files with lots of number...
a.txt (each containing numbers by line 1 3423 542 234 524... )
b.txt
c.txt
.
.
.


If I do cat *.txt
it will print the number in a row but I want to use it to print in columns of let's say 5 row.
Is it possible?
 
Old 04-08-2013, 06:36 PM   #2
rigor
Member
 
Registered: Sep 2011
Posts: 241

Rep: Reputation: Disabled
Hi Drigo!

If you are asking how to display on the terminal, for example, just what is in the fifth field in each file, that can be done as follows.


If this is what's in file a.txt:

Code:
1 3423 542 234 524

and this is what's in file b.txt:

Code:
2 1728 409 637 031

then this command:

Code:
cut -d' ' -f5-5 a.txt b.txt

will produce this output:

Code:
524
031
If you meant something else, perhaps you can show us visually, just what you want the output to look like.

If you look at the bottom of the area where you enter text into your comment, there's a button marked "Go Advanced". If you press that button, you'll see additional controls are added above the area where you enter text. If you press the control labeled "#", it will place "CODE" tags in the text area. If you paste your example between the code tags, the formatting will be maintained so we can see the structure of exactly what you want.

Hope this helps.
 
Old 04-08-2013, 07:12 PM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
The ncurses library allows you to move the cursor about and print text at selected places on the screen. You can do that from C (and I did some once).
Or I think http://en.wikipedia.org/wiki/Tcl is used for this as a higher level language (I have not done this).

Otherwise you'd be reading in all your files' content and writing it in your preferred order (maybe by filling a 2-d array).
 
Old 04-09-2013, 02:55 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
It's not very clear so you want.

if you want to print the same column for each file (for example, column num 5)

where aaa.txt bbb.txt are so:

1 324 3453 3 34
21 34 2 3 44

Code:
awk '{print $5}' aaa.txt bbb.txt
it will print:

34
44
 
Old 04-11-2013, 04:14 PM   #5
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
let say I have a text file with these numbers:

AllNumbers.txt:
923
432
824
267
523
234
423
452
778
758
695
896




I want something like:
$run_script col=3 rows=4
and get an output file with tab delimiters like:

923 523 778
432 234 758
824 423 695
267 452 896

so I can copy it to excel. I have obviously not only 12 numbers but 2k of them....and I want a 58x20 matrix instead of a 2k x 1
 
Old 04-11-2013, 08:17 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,372

Rep: Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382Reputation: 2382
Here's a basic program.
Note that I'm using csv, because that's more usual for import/export of spreadsheets.
Also, if you got a list of nums like that, specifying the num of output cols is sufficient. The num of rows would be dictated by how many nums you've got, unless you only want a subset of the input.
Code:
#!/bin/bash
#set -xv

cnt=0
col=$1
for num in $(cat t.t)
do
    ((cnt++))
    if (( $cnt == 1 ))
    then
        out=$num
    else
        out=${out},${num}
        # tabs : out=${out}'\t'${num}
    fi

    if (( $cnt == $col ))
    then
        # tabs : echo -e $out
        echo $out
        cnt=0
    fi
done

# leftovers, if any
if (( $cnt != 0 ))
then
    # tabs : echo -e $out
    echo $out
fi

# example runs
./t.sh 2
1,2
3,4
5,6
7,8
9,0
1,2
3,4
5,6
7,8

# showing result if amt of nums doesn't divide into cols cnt exactly.
./t.sh 4
1,2,3,4
5,6,7,8
9,0,1,2
3,4,5,6
7,8
Alternate to echo is to use printf

Last edited by chrism01; 04-11-2013 at 08:24 PM.
 
Old 04-12-2013, 11:28 AM   #7
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
I'll give this a try...forgot the for file in $(cat.t) command.
Thanks a bunch
 
Old 04-12-2013, 01:38 PM   #8
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Yeap it works but the problems is that I want an output as:
1,5
2,6
3,7
4,8


instead of
1,2
2,4
5,6
7,8

So basically I need another script to transpose this matrix but I can do it with excel for now. However a transpose script will be great!
Thanks a nlot again.
 
Old 04-14-2013, 06:43 AM   #9
rigor
Member
 
Registered: Sep 2011
Posts: 241

Rep: Reputation: Disabled
Drigo,

If the following script is in the file "mat_csv.sh"

Code:
#!/bin/bash +x

data=( $(<"$3") )

vals_per_col="$1"

vals_tot=${#data[*]}

for ((  val_num=0 ;  val_num < vals_tot ;  val_num++   ))
    do
        row_num=$(( val_num % vals_per_col ))
        row[$row_num]=${row[$row_num]}${row[$row_num]:+,}${data[$val_num]}
    done

for ((  row_num=0 ;  row_num < vals_per_col ;  row_num++   ))
    do
        IFS=',' echo ${row[row_num]}
    done
and the data you showed us in message #5 in this "thread" is in the file "AllNumbers.txt", then this:

Code:
mat_csv.sh 4 3 AllNumbers.txt
produces the output you showed us in message #5 in this "thread", except that values are separated with commas, since as chrism01 has mentioned, CSV is more usual for import to Excel. I don't quite know how you would apply this to the data in your original message in this thread.

Last edited by rigor; 04-14-2013 at 06:47 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Script to sum values across columns if they have the same row title kmkocot Programming 7 08-05-2012 07:19 PM
text manipulation in bash: sort columns according to the first row lethalfang Linux - Newbie 5 06-20-2011 04:10 PM
gnome-terminal is missing one pixel row and its an important row rednuht Linux - General 1 12-24-2009 10:30 AM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM


All times are GMT -5. The time now is 08:09 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration