awk printing all columns after (but including) $n


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk printing all columns after (but including) $n
# 1  
Old 03-05-2008
awk printing all columns after (but including) $n

I am piping an "ls -l" to awk so that all it returns is the file size, date, and file name. The problem is that some files may have spaces in the name so awk is only printing the first word in the file name. I won't know how many space-delimited words are in the filename, so what I want to do is just print everything to the right of position $8 including $8. Is there a way to do this?

Here is what I am using:
Code:
ls -l | awk '{print $5 "\t" $6 "\t" $8}'

A normal "ls -l" might return this:
Code:
-rwxr--r-- 1 user user 11948 2007-09-26 15:14 ubuntu normal size.png
-rwxr--r-- 1 user user 11948 2007-09-26 15:14 ubuntu oblique.png
-rwxr--r-- 1 user user 11948 2007-09-26 15:14 ubuntu_oblique.png

And my command would return this (dropping part of the file names with spaces):
Code:
11948   2007-09-26      ubuntu
11948   2007-09-26      ubuntu
11948   2007-09-26      ubuntu_oblique.png

But what I want to do is print $8 and everything that follows it, preserving the entire file name.

Thanks very much for any advice!.......Cassj
# 2  
Old 03-05-2008
Try this:

Code:
matt@dublinux  [/home/matt/tmp]$ ls -l
total 0
-rw-r--r-- 1 matt users 0 2008-03-05 13:39 ubuntu normal size.png
-rw-r--r-- 1 matt users 0 2008-03-05 13:40 ubuntu oblique.png
-rw-r--r-- 1 matt users 0 2008-03-05 13:40 ubuntu_oblique.png
matt@dublinux  [/home/matt/tmp]$ ls -l | cut -f5- -d ' '

0 2008-03-05 13:39 ubuntu normal size.png
0 2008-03-05 13:40 ubuntu oblique.png
0 2008-03-05 13:40 ubuntu_oblique.png

# 3  
Old 03-05-2008
That works beautifully! Thanks very much, matt! :-)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

2. UNIX for Dummies Questions & Answers

AWK print last field including SPACES

I have simple test.sh script, see below: bill_code=`echo $record | awk -F"|" '{print $1}'` Fullname=`echo $record | awk -F"|" '{print $3}'` email=`echo $record | awk -F\ '{print $4}'` The last field contains spaces: see csv below: A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com ... (9 Replies)
Discussion started by: quay
9 Replies

3. Shell Programming and Scripting

printing only columns containing a determined character with awk

Hello I'm trying to extract two columns from a database using awk, the thing is i have a variable number of columns in each row, and I just need to get out the two first ones ending in "," This is an example: ABE, ABE V149 MAZIE ARD CYN ACY, ACY, , , ,TEC, , 5000, , , 1, ZNY,ZDC ABE, ABE... (1 Reply)
Discussion started by: vir
1 Replies

4. Shell Programming and Scripting

Printing in columns

is there a short one-liner that can print out in columns instead of a long list? as in, instead of: apples oranges cats dogs sky monkey giraffe cups spoons tv cable it'll print something like this (properly indented of course :) ): (9 Replies)
Discussion started by: SkySmart
9 Replies

5. UNIX for Dummies Questions & Answers

Printing columns with header

Hi Gurus, I want to extract certain columns from file 2 and combine with file 1. I am using the following script to extract the columns. $ awk 'FNR>1{print $2, $9, FILENAME}' *.lim > out1 However, this script does not print the titles of the columns 2 and 9. Can somebody help me in... (1 Reply)
Discussion started by: Unilearn
1 Replies

6. Shell Programming and Scripting

Printing columns

Im using awk to print columns. Basically I have a file with like 500 columns and I want to print the 200th-300th column and ignore the rest... how would I do it without putting $200, $201 .... $300 thanks (6 Replies)
Discussion started by: kylle345
6 Replies

7. Shell Programming and Scripting

printing two lines in awk as two columns in excel

hi guys, i would like to print two lines from a file as two adjacent columns using excel using awk.. i have this so far: awk '{for(i=1; i<=NF; i++) {printf("%s\n",$i)}}' "$count".ttt > "$count".csv #this to print the first line from the .ttt file as rows of the first column in the .csv... (9 Replies)
Discussion started by: npatwardhan
9 Replies

8. Shell Programming and Scripting

including spaces in awk output

I need to tweek my awk output: #cat filename ab cd ef:ghi:jk lm:nop qrs #cat filename | awk '{ for(i=3;i<NF+1;i++) printf $i}' ef:ghi:jklm:nopqrs I would like the ouput to include the original spaces from columns 3 on: ef:ghi:jk lm:nop qrs any suggestions? (4 Replies)
Discussion started by: prkfriryce
4 Replies

9. Shell Programming and Scripting

Printing Columns in Unix

ok. this is a bit of a difficult question but i've been trying to figure this out for quite some time but couldn't. how do I print columns on the screen? like take for instant. using the ls and the file command, how do i print it so i can have the filenames on the left hand side and the... (3 Replies)
Discussion started by: Terrible
3 Replies

10. Shell Programming and Scripting

printing columns

Here's what I wrote: #!/bin/sh d1=`grep Dialtone dialtone | awk '{print $2, $3, $4, $5, $6, $7, $8, $9}'` d2=`grep pstsys dialtone | awk '{print $12}'` echo "$d1 $d2" I expected the result to be this: Dialtone on host 1 slot 13 port 1, pstsys05 Dialtone on host 1 slot 13 port 1,... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question