简体   繁体   中英

awk on windows - print fields - quotes as delimiter

this is the feed file,

dfscmd /map "\\SERVER24\dfs\shared\can\Group Pension\Quality Assurance Auditors" "\\serverfile16\bugp-qaaud$" ""
dfscmd /map "\\SERVER24\dfs\userhome\serverfile52_d" "\\serverfile52\userhome_d" ""
dfscmd /map "\\SERVER24\dfs\shared\can\Wealth Management\WM CSC Stats - Support Team, CRT Team & CSR Performance" "\\serverfile48\bucan-WM_CSC_Support_&_CRT_Team_Stats" ""

Using this line

gawk -v FS=""" "{print $2 \";\" $4 }" a.txt

I am able to print but I cannot save the output,

C:\>gawk -v FS=""" "{print $2 \";\" $4 }" a.txt   >b.txt
\\SERVER24\dfs\shared\can\Group Pension\Quality Assurance Auditors;\\serverfile16\bugp-qaaud$
\\SERVER24\dfs\userhome\serverfile52_d;\\serverfile52\userhome_d
\\SERVER24\dfs\shared\can\Wealth Management\WM CSC Stats - Support Team, CRT Team & CSR Performance;\\serverfile48\bucan-   WM_CSC_Support_&_CRT_Team_Stats
gawk: (FILENAME=a.txt FNR=3) fatal: cannot open file `>b.txt' for reading (Invalid argument)

Single quotes should always be used to surround awk 's braces. This would be better:

 gawk -v FS=\" '{ print $2 ";" $4 }' a.txt > b.txt

Contents of b.txt :

\\SERVER24\dfs\shared\can\Group Pension\Quality Assurance Auditors;\\serverfile16\bugp-qaaud$
\\SERVER24\dfs\userhome\serverfile52_d;\\serverfile52\userhome_d
\\SERVER24\dfs\shared\can\Wealth Management\WM CSC Stats - Support Team, CRT Team & CSR Performance;\\serverfile48\bucan-WM_CSC_Support_&_CRT_Team_Stats

EDIT:

If you're using windows, you may also like to consider running a script like this:

gawk -f script.awk a.txt > b.txt

Contents of script.awk :

BEGIN { FS="\"" }
{ print $2 ";" $4 }

This avoids having to wrap quotes around your expression and produces the results you require. HTH.

gawk -v FS=""" "{print $2 \";\" $4 }" a.txt   ">b.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM