Unable to set many characters to the field separator in AWK

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

Upvotes: 2

Views: 2165

Answers (1)

vladr
vladr

Reputation: 66661

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

Upvotes: 5

Related Questions