I am trying to write an awk command to take certain fields out of one file and write them to a new file. I am able to to separate the fields (comma delineate) and print it to stdout but I can't figure out how to get it into the new file in the correct format. Here is my current command:
我试图写一个awk命令从一个文件中取出某些字段并将它们写入一个新文件。我能够分隔字段(逗号描述)并将其打印到stdout但我无法弄清楚如何以正确的格式将其放入新文件中。这是我当前的命令:
$ awk '{FS =","};{print $3}' test.log > test1.log
This puts the third field of each line in the new file but inserts 2 blank lines at the beginning of the new file and then a blank line in between each field. I would also like the keep the comma separators but can't figure that out ether. I'm fairly new to bash and awk so any help is appreciated!
这会将每行的第三个字段放在新文件中,但在新文件的开头插入2个空行,然后在每个字段之间插入一个空行。我也想保留逗号分隔符,但无法想象以太。我是bash和awk的新手,所以任何帮助都表示赞赏!
Here a chunk of my sample data:
这是我的样本数据的一大块:
10.60.3.109, anonymous, 12/1/2003, 6:56:20, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3007]USER, anonymous, -,
10.60.3.109, -, 12/1/2003, 6:56:20, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 530, 1326, [3007]PASS, IEUser@, -,
10.60.3.109, anonymous, 12/1/2003, 6:56:20, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3008]USER, anonymous, -,
10.60.3.109, -, 12/1/2003, 6:56:20, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 530, 1326, [3008]PASS, IEUser@, -,
10.60.3.109, anonymous, 12/1/2003, 6:56:20, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3009]USER, anonymous, -,
10.60.3.109, -, 12/1/2003, 6:56:20, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 530, 1326, [3009]PASS, IEUser@, -,
10.60.3.109, sally, 12/1/2003, 6:56:26, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3010]USER, sally, -,
10.60.3.109, -, 12/1/2003, 6:56:26, MSFTPSVC1, CSS, ipaddr, 16, 0, 0, 530, 1326, [3010]PASS, -, -,
10.60.3.109, sally, 12/1/2003, 6:56:31, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3011]USER, sally, -,
10.60.3.109, -, 12/1/2003, 6:56:31, MSFTPSVC1, CSS, ipaddr, 31, 0, 0, 530, 1326, [3011]PASS, -, -,
10.60.3.109, sally, 12/1/2003, 6:56:36, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3012]USER, sally, -,
10.60.3.109, -, 12/1/2003, 6:56:36, MSFTPSVC1, CSS, ipaddr, 15, 0, 0, 530, 1326, [3012]PASS, -, -,
10.60.3.109, sally, 12/1/2003, 6:56:40, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3013]USER, sally, -,
10.60.3.109, sally, 12/1/2003, 6:56:40, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 230, 0, [3013]PASS, -, -,
10.60.3.109, sally, 12/1/2003, 6:56:40, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3014]USER, sally, -,
10.60.3.109, sally, 12/1/2003, 6:56:40, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 230, 0, [3014]PASS, -, -,
10.60.3.109, sally, 12/1/2003, 6:56:47, MSFTPSVC1, CSS, ipaddr, 0, 0, 0, 331, 0, [3015]USER, sally, -,
1 个解决方案
#1
2
awk '{FS =","};{print $3}' test.log > test1.log
The first action is {FS = ","}
and it is being executed for each line. It should probably be preceded by BEGIN
. Because the field separator is currently not set until after the first line is already read and split, the first line is most likely read as a single field, and hence $3
is empty, leading to the blank line.
第一个动作是{FS =“,”},它正在为每一行执行。它可能应该在BEGIN之前。由于字段分隔符当前未在第一行已被读取和拆分之后设置,因此第一行很可能被读取为单个字段,因此$ 3为空,从而导致空白行。
The second action is ;
, which prints the entire line doesn't seem to do anything. You don't normally use a semicolon like that; it normally only appears inside the braces of an action.
第二个动作是;,打印整行不似乎做任何事情。你通常不会使用这样的分号;它通常只出现在动作的大括号内。
The third action is {print $3}
, which prints the 3rd field (when there is a third field to print).
第三个动作是{print $ 3},它打印第3个字段(当有第三个字段要打印时)。
I'm not yet sure I understand the double blank line at the beginning of your output, unless there's a blank line at the beginning of your input. Nor am I reproducing the alternating blank lines, this using the BSD awk
from Mac OS X 10.10.1 Yosemite, but also using GNU awk
3.1.7.
我还不确定我理解输出开头的双空行,除非输入开头有空行。我也没有使用Mac OS X 10.10.1优胜美地的BSD awk再现交替的空白行,但也使用GNU awk 3.1.7。
You probably want:
你可能想要:
awk 'BEGIN {FS = ","} {print $3}' test.log > test1.log
or (preferable in my view):
或(我认为最好):
awk -F"," '{print $3}' test.log > test1.log
where the quotes around the comma are optional (since comma is not a shell metacharacter). But if you're later going to be selecting multiple fields, you probably want to do as jaypal singh suggested in a comment, and use:
逗号周围的引号是可选的(因为逗号不是shell元字符)。但是如果你以后要选择多个字段,你可能想像jaypal singh在评论中建议的那样做,并使用:
awk 'BEGIN {OFS = FS = ","} {print $3, $5, $9}' test.log > test1.log
for whatever permutation of the fields you want printed.
无论您想要打印哪些字段的排列。
#1
2
awk '{FS =","};{print $3}' test.log > test1.log
The first action is {FS = ","}
and it is being executed for each line. It should probably be preceded by BEGIN
. Because the field separator is currently not set until after the first line is already read and split, the first line is most likely read as a single field, and hence $3
is empty, leading to the blank line.
第一个动作是{FS =“,”},它正在为每一行执行。它可能应该在BEGIN之前。由于字段分隔符当前未在第一行已被读取和拆分之后设置,因此第一行很可能被读取为单个字段,因此$ 3为空,从而导致空白行。
The second action is ;
, which prints the entire line doesn't seem to do anything. You don't normally use a semicolon like that; it normally only appears inside the braces of an action.
第二个动作是;,打印整行不似乎做任何事情。你通常不会使用这样的分号;它通常只出现在动作的大括号内。
The third action is {print $3}
, which prints the 3rd field (when there is a third field to print).
第三个动作是{print $ 3},它打印第3个字段(当有第三个字段要打印时)。
I'm not yet sure I understand the double blank line at the beginning of your output, unless there's a blank line at the beginning of your input. Nor am I reproducing the alternating blank lines, this using the BSD awk
from Mac OS X 10.10.1 Yosemite, but also using GNU awk
3.1.7.
我还不确定我理解输出开头的双空行,除非输入开头有空行。我也没有使用Mac OS X 10.10.1优胜美地的BSD awk再现交替的空白行,但也使用GNU awk 3.1.7。
You probably want:
你可能想要:
awk 'BEGIN {FS = ","} {print $3}' test.log > test1.log
or (preferable in my view):
或(我认为最好):
awk -F"," '{print $3}' test.log > test1.log
where the quotes around the comma are optional (since comma is not a shell metacharacter). But if you're later going to be selecting multiple fields, you probably want to do as jaypal singh suggested in a comment, and use:
逗号周围的引号是可选的(因为逗号不是shell元字符)。但是如果你以后要选择多个字段,你可能想像jaypal singh在评论中建议的那样做,并使用:
awk 'BEGIN {OFS = FS = ","} {print $3, $5, $9}' test.log > test1.log
for whatever permutation of the fields you want printed.
无论您想要打印哪些字段的排列。