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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
12-15-2009, 12:58 PM
|
#1
|
|
Member
Registered: Aug 2009
Posts: 306
Rep:
|
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 
|
|
|
|
12-15-2009, 01:05 PM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
Hi,
Do you have to use awk?
cut -d" " -f7- infile
Hope this helps.
|
|
|
|
12-15-2009, 01:19 PM
|
#3
|
|
Member
Registered: Aug 2009
Posts: 306
Original Poster
Rep:
|
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 01:27 PM.
|
|
|
|
12-15-2009, 01:57 PM
|
#4
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
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.
|
|
|
|
12-15-2009, 02:20 PM
|
#5
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Code:
awk -F":[0-9][0-9] " '{print $2}' filename
|
|
|
|
12-15-2009, 02:28 PM
|
#6
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
@pixellany: That is very short and compact. Nice!
|
|
|
|
12-15-2009, 02:39 PM
|
#7
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Code:
awk -F":" '{print substr($3,4)}' filename
Someone stop me......... 
|
|
|
|
12-15-2009, 03:58 PM
|
#8
|
|
Member
Registered: Aug 2009
Posts: 306
Original Poster
Rep:
|
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.
|
|
|
|
12-15-2009, 06:59 PM
|
#9
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
awk '{$1=$2=$3=$4=$5="";sub(/^ +/,"")}1' file
|
|
|
|
12-15-2009, 07:00 PM
|
#10
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by pixellany
Code:
awk -F":" '{print substr($3,4)}' filename
Someone stop me......... 
|
file name can contain ":" as well 
|
|
|
|
12-15-2009, 11:24 PM
|
#11
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by ghostdog74
file name can contain ":" as well 
|
Missing your point....(I'm slow)
|
|
|
|
12-15-2009, 11:29 PM
|
#12
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by kdelover
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/
|
|
|
|
12-16-2009, 05:20 AM
|
#13
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
Quote:
Originally Posted by kdelover
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.
|
|
|
|
12-16-2009, 06:08 AM
|
#14
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by pixellany
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.
|
|
|
|
12-16-2009, 08:19 AM
|
#15
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by ghostdog74
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".
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:33 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
|
|