Print filenames with spaces using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print filenames with spaces using awk
# 1  
Old 11-30-2009
Print filenames with spaces using awk

Hello all,
I want to list the file contents of the directory and number them. I am using la and awk to do it now,

#ls |awk '{print NR "." $1}'
1. alpha
2. beta
3. gamma

The problem I have is that some files might also have some spaces in the filenames.

#ls
alpha beta gamma some file with spaces.txt

Now I want it this way
1. alpha
2. beta
3. gamma
4. some file with spaces.txt

How do I do it?
# 2  
Old 11-30-2009
Code:
printf '%s\n' *|nl

If the format is important:

Code:
printf '%s\n' *|nl -w1 -s.

# 3  
Old 11-30-2009
I need something like this

Filename 1 is alpha
Filename 2 is beta
Filename 3 is gamma
Filename 4 is some file with spaces.txt
# 4  
Old 11-30-2009
Code:
awk 'BEGIN {
  while (++i < ARGC)
    printf "Filename %d is %s\n", i, ARGV[i]
    }' *

# 5  
Old 12-01-2009
Lets say, I have 3 files in a directory

#ls
alpha
beta
file with spaces

Now I want to list them in the following manner
<LI><A href="alpha">alpha</A>
<LI><A href="beta">beta</A>
<LI><A href="file with spaces">file with spaces</A>

I tried this,
ls |awk '{ print "<LI><A href=\" "$1" \"> " $1 "</A>"}'

It works fine for the files without spaces but only pickes up the first word of the file with spaces.

I know that I am doing it wrong, but am not sure of the corrected method. Please help.
# 6  
Old 12-01-2009
Code:
ls |awk '{ print "<LI><A href=\" "$0" \"> " $0 "</A>"}'

# 7  
Old 12-01-2009
Quote:
Originally Posted by rdcwayx
Code:
ls |awk '{ print "<LI><A href=\" "$0" \"> " $0 "</A>"}'

I would avoid to use external commands (ls) in this case,
AWK can do it all Smilie.

Code:
awk 'BEGIN {
  while (++i < ARGC)
    printf "<LI><A href=\"%s\">%s</A>\n", ARGV[i], ARGV[i]
    }' *

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print string with removing all spaces

Hi all, I want to set 10 set of strings into a variable where: removing all spaces within each string change the delimiter from "|" to "," Currently, I've the below script like this:Table=`ten character strings with spaces in-between and each string with delimiter "|" | tr -d ' ' |... (7 Replies)
Discussion started by: o1283c
7 Replies

2. Shell Programming and Scripting

awk - print columns with text and spaces

Hi, I'm using awk to print columns from a tab delimited text file: awk '{print " "$2" "$3" $6"}' file The problem I have is column 6 contains text with spaces etc which means awk only prints the first word. How can I tell awk to print the whole column content as column 6? Thanks, (10 Replies)
Discussion started by: keenboy100
10 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Print files with spaces using awk

When I use: find . -ls | awk 'BEGIN { OFS = ";"} {print $1,$2,$11}'I get a nice result, yet the files with spaces in it show only the first word and all other characters after the blank space are not printed. e.g. 'file with a blank space' is printed file 'file_with a blank space' is... (7 Replies)
Discussion started by: dakke
7 Replies

5. Shell Programming and Scripting

[awk] print all filenames in directory

Hello, I was given a (I suppose) a simple task which I don't know how to do. Before coming here I have read a dozen of awk tutorials (full read, not just line-skipping) but I still don't know how to do this. Task: Write an script file 'check.awk' with a parameter current directory that... (5 Replies)
Discussion started by: c0dehunter
5 Replies

6. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

7. Shell Programming and Scripting

How to print the last column of the list contain filenames with spaces.

Hi experts, I have the following data: I want the last filed in the output. How to print the last field , It contains the file names and few filenames with white spaces . -rw-r--r-- 1 root root 0 2010-04-26 16:57 file1 2space_File.txt -rw-r--r-- 1 root root 0 2010-04-26... (2 Replies)
Discussion started by: rveri
2 Replies

8. Shell Programming and Scripting

awk help with find command and filenames with spaces

I have the following code: find /usr/local/test5 -type f -mtime +30 -exec ls -l {} \; | awk '{print $5, $6, $7, $8, $9}' I have this as output: 14 Aug 12 00:00 /usr/local/test5/file1 14 Aug 12 00:00 /usr/local/test5/lastname, The bolded part is where I run into trouble. The actual... (4 Replies)
Discussion started by: streetfighter2
4 Replies

9. Shell Programming and Scripting

How to print arguments along with spaces using awk

Hi All, file_1.txt contains aaa bbbb hhhh vvvvv mmmmm iiiii What i want is to search for the first coloumn of each line using awk.i.e as below: awk '/aaa/ {printf(<>)}' file_1.txt The print part (<>) should contain all the values(or coloumns ) in the particular line(here it... (8 Replies)
Discussion started by: jisha
8 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question