LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 12-15-2009, 01:58 PM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
printing multiple columns with awk


Hi people,

I have a text file which has the output in this fashion


Code:
-rw-rw-r-- joe/joe  312 2002-05-07 09:38:51 file.txt
-rw-rw-r-- joe/joe  312 2002-05-07 09:38:51 file1.txt
-rw-rw-r-- joe/joe  312 2002-05-07 09:38:51 file2 my file.txt
-rw-rw-r-- joe/joe  312 2002-05-07 09:38:51 file3 my file,txt
Can any one tell me is there any way of printing the entire filename (including the spaces)?

Code:
awk -F" " '{print $6}'
only prints file2 & file3 as output where as i expect it to print the entire filename which may have any number of spaces.


Thanks
 
Old 12-15-2009, 02:05 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Do you have to use awk?

cut -d" " -f7- infile

Hope this helps.
 
Old 12-15-2009, 02:19 PM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
hi,

Thanks it works.Ill check it out if i can use this in my programme. I am trying to read a file which say has contents of ls -l in it,and extract the file names from it and add it to a database,so if a file name has spaces in it my script is failing.Ill try it out with cut.


EDIT: -f1,3 prints 1 and 3
-f1-3 prints 1 to 3
-f1- prints 1 to infinte got it thanks.

It would be nice to know how to do the same using awk and print a range of columns.

Last edited by kdelover; 12-15-2009 at 02:27 PM.
 
Old 12-15-2009, 02:57 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Only awk solution I can think of at the moment is this:

awk 'BEGIN { OFS="" ; ORS="" } ; { for ( i=6; i<NF; i++ ) print $i " "; print $NF "\n" }' infile

Hope this helps.
 
Old 12-15-2009, 03:20 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Code:
awk -F":[0-9][0-9] " '{print $2}' filename
 
Old 12-15-2009, 03:28 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@pixellany: That is very short and compact. Nice!
 
Old 12-15-2009, 03:39 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Code:
awk -F":" '{print substr($3,4)}' filename
Someone stop me.........
 
Old 12-15-2009, 04:58 PM   #8
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Thanks pixel and drruna,id be glad if you can tell me how

awk 'BEGIN { OFS="" ; ORS="" } ; { for ( i=6; i<NF; i++ ) print $i " "; print $NF "\n" }' infile

and

awk -F":" '{print substr($3,4)}' filename

are working :-) thanks to all.
 
Old 12-15-2009, 07:59 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk '{$1=$2=$3=$4=$5="";sub(/^ +/,"")}1'   file
 
Old 12-15-2009, 08:00 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by pixellany View Post
Code:
awk -F":" '{print substr($3,4)}' filename
Someone stop me.........
file name can contain ":" as well
 
Old 12-16-2009, 12:24 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by ghostdog74 View Post
file name can contain ":" as well
Missing your point....(I'm slow)
 
Old 12-16-2009, 12:29 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by kdelover View Post
Thanks pixel and drruna,id be glad if you can tell me how

<<snip>>

awk -F":" '{print substr($3,4)}' filename

are working :-) thanks to all.
I assume you are familiar with the basics of AWK**.....

Having defined ":" as the IFS, the third field is ---e.g.:
"51 file2 my file.txt"

The substr function returns the string, starting with the 4th character.


**If not, go here:
http://www.grymoire.com/Unix/
 
Old 12-16-2009, 06:20 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by kdelover View Post
Thanks pixel and drruna,id be glad if you can tell me how

awk 'BEGIN { OFS="" ; ORS="" } ; { for ( i=6; i<NF; i++ ) print $i " "; print $NF "\n" }' infile
Like pixellany, I do assume a basic understanding of awk.

The command given:
- sets the output field and record separator (OFS and ORS) to NULL, which makes sure that all is printed on the same line.
- prints fields number 6 up to the last field (NF holds the last field) on one line,
- the print $NF "\n" prints the last field and a carriage return (thus making sure all entries are on their own line).

Hope this clears things up a bit.
 
Old 12-16-2009, 07:08 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by pixellany View Post
Missing your point....(I'm slow)
if input has
Code:
-rw-rw-r-- joe/joe  312 2002-05-07 09:38:51 file2:my:file.txt
then by using -F":", the field numbers will be wrong.
 
Old 12-16-2009, 09:19 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by ghostdog74 View Post
if input has
Code:
-rw-rw-r-- joe/joe  312 2002-05-07 09:38:51 file2:my:file.txt
then by using -F":", the field numbers will be wrong.
Of course!! But then, why would anyone put ":" in filenames?

Seriously, many of the solutions that we come up with are not robust---you're one of the people that keeps people like me honest....

Sometimes the discussions in programming turn out to be "cleverness" contests---and the results---while educational---are not what you would want to use for "real work".
 
  


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
sort by multiple columns wakatana Linux - Newbie 5 10-18-2009 04:35 PM
Using awk to switch columns bioinformatics_guy Linux - Newbie 3 10-30-2008 10:50 AM
awk print correct lines when certain columns are blank schneidz Programming 11 04-04-2008 05:06 PM
Moving columns with sed or awk? btm Linux - Newbie 4 09-27-2007 03:03 PM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 11:10 AM


All times are GMT -5. The time now is 05:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration