Sample Logs
Location Number Status Comment
Delhi 919xxx Processed Test File 1
Mumbai 918xxx Got Stucked Test File 123
I'm trying to add one tab space after Status using AWK, but getting error.
我正在尝试使用AWK在Status之后添加一个选项卡空间,但是收到错误。
Sample Query
awk '{$3 = $3 "\t"; print}' z
Getting Output As
获得输出
Location Number Status Comment
Delhi 919xxx Processed Test File 1
Mumbai 918xxx **Got** **Stucked** Test File 123
As it is taking 'Got Stucked' as multiple fields please suggest.
因为它将'Got Stucked'视为多个领域,请建议。
3 个解决方案
#1
2
If you only want one tab after the header text Status
to make it look better, use sub
to the first record only:
如果您只想在标题文本状态之后使用一个选项卡使其看起来更好,请仅使用sub到第一个记录:
$ awk 'NR==1 {sub(/Status/,"Status\t")} 1' file
Location Number Status Comment
Delhi 919xxx Processed Test File 1
Mumbai 918xxx Got Stucked Test File 123
This way awk won't rebuild the record and replace FS
with OFS
etc.
这样awk就不会重建记录并用OFS等替换FS。
#2
2
@JamesBrown's answer sounds like what you asked for but also consider:
@ JamesBrown的答案听起来像你要求的但也考虑:
$ awk -F' +' -v OFS='\t' '{$1=$1}1' file | column -s$'\t' -t
Location Number Status Comment
Delhi 919xxx Processed Test File 1
Mumbai 918xxx Got Stucked Test File 123
The awk converts every sequence of 2+ spaces to a tab so the result is a tab-separated stream which column
can then convert to a visually aligned table if that's your ultimate goal. Or you could generate a CSV to read into Excel or similar:
awk将2+空格的每个序列转换为制表符,因此结果是制表符分隔的流,如果这是您的最终目标,则该列可以转换为可视对齐的表。或者您可以生成CSV以读入Excel或类似内容:
$ awk -F' +' -v OFS=',' '{$1=$1}1' file
Location,Number,Status,Comment
Delhi,919xxx,Processed,Test File 1
Mumbai,918xxx,Got Stucked,Test File 123
$ awk -F' +' -v OFS=',' '{for(i=1;i<=NF;i++) $i="\""$i"\""}1' file
"Location","Number","Status","Comment"
"Delhi","919xxx","Processed","Test File 1"
"Mumbai","918xxx","Got Stucked","Test File 123"
or more robustly:
或更强大:
$ awk -F' +' -v OFS=',' '{$1=$1; for(i=1;i<=NF;i++) { gsub(/"/,"\"\"",$i); if ($i~/[[:space:],"]/) $i="\""$i"\"" } }1' file
Location,Number,Status,Comment
Delhi,919xxx,Processed,"Test File 1"
Mumbai,918xxx,"Got Stucked","Test File 123"
If your input fields aren't always separated by at least 2 blank chars then tell us how they are separated.
如果您的输入字段并不总是至少用2个空白字符分隔,那么请告诉我们它们是如何分开的。
#3
0
Try using
awk -F' ' '{$3 = $3 "\t"; print}' z
The problem is that awk consider (by default) a single space as the separator between two column. This means that Got Stucked
are actually two different columns.
问题是awk考虑(默认情况下)单个空格作为两列之间的分隔符。这意味着Got Stucked实际上是两个不同的列。
With -F' '
you tell awk to use a double space as the separator to distinguish between two columns.
使用-F''告诉awk使用双空格作为分隔符来区分两列。
#1
2
If you only want one tab after the header text Status
to make it look better, use sub
to the first record only:
如果您只想在标题文本状态之后使用一个选项卡使其看起来更好,请仅使用sub到第一个记录:
$ awk 'NR==1 {sub(/Status/,"Status\t")} 1' file
Location Number Status Comment
Delhi 919xxx Processed Test File 1
Mumbai 918xxx Got Stucked Test File 123
This way awk won't rebuild the record and replace FS
with OFS
etc.
这样awk就不会重建记录并用OFS等替换FS。
#2
2
@JamesBrown's answer sounds like what you asked for but also consider:
@ JamesBrown的答案听起来像你要求的但也考虑:
$ awk -F' +' -v OFS='\t' '{$1=$1}1' file | column -s$'\t' -t
Location Number Status Comment
Delhi 919xxx Processed Test File 1
Mumbai 918xxx Got Stucked Test File 123
The awk converts every sequence of 2+ spaces to a tab so the result is a tab-separated stream which column
can then convert to a visually aligned table if that's your ultimate goal. Or you could generate a CSV to read into Excel or similar:
awk将2+空格的每个序列转换为制表符,因此结果是制表符分隔的流,如果这是您的最终目标,则该列可以转换为可视对齐的表。或者您可以生成CSV以读入Excel或类似内容:
$ awk -F' +' -v OFS=',' '{$1=$1}1' file
Location,Number,Status,Comment
Delhi,919xxx,Processed,Test File 1
Mumbai,918xxx,Got Stucked,Test File 123
$ awk -F' +' -v OFS=',' '{for(i=1;i<=NF;i++) $i="\""$i"\""}1' file
"Location","Number","Status","Comment"
"Delhi","919xxx","Processed","Test File 1"
"Mumbai","918xxx","Got Stucked","Test File 123"
or more robustly:
或更强大:
$ awk -F' +' -v OFS=',' '{$1=$1; for(i=1;i<=NF;i++) { gsub(/"/,"\"\"",$i); if ($i~/[[:space:],"]/) $i="\""$i"\"" } }1' file
Location,Number,Status,Comment
Delhi,919xxx,Processed,"Test File 1"
Mumbai,918xxx,"Got Stucked","Test File 123"
If your input fields aren't always separated by at least 2 blank chars then tell us how they are separated.
如果您的输入字段并不总是至少用2个空白字符分隔,那么请告诉我们它们是如何分开的。
#3
0
Try using
awk -F' ' '{$3 = $3 "\t"; print}' z
The problem is that awk consider (by default) a single space as the separator between two column. This means that Got Stucked
are actually two different columns.
问题是awk考虑(默认情况下)单个空格作为两列之间的分隔符。这意味着Got Stucked实际上是两个不同的列。
With -F' '
you tell awk to use a double space as the separator to distinguish between two columns.
使用-F''告诉awk使用双空格作为分隔符来区分两列。