LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Register
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 09-21-2008, 12:18 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 189

Rep: Reputation: 16
why is doesnt work? (awk, set)


set DBB = /goog/db/
ls -ltr db/ | awk '{print$DBB,$9}'

I want to add "/goog/db/" for each line with awk.
 
Old 09-21-2008, 01:16 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
Since you are removing all of the items supplied by the -l option, why use it? You could use print just the filenames as is normal and add '/goog/db/' before each line.

My version of ls starts the filename at field 8:
Code:
ls -ltr | awk -v DBB=/goog/db/ 'NR>1 { printf DBB; for (i=8;i<NF;i++) printf "%s ", $i ; printf $NF"\n"}'
Because a filename may contain whitespace, some lines may contain more than 8 fields. You need to iterate from 8 to the end and print out each part.

Last edited by jschiwal; 09-21-2008 at 01:22 AM.
 
Old 09-21-2008, 01:47 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
find -printf "/goog/db %f\n"
 
Old 09-21-2008, 03:13 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Try it without spaces on both sides of the = sign.

End
 
Old 09-21-2008, 06:32 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
  1. Why it doesn't work:
    Code:
    set DBB = /goog/db/
    ls -ltr db/ | awk '{print$DBB,$9}'
    because you didn't put the bash variable (DDB) into awk correctly -- see jschiwal's syntax in post #2.

  2. I assume (& applaud) that you are using the "DDB" variable to make your code more general. In general (), this is a good idea.

  3. Why are you using ls's "-l" (ell) option when you are only going to throw everything from the 9th field on? The "-1" (one) option will put everything in a single column.

  4. Why are you using awk when it will stumble over file names w/ spaces in them?

  5. Try this instead:
    Code:
    DDB='/goog/db/'
    ls -1tr | while read F; do echo "$DDB$F"; done
    Tested on my ~/.opera/sessions/ directory which has a file w/ spaces in its name.
 
  


Reply



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
fgrlx doesnt work Odious Linux - Laptop and Netbook 3 09-27-2007 04:42 PM
rmdir -rf does not seem to work in fc4 .what do i do?rm -p also doesnt seem to work vinay87 Linux - Newbie 2 05-09-2006 09:18 AM
Why doesnt my USB mouse doesnt work? barkha Linux - Hardware 2 08-16-2005 11:31 AM
LYNX doesnt work emrkar Linux - General 3 03-03-2004 03:14 PM
gethostbyname() doesnt always work nodger Programming 1 01-22-2004 10:56 AM

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

All times are GMT -5. The time now is 04:56 PM.

Contact Us - Advertising Info - Rules - Privacy - Donations - Contributing Member - LQ Sitemap - "Weather apps tell you it'll rain. Wyndo tells you when to go."
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