LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-22-2003, 04:21 AM   #1
jpostma
Member
 
Registered: Jun 2003
Location: Alphen ad Rijn -- The Netherlands
Distribution: Fedora Core 2 - Slackware 12
Posts: 119

Rep: Reputation: 15
awk problem


I want to create a new list of ls -l. This new list includes the filename or directoryname. I added in awk Print $9. The problem is if the filname or directoryname contains a space only the first part of the filename will be printed in the new list. E.g. if the directory is "New Folder" only New will be printed.

How can I solve this problem?

Johannes
 
Old 10-22-2003, 04:46 AM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
Code:
 
ls -l | awk '{for(i=9;i<=NF;i++){printf $i" "}{print ""}}'
This would output all the fields starting from the 9th field.
NF contains the number of fields of the line for which awk is done.
 
Old 10-22-2003, 04:49 AM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
Hmmm to make it easier you could just use

ls -l | awk '{print $8 $9}'

 
Old 10-22-2003, 04:52 AM   #4
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
Oops i meant $9 and $10

basically, every word between whitespace is a higher number - i.e.

Hello World From LQ

$1 $2 $3 $4

hope this helps
 
Old 10-22-2003, 05:20 AM   #5
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Couldn't you use substr?

ls -l | awk '{print substr($0,57)}'

works for me, but if the file is a link, you get all that stuff too.

John
 
Old 10-22-2003, 05:52 AM   #6
jpostma
Member
 
Registered: Jun 2003
Location: Alphen ad Rijn -- The Netherlands
Distribution: Fedora Core 2 - Slackware 12
Posts: 119

Original Poster
Rep: Reputation: 15
Thanks for the answers. I will try it tonight. Maybe the problem still exits if the directoryname contains more than one space. E.g.

New Folder (two spaces between New and Folder)

Johannes
 
Old 10-22-2003, 06:04 AM   #7
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
No, usually awk will treat consecutive whitespace as one.

But the simplest answer is probably...

ls -1 // That switch is "one", not "ell".

John
 
Old 10-22-2003, 06:07 AM   #8
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
LOL Didn't even think of that
 
  


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
AWK problem embsupafly Programming 13 08-21-2005 07:43 PM
script problem while using awk and passing varibales ulto Programming 6 04-23-2004 11:58 AM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM
sed/awk problem player_2 Programming 9 08-26-2003 06:09 PM
awk problem alaios Programming 4 05-04-2003 09:46 AM


All times are GMT -5. The time now is 09:31 PM.

Main Menu
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