tags:

views:

484

answers:

1

I want to have -[space] as an field separator in AWK.

For instance,

awk -F-[space] {' print $1 '}

How can you have many characters as a field separator in AWK?

[edit]

The exact output of Vlad's command

$echo /Users/Sam/Dropbox/Education/Chemistry/Other\ materials/*.pdf | sed -e 's: : - :g'
/Users/Sam/Dropbox/Education/Chemistry/Other - materials/CHE_IB_LAB.pdf - /Users/Sam/Dropbox/Education/Chemistry/Other - materials/Lecture19_20_21.pdf

The exact output of Vlad's command with sed

$echo /Users/Sam/Dropbox/Education/Chemistry/Other\ materials/*.pdf
/Users/Sam/Dropbox/Education/Chemistry/Other materials/CHE_IB_LAB.pdf /Users/Sam/Dropbox/Education/Chemistry/Other materials/Lecture19_20_21.pdf
+5  A: 

Know your quoting :) , but be aware that GNU awk's -F takes an extended regular expression (ERE) as its argument so you may also need to escape accordingly if you want a textual match as opposed to an ERE match, e.g.

awk -F '- ' '{ print $1 }'

UPDATE

Your latest comment is still unclear.

  1. What are the full paths of the PDF files on your disk
  2. what is the exact output you require from those files?

I need a concrete example, e.g.

  1. for the following files:

    ../Phy/Phy1/file1.pdf
    ../Phy/file2.pdf
    ../Che/Che1/file3.pdf
    ../Che/file4.pdf
    ../file5.pdf
    ../Phy2/file6.pdf
    
  2. I want to display:

    PhyPhy1 file1.pdf - Phy file2.pdf - Che/Che1 file3.pdf - Che file4.pdf
    

Please note that, in the case of the example files above, the command:

echo lpr ../{Che,Phy}/{*.pdf,*/*.pdf}

will only display:

lpr Che/file4.pdf
lpr Che/Che1/file3.pdf
lpr Phy/file2.pdf
lpr Phy/Phy1/file1.pdf

Let's get this part right first, then we'll worry about the dash etc.


UPDATE

OK. Please run one or both of the following almost equivalent commands:

echo Dropbox/Mas/edu/{Phy,Che}/*.pdf | sed -e 's: : - :g'
ls -1d Dropbox/Mas/edu/{Phy,Che}/*.pdf | paste -s -d '|' - | sed -e 's:|: - :g'

Then please edit your original post and add the following, separated:

  1. the exact output of the above command(s) (copy-paste)
  2. the exact output of the above command(s) altered by you to suit your need

Cheers, V.

vladr
I cannot get the whitespace to work as a field separator. Do you see any mistake in the following code $echo lpr /../{Che,Phy}/{*.pdf,*/*.pdf} | awk -F ' - ' '{ print $1 }' ?
Masi
awk -F '- ' '{ print $1 }' works just fine for me...
David Zaslavsky
t prints me just the first occurrence. I would like to print all occurrences. Perhaps, my mistake is in the $1. Does it mean print all occurences?
Masi
print $1 prints the first column... what are you trying to achieve exactly?
vladr
are you perchance trying to insert a dash between the pdf filenames? if so then use sed to replace spaces with e.g. space-dash-space: $echo a b c d | sed -e 's: : - :g'
vladr
@Vlad: I am trying to dipslay pdf filenames as a list, instead of a row, by AWK.
Masi
as what kind of list? comma-separated? one file name per row? provide an accurate example of the desired output please. e.g. to list one file per row you can do "ls -1 /../{Che,Phy}/{*.pdf,*/*.pdf}" or "for file in /../{Che,Phy}/{*.pdf,*/*.pdf} ; do echo $file ; done"
vladr
The items in the list are separated by the mark -. An example is PhyPhy1 FilePath1 - Phy2 Filepath2 - CheChe1 filepath1 - Che2 filepath
Masi
1. the full path is Dropbox/Mas/edu/{Phy,Che}/*.pdf2. The output needs to be in the list form. It looks now like Path1 - Path2 - Path3 - Pathn
Masi
Please see my last update.
vladr
What does the following mean sed -e 's: : - :g'? My attempt: You try to append...
Masi
sed -e 's: : - :g' will replace all occurrences of " " with " - ". Now, just what I was afraid of: your path has spaces in it, so you must use the SECOND command (with 'ls -1d'.) Thank you for adding the commands' output, but you have not said whether it's OK as it is or, if not, what must change.
vladr
in sed's 's: : - :g', s is for substitute, g is for global. You could have also written this as 's/ / - /g' or 's# # - #g' or 's| | - |g' - you get the hang of it. See 'man sed' or http://unix.compufutura.com/sedawk/ch02_03.htm for a more detailed discussion.
vladr
Masi
@Masi, when you say that ls does not work, what is the exact error or output? Also, "echo ..." will output everything on one line, and there is no way to distinguish between the spaces naturally occurring inside the filenames, and spaces separating different file names
vladr
"ls -1d" does what echo cannot do, i.e. output one filename per line (with \n"), and "paste" will glue all lines together separated by the single character passed to -d (e.g. |) which can then be sed'ed away to a multi-character separator
vladr
If "paste" is the problem on your system then you can also do: ls -1d Dropbox/Mas/edu/{Phy,Che}/*.pdf | tr '\n' '|' | sed -e 's:|*$::' -e 's: : - :g'
vladr
tr will replace all "\n" characters with '|', almost like paste, except that the last "\n" (after the last file) will also be replaced and has to be trimmed with 's:|*$::' (all | at the end of a line replaced with blanks), which was not an issue with "paste"
vladr