Print files with spaces using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print files with spaces using awk
# 1  
Old 04-01-2011
Print files with spaces using awk

When I use:
Code:
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 printed
file_with

What am I doing wrong?
# 2  
Old 04-01-2011
The default field delimiter is a space. A filename with three spaces will be $2 $3 $4 for example.
# 3  
Old 04-01-2011
Aha, slowly understanding this.

So I in fact must do something else. I posted another thread here and I now understand that the solution I had there is not what I needed.

I use
Code:
find . -ls | awk 'BEGIN { OFS = ";"} {print $1,$2,$11,$12}'

to list all files, folders and subfolders and for each of those files list the
  • file type (dir, file, symlink)
  • unique identifier (I need this for later in my excel file)
  • file name
Ideally I would also like to list for each of those files the folder it belongs to (in case of stored in root, put 'root').

Instead of using awk then, what should I use?
# 4  
Old 04-01-2011
Could you show an sample output you want, it would be esay to understand correctly
# 5  
Old 04-01-2011
Sure!

Suppose the following folder/file structure:
Code:
Folder 1
-- File 1
-- File 2
Folder 2
-- File 3
-- Folder 21
---- Folder 4
---- Folder 5
-- Folder 22
---- Folder 6

I would like a list as follows:
Code:
./Folder 1 (--Directory--) (--root--) (--UniqueID 1--)
./Folder 1/File 1 (--File--) (--./Folder 1--) (--UniqueID 2--)
./Folder 1/File 2   (--File--) (--./Folder 1--) (--UniqueID 3--)
./Folder 2  (--Directory--) (--root--) (--UniqueID 4--)
./Folder 2/File 3  (--File--) (--./Folder 2--) (--UniqueID 5--)
./Folder 2/Folder 21  (--Directory--) (--./Folder 2--) (--UniqueID 6--)
./Folder 2/Folder 21/File 4  (--File--) (--./Folder 2/Folder 21--) (--UniqueID 7--)
./Folder 2/Folder 21/File 5  (--File--) (--./Folder 2/Folder 21--) (--UniqueID 8--)
./Folder 2/Folder 22  (--Directory--) (--./Folder 2--) (--UniqueID 9--)
./Folder 2/Folder 22/File 6  (--File--) (--./Folder 2/Folder 22--) (--UniqueID 10--)

  • Name of the file (filepath is ok as well, as in example)
  • For the file type (in previous only file, dir... in real I also need symlink)
  • Folder it belongs to (path)
  • For the UniqueID I could use the inode
# 6  
Old 04-01-2011
how about this?
Code:
fold and file information:
 
find . -ls |awk -F'.' 'NR>1{print $NF}'
/folder2
/folder2/file6
/folder1
/folder1/folder11
/folder1/folder11/file3
/folder1/folder11/file4
/folder1/file2
/folder1/file\ 1
/folder4
/folder4/file5
/folder3
 
output:
 
 find . -ls |
awk -F'.' 'NR>1{print ($1~/dr/)?"D."$NF:"F."$NF}' |
awk -F'./' 'NF==2&&/^D/{print substr($0,2),"(--D--)","(--root--)","(--UniqueID "NR"--)"}
NF>2&&/^D/{print substr($0,2),"(--D--)","(--"gensub(/D(\.\/.*)\/.*/,"\\1",1,$0)"--)","(--UniqueID "NR"--)"}
/^F/{print  substr($0,2),"(--F--)","(--"gensub(/F(\.\/.*)\/.*/,"\\1",1,$0)"--)","(--UniqueID "NR"--)"}'
./folder2 (--D--) (--root--) (--UniqueID 1--)
./folder2/file6 (--F--) (--./folder2--) (--UniqueID 2--)
./folder1 (--D--) (--root--) (--UniqueID 3--)
./folder1/folder11 (--D--) (--./folder1--) (--UniqueID 4--)
./folder1/folder11/file3 (--F--) (--./folder1/folder11--) (--UniqueID 5--)
./folder1/folder11/file4 (--F--) (--./folder1/folder11--) (--UniqueID 6--)
./folder1/file2 (--F--) (--./folder1--) (--UniqueID 7--)
./folder1/file\ 1 (--F--) (--./folder1--) (--UniqueID 8--)
./folder4 (--D--) (--root--) (--UniqueID 9--)
./folder4/file5 (--F--) (--./folder4--) (--UniqueID 10--)
./folder3 (--D--) (--root--) (--UniqueID 11--)

# 7  
Old 04-01-2011
I might have misunderstood you but...

Using

Code:
find . -ls |awk -F'.' 'NR>1{print $NF}'

Gives the dirs and extensions of files.

Second
Code:
find . -ls |
awk -F'.' 'NR>1{print ($1~/dr/)?"D."$NF:"F."$NF}' |
awk -F'./' 'NF==2&&/^D/{print substr($0,2),"(--D--)","(--root--)","(--UniqueID "NR"--)"}
NF>2&&/^D/{print substr($0,2),"(--D--)","(--"gensub(/D(\.\/.*)\/.*/,"\\1",1,$0)"--)","(--UniqueID "NR"--)"}
/^F/{print  substr($0,2),"(--F--)","(--"gensub(/F(\.\/.*)\/.*/,"\\1",1,$0)"--)","(--UniqueID "NR"--)"}'

gives error
Code:
awk: calling undefined function gensub
 input record number 1, file 
 source line number 3

 
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. Shell Programming and Scripting

Compare two files and print using awk

I have 2 files: email_1.out 1 abc@yahoo.com 2 abc_1@yahoo.com 3 abc_2@yahoo.com data_1.out <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td... (1 Reply)
Discussion started by: sol_nov
1 Replies

5. Shell Programming and Scripting

AWK help print dirs with files in it

Hi, I'm writing some start of day checks for my work. I want to check some dirs for files that have been created longer than 10 mins ago and not been transfered. I've already used a find command to write a list of files that meet this criteria to a log called sod.log i.e. ... (1 Reply)
Discussion started by: elcounto
1 Replies

6. Shell Programming and Scripting

use awk print statement for two files

Hello, is there a way to use the awk print statement on two files at once? I would like to take two columns from one file, and one column from another file and print them as consecutive columns to a third file. Seems simple...as in: file 1 1 a 2 b 3 c 4 d 5 e file 2 1 t 2 u... (3 Replies)
Discussion started by: HugoHuhn
3 Replies

7. Shell Programming and Scripting

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 ... (7 Replies)
Discussion started by: grajp002
7 Replies

8. Shell Programming and Scripting

Print to 2 files in awk if statement

Hi all, I have some code like this awk -F, '{ if ($1==3) print $2 > "output_file" print "1" > "new_file" }' "input_file" When I check output_file this has the correct values in it. However the new_file has 1 in it for every line in the input_file. If the input file has 20 lins then... (2 Replies)
Discussion started by: Donkey25
2 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

awk print fields to multiple files?

I am trying to print the output of a command to two separate files. Is it possible to use awk to print $1 to one file and $2 to another file? Thanks in advance! (1 Reply)
Discussion started by: TheCrunge
1 Replies
Login or Register to Ask a Question