LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why is doesnt work? (awk, set) (https://www.linuxquestions.org/questions/programming-9/why-is-doesnt-work-awk-set-671329/)

DoME69 09-21-2008 12:18 AM

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.

jschiwal 09-21-2008 01:16 AM

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.

ghostdog74 09-21-2008 01:47 AM

Code:

find -printf "/goog/db %f\n"

AnanthaP 09-21-2008 03:13 AM

Try it without spaces on both sides of the = sign.

End

archtoad6 09-21-2008 06:32 AM

  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.


All times are GMT -5. The time now is 06:47 PM.