我有一个以下格式的文本文件
4 This is my test file 4500
5 This is test 6000
6 Not sure how it will work 9000
I want to extract data as follows
Field1 = 4
Field2 = This is my test file
Field3 = 4500
这是第一行,我想要这种格式的所有行。有人可以帮忙吗?我想做sed或awk no perl。我更喜欢sed和/或awk。
我遇到Field2问题(可能是单个字或多个字符串),用单引号或双引号括起来。休息我觉得很容易。请帮忙
答案 0 :(得分:4)
使用sed
...
sed -re 's/(\S+)\s+(.*)\s+(\S+)/Field1 = \1\nField2 = \2\nField3 = \3/g' file
输出:
Field1 = 4
Field2 = This is my test file
Field3 = 4500
Field1 = 5
Field2 = This is test
Field3 = 6000
Field1 = 6
Field2 = Not sure how it will work
Field3 = 9000
答案 1 :(得分:4)
几乎总是很容易想出一个适用于给定样本输入集的“解决方案”,但更难以提出一个工作期间的解决方案。在选择“解决方案”之前,请真正考虑您的实际可能输入。如果输入中的字段少于3个,则可能无法生成所需的输出,如果可能更新了样本输入和预期输出,则显示您希望如何处理。
$ awk '{
f2=$0
gsub(/^[^[:space:]]+[[:space:]]+|[[:space:]]+[^[:space:]]+$/,"",f2)
print "field1 =", $1
print "field2 =", f2
print "field3 =", $NF
}' file
field1 = 4
field2 = This is my test file
field3 = 4500
field1 = 5
field2 = This is test
field3 = 6000
field1 = 6
field2 = Not sure how it will work
field3 = 9000
答案 2 :(得分:0)
这不完美,但您可以尝试使用此awk
:
awk '{s=$1;e=$NF; $1=$NF=""; gsub(/^ +| +$/, "");
printf "f1=<%s>,f2=<%s>,f3=<%s>\n", s, $0, e}' file
f1=<4>,f2=<This is my test file>,f3=<4500>
f1=<5>,f2=<This is test>,f3=<6000>
f1=<6>,f2=<Not sure how it will work>,f3=<9000>