LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-21-2006, 04:17 AM   #1
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Rep: Reputation: 30
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?
 
Old 02-21-2006, 04:39 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
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
 
Old 02-21-2006, 04:41 AM   #3
furquan
Member
 
Registered: Feb 2002
Posts: 30

Rep: Reputation: 15
You could do this with awk to print column 4&6

Quote:
awk '{print $4, $6} filename
 
Old 02-21-2006, 04:44 AM   #4
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Original Poster
Rep: Reputation: 30
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)?
 
Old 02-21-2006, 04:54 AM   #5
furquan
Member
 
Registered: Feb 2002
Posts: 30

Rep: Reputation: 15
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.
 
Old 02-21-2006, 05:02 AM   #6
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Original Poster
Rep: Reputation: 30
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
 
Old 02-21-2006, 05:06 AM   #7
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
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
 
Old 02-21-2006, 05:06 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Awk has a printf function that you can use to provide the formatting you want.
 
Old 02-21-2006, 05:08 AM   #9
furquan
Member
 
Registered: Feb 2002
Posts: 30

Rep: Reputation: 15
It is aligned, If you notice the file size for FEB is larger than any other file and hence it looks out of place.
 
Old 02-21-2006, 05:15 AM   #10
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Original Poster
Rep: Reputation: 30
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}
 
Old 02-28-2006, 08:20 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


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
shell script to read ps -e output and determine process double processes. dr_zayus69 Programming 1 09-21-2005 06:37 PM
printf: postion cursor to output columns schneidz Programming 5 09-20-2005 09:59 AM
weird sed output schneidz Programming 3 05-19-2005 12:16 PM
python read output from seperate script? bendeco13 Programming 1 02-01-2005 11:38 PM
Removing Columns From Output darthtux Programming 4 08-01-2004 10:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 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
Open Source Consulting | Domain Registration