LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   list file by column (https://www.linuxquestions.org/questions/linux-newbie-8/list-file-by-column-944377/)

ust 05-11-2012 05:29 AM

list file by column
 
I have a file , the content is as below , the character is separated by space ,

file content
============
aa bb cc
dd dd ff
"
"

can advise if I want to list it by column as below .

column 1
============
aa
dd

column 2
============
bb
dd

column 3
============
cc
ff


what can i do ? thx

fukawi1 05-11-2012 05:39 AM

I dont really understand your question, and it is difficult to give an example when you only gave two lines of sample data.
But i think sort will be the solution to your needs, I replicated the pattern in your examples further down the alphabet for a clearer example.
"man sort" for info on what criteria you want to sort by.
Code:

$ echo -e "aa bb cc\ndd dd ff\ntt uu vv\nww ww xx" | sort
aa bb cc
dd dd ff
tt uu vv
ww ww xx


David the H. 05-11-2012 12:11 PM

***Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.***

(934 posts and still posting data raw? Doesn't anyone know what code tags are anymore? :rolleyes: )


As fukawi1 said, more details are in order. But if you want to do it all in one operation you'll probably have to write an awk script. I will probably involve storing fields in arrays and printing them out again at the end or something.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/


Printin a single column only though is easy; just use cut:

Code:

cut -d " " -f 1 infile

allend 05-11-2012 09:56 PM

Probably not what you want, but this is an alternative that does not consider field delimiters.
Code:

bash-4.2$ echo -e "aa bb cc\ndd dd ff\ntt uu vv\nww ww xx" | sort | colrm 3 8
aa
dd
tt
ww
bash-4.2$ echo -e "aa bb cc\ndd dd ff\ntt uu vv\nww ww xx" | sort | colrm 1 3 | colrm 3 5                                                                                     
bb                                                                                                                                                                           
dd                                                                                                                                                                           
uu                                                                                                                                                                           
ww                                                                                                                                                                           
bash-4.2$ echo -e "aa bb cc\ndd dd ff\ntt uu vv\nww ww xx" | sort | colrm 1 6
cc                                                                                                                                                                           
ff                                                                                                                                                                           
vv                                                                                                                                                                           
xx


ust 05-14-2012 07:51 PM

Quote:

Originally Posted by fukawi1 (Post 4675830)
I dont really understand your question, and it is difficult to give an example when you only gave two lines of sample data.
But i think sort will be the solution to your needs, I replicated the pattern in your examples further down the alphabet for a clearer example.
"man sort" for info on what criteria you want to sort by.
Code:

$ echo -e "aa bb cc\ndd dd ff\ntt uu vv\nww ww xx" | sort
aa bb cc
dd dd ff
tt uu vv
ww ww xx


thx reply ,

but it seems not fit my requirement , may be my question is not clear

I give another example , my file is as like the crontab file


00 15 * 8 * /bin/chmod 755 /tmp
00 17,20 * 9 1 /bin/runcron
"
"

what I want is to sort it by delimiter , the delimiter is space , I have upload my desired output as attachment , can advise what can i do ? thx

ust 05-14-2012 07:58 PM

1 Attachment(s)
please see the attachment

allend 05-14-2012 09:37 PM

Your output.txt file simply shows the fields separated by tab characters rather than spaces.
You could use the 'tr' command to convert spaces to tabs.
e.g. cat <file> | tr "[:space:]" "\t"

Tinkster 05-14-2012 10:45 PM

Just looking at the original sample, and output....
Code:

$ cat ust
aa bb cc
dd dd ff
$ cat ust.awk
{
  for(i=1;i<=NF;i++){
    a[NR" "i]=$i
  }
}
END{
  for(b in a){
    print a[b]
  }
}
$ awk -f ust.awk ust
aa
dd
bb
dd
cc
ff



Cheers,
Tink

ust 05-14-2012 10:46 PM

Quote:

Originally Posted by allend (Post 4678428)
Your output.txt file simply shows the fields separated by tab characters rather than spaces.
You could use the 'tr' command to convert spaces to tabs.
e.g. cat <file> | tr "[:space:]" "\t"

thx reply,

it works ,

may I ask what is the use of tr ? is it used to remove the delimiter - space ?

chrism01 05-15-2012 12:22 AM

http://linux.die.net/man/1/tr

ust 05-15-2012 01:21 AM

one more request , if I want to seperate the first 5 column by delimiter , not the whole file , what can i do ? thx

fukawi1 05-15-2012 02:45 AM

Quote:

Originally Posted by ust (Post 4675820)
I have a file , the content is as below , the character is separated by space ,

file content
============
aa bb cc
dd dd ff
"
"

Quote:

my file is as like the crontab file


00 15 * 8 * /bin/chmod 755 /tmp
00 17,20 * 9 1 /bin/runcron
"
"
Quote:

but it seems not fit my requirement
Well, in my defense, I never stood a bloody chance...
Not only did you mislead the forum by posting irrelevant crap as sample data.
The irrelevant crap wasn't even close (alpha rather than numeric) to the same format of the REAL data you wanted answers for..

I guess, luckily for you, the rest of the LQ community is somewhat more tolerant of stupidity than I am.

ust 05-15-2012 03:56 AM

Quote:

Originally Posted by fukawi1 (Post 4678594)
Well, in my defense, I never stood a bloody chance...
Not only did you mislead the forum by posting irrelevant crap as sample data.
The irrelevant crap wasn't even close (alpha rather than numeric) to the same format of the REAL data you wanted answers for..

I guess, luckily for you, the rest of the LQ community is somewhat more tolerant of stupidity than I am.

thx r suggetion ,

I am not intended to mislead the forum by re-post the requirement , but after I use it , there have a problem , so I ask for help again .

ust 05-15-2012 09:38 AM

hi all,

as my previous , if I want to seperate the first 5 column not the shole file ( eg . the first 5 column of a crontab file is data / time ) , cam advise what can i do ?

thx

allend 05-15-2012 12:11 PM

I am assuming that you want a formatted display of a crontab file. It is much easier to help when good examples of input file format and desired output file format are supplied. It is also more likely that you will get help when you demonstrate some application to the task, such as reading manual pages, writing posts carefully with attention to spelling and use of proper language rather than text messaging abbreviations.
I show a file ust.txt containing this data
Quote:

00 15 * 8 * /bin/chmod 755 /tmp aa bb cc
00 17,20 * 9 1 /bin/runcron dd ee ff
00 17,20 * 9 1 /bin/runcron gg hh
00 17,20 * 9 1 /bin/runcron jj
Despite my reservations about this being a homework assignment, here is a bash script that seems to do what you want.
Code:

#!/bin/bash

while read -a line; do
  echo -n ${line}
  for (( i=1; i<${#line[*]}; i++ )); do
    if [[ $i < 5 ]]; then
      echo -ne "\t"${line[$i]};
    else
      echo -n " "${line[$i]};
    fi
  done
  echo
done < ust.txt



All times are GMT -5. The time now is 02:26 PM.