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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
05-11-2012, 05:29 AM
|
#1
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Rep:
|
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.
|
05-11-2012, 05:39 AM
|
#2
|
Member
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854
Rep:
|
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
|
|
|
05-11-2012, 09:56 PM
|
#4
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,449
|
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
|
|
|
05-14-2012, 07:51 PM
|
#5
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by fukawi1
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.
|
|
|
05-14-2012, 07:58 PM
|
#6
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
please see the attachment
|
|
|
05-14-2012, 09:37 PM
|
#7
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,449
|
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"
|
|
|
05-14-2012, 10:45 PM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
05-14-2012, 10:46 PM
|
#9
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by allend
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 ?
|
|
|
05-15-2012, 12:22 AM
|
#10
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
|
|
|
05-15-2012, 01:21 AM
|
#11
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
one more request , if I want to seperate the first 5 column by delimiter , not the whole file , what can i do ? thx
|
|
|
05-15-2012, 02:45 AM
|
#12
|
Member
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854
Rep:
|
Quote:
Originally Posted by ust
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.
|
|
|
05-15-2012, 03:56 AM
|
#13
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
Quote:
Originally Posted by fukawi1
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 .
|
|
|
05-15-2012, 09:38 AM
|
#14
|
Senior Member
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130
Original Poster
Rep:
|
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
|
|
|
05-15-2012, 12:11 PM
|
#15
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,449
|
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:33 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|