English 中文(简体)
用 a子将价值重置在固定位置档案中
原标题:Replacing a value in a fixed-position file using awk

这里要求将职位9-12改为空白,例如,在所有固定规模的记录中。

I 旧

cat file | awk  {sub(substr($0,9,12),"   ",$0);print} 

工作,除非职位上有一个空白 9. 要求指出这种限制。

cat file | awk -F"*"  {sub(substr($0,9,12),"   ",$0);print} 

无帮助。 这里的右yn是什么?

最佳回答

Sample file:

$ cat file
1234567890123456
1234567 90123456
1234567890123456
1234567 90123456
1234567890123456

1 D-1, 1 D-1, 1 D-1, 1 D-1, 1 P-5, 1 P-4, 1 P-3, 1 P-2, 1 GS 想法:

awk  { print substr($0,1,8) "    " substr($0,13) }  file

<>0>NOTES:

  • this replaces each of the 9th-12th positions with a space (ie, replace the 4 fields with a total of 4 spaces)
  • if the intention is to replace the 4 fields with a single space then replace the " " with " "

这产生了:

12345678    3456
1234567     3456
12345678    3456
1234567     3456
12345678    3456

利用同一想法积极处理这些立场(在评论中类似于Cyrus建议):

awk -v start=9 -v end=12  { print substr($0,1,start-1) sprintf("%*s",(end-start+1)," ") substr($0,end+1) }  file

这也产生了:

12345678    3456
1234567     3456
12345678    3456
1234567     3456
12345678    3456
问题回答

利用GNU AWK

$ awk -v from=9 -v to=12  BEGIN{FS=OFS=""}{for(i=from;i<=to;i++) $i=" "}1  file




相关问题
Split a text file in PHP

How can I split a large text file into separate files by character count using PHP? So a 10,000 character file split every 1000 characters would be split into 10 files. Further, can I split only after ...

term clustering library?

Does anybody know an open-sourcefree library that does term clustering? Thanks, yaniv

Unicode Strings in Ruby 1.9

I ve written a Ruby script that is reading a file (File.read()) that contains unicode characters, and it works fine from the command line. However, when I try to put it into an Automator Workflow (...

Extract key sentences from a text

do you know about an effective method for extracting key sentences from a text with their frequency parameters, etc and that can also do "stemming" (search also for similar sentences) ? I wonder also ...

Getting word count for all files within a folder

I need to find word count for all of the files within a folder. Here is the code I ve come up with so far: $f="../mts/sites/default/files/test.doc"; // count words $numWords = str_word_count($str)/...

How to read, in a line, all characters from column A to B

is it possible in Python, given a file with 10000 lines, where all of them have this structure: 1, 2, xvfrt ert5a fsfs4 df f fdfd56 , 234 or similar, to read the whole string, and then to ...

热门标签