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 05-11-2012, 05:29 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-11-2012, 05:39 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
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
 
Old 05-11-2012, 12:11 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
***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? )


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
 
Old 05-11-2012, 09:56 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,501

Rep: Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383
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
 
Old 05-14-2012, 07:51 PM   #5
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by fukawi1 View Post
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

Last edited by ust; 05-14-2012 at 07:59 PM.
 
Old 05-14-2012, 07:58 PM   #6
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
please see the attachment
Attached Files
File Type: txt output.txt (80 Bytes, 9 views)
 
Old 05-14-2012, 09:37 PM   #7
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,501

Rep: Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383
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"
 
Old 05-14-2012, 10:45 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 05-14-2012, 10:46 PM   #9
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by allend View Post
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 ?
 
Old 05-15-2012, 12:22 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,294

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
http://linux.die.net/man/1/tr
 
Old 05-15-2012, 01:21 AM   #11
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
one more request , if I want to seperate the first 5 column by delimiter , not the whole file , what can i do ? thx
 
Old 05-15-2012, 02:45 AM   #12
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Quote:
Originally Posted by ust View Post
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.
 
Old 05-15-2012, 03:56 AM   #13
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by fukawi1 View Post
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 .
 
Old 05-15-2012, 09:38 AM   #14
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
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
 
Old 05-15-2012, 12:11 PM   #15
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,501

Rep: Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383Reputation: 1383
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
 
  


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
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
[SOLVED] Is it possible to use the ls command to list files in one column? BobNutfield Slackware 23 09-21-2010 02:13 AM
Bash script to suppress matches in two column list xsyntax Linux - General 6 09-03-2010 05:08 AM
error unknown column 'xxx' in field list rajesh84210 Linux - Newbie 4 09-07-2009 11:22 PM
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM


All times are GMT -5. The time now is 07:02 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