Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
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.
|
|
02-21-2006, 04:17 AM
|
#1
|
Member
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223
Rep:
|
sed script to read only columns 4 to 6 in output database
hello,
I have a application that outputs data in 6 columns but i want only col 4 to 6 information how can i do that?
|
|
|
02-21-2006, 04:39 AM
|
#2
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
why sed??
Code:
awk '{print $4 $5 $6}' $your_output
should be fine.
you can send your output to "pipe" as well.
regards,
slackie1000
|
|
|
02-21-2006, 04:41 AM
|
#3
|
Member
Registered: Feb 2002
Posts: 30
Rep:
|
You could do this with awk to print column 4&6
Quote:
awk '{print $4, $6} filename
|
|
|
|
02-21-2006, 04:44 AM
|
#4
|
Member
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223
Original Poster
Rep:
|
hi,
No its not working for what i want
eg. if i give
Quote:
ls -lrt | awk '{print $4 $5 $6}'
|
in /etc and i got
root93Feb
root171830Feb
root70Feb
root804Feb
which is not correct
Also how can i include headers(i mean first line ststic in output)?
|
|
|
02-21-2006, 04:54 AM
|
#5
|
Member
Registered: Feb 2002
Posts: 30
Rep:
|
you need to seperate your print statments with a comma (,)
Quote:
ls -lrt | awk '{print $4, $5, $6}'
|
not sure what you mean by headers.
|
|
|
02-21-2006, 05:02 AM
|
#6
|
Member
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223
Original Poster
Rep:
|
hi,
thanks but what i have to do so that output will come proper aligned.
what i get is
root 355 Feb
root 93 Feb
root 171830 Feb
root 70 Feb
root 804 Feb
which is not aligned to columns starting at some distance
|
|
|
02-21-2006, 05:06 AM
|
#7
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
Quote:
Originally Posted by cranium2004
hi,
thanks but what i have to do so that output will come proper aligned.
what i get is
root 355 Feb
root 93 Feb
root 171830 Feb
root 70 Feb
root 804 Feb
which is not aligned to columns starting at some distance
|
next time you can elaborate better your question in the first post..
the following quick and dirty code ..
Code:
awk '
begin
{
printf("%6s %6s %6s",$4,$5,$6)
} ' $some_file
will format the output for you...
regards,
slackie1000
|
|
|
02-21-2006, 05:06 AM
|
#8
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Awk has a printf function that you can use to provide the formatting you want.
|
|
|
02-21-2006, 05:08 AM
|
#9
|
Member
Registered: Feb 2002
Posts: 30
Rep:
|
It is aligned, If you notice the file size for FEB is larger than any other file and hence it looks out of place.
|
|
|
02-21-2006, 05:15 AM
|
#10
|
Member
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223
Original Poster
Rep:
|
furguan,
then how can ls -lrt gives proper output?
slakie1000,
how can i use that script? actually it will be good if i will get single command
like ls -lrt | {command to output col 4 to 6}
|
|
|
02-28-2006, 08:20 AM
|
#11
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You can remove the $somefile from the end if you want to pipe the output of the ls command directly to the awk command.
Another way of extracting certain columns from ls -l is to use the "cut" command.
example:
ls -ld * | tr -s ' ' | cut -d' ' -f5,8 --output-delimiter=" "
( note: for the output delimiter I pressed CNTRL-v [TAB] )
The tr command removes duplicate spaces between each field. The cut command then extracts fields 5 and 8. A tab is used in between the size and name on the output. The cut and tr commands are a lot smaller than awk, 34k vs 226k but my example uses 3 commands instead of two. Using a wildcard at the end of the ls command avoids the initial total line.
|
|
|
All times are GMT -5. The time now is 06:14 AM.
|
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
|
|