Awk命令,用于更改具有多个输出的文件中的字母

时间:2018-12-15 11:02:31

标签: python parsing awk

我有一个看起来像这样的输入文件:

button1_Click

我还有另一个文件,该文件的位置要更改为我想更改的字母,例如:

// better idea is to have this as class property,
// than creating it everytime you want to use it
Random r = new Random();
// Here you could use one of methods: AddSeconds, AddMinutes, etc.,
// Choose one that fits best. Also you could choose othe base date
// than DateTime.MinValue
DateTime d = DateTime.MinValue.AddSeconds(r.Next());

label1.Text = d.ToString();

(实际上大约会有10,000个字母更改)

我想为每个文本更改提供一个单独的输出文件,该文件应如下所示:

input.txt

THISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

下一个:

textpos.txt

Position    Text_Change
1           A
2           B
3           X

下一个:

output1.txt

AHISISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT

我想学习如何以awk命令和pythonic方式进行此操作,并且想知道什么是最快,最快的方法?

谢谢。

3 个答案:

答案 0 :(得分:2)

请尝试以下操作(考虑到实际的Input_files中将包含相同类型的数据)。由于我要关闭var str;代码中的输出文件,因此该解决方案应注意错误 var c = 0; var a; var b; var str; function counter() { return c++; } $(document).ready(function () { var url = "https://graph.facebook.com/v3.2/..."; $.getJSON(url, function (data) { var items = []; $.each(data.data, function (i, obj) { //$('#target').append($('<div/>', { id: 'dummy' + i })) var text = '<p class="review_text">' + obj.review_text + '</p>' var date = '<p class="date">' + obj.created_time + '</p>' a = counter(); $("#carousel").find("[data-index='" + i + "']").append(text, date) }); $('#scripts').append('<input type="number" id="spam_key" value= ' + a + '>'); element = document.getElementById('spam_key'); if (element !== null) { str = element.value; } else { str = null; } var wrapper = document.getElementById("carousel"); var myHTML = ''; for (b = 0; b <= str; b++) { myHTML += '<div id="review" data-index=' + (b) + '></div>'; } wrapper.innerHTML = myHTML }); });

Too many open files error while running awk command

3个名为awkawk ' FNR==NR{ a[++count]=$0 next } FNR>1{ close(file) file="output"(FNR-1)".txt" for(i=1;i<=count;i++){ if($1==1){ print $2 substr(a[i],2) > file } else{ print substr(a[i],1,$1-1) $2 substr(a[i],$1+1) > file } } }' input.txt textpos.txt output1.txt的输出文件及其内容如下。

output2.txt

说明: 在此处添加上述代码的说明。

output3.txt

答案 1 :(得分:1)

使用gawk:

for (String line = bufferedReader.readline(); line != null; line = bufferedReader.readLine()) {
    System.out.println(line);
    stringBuilder.append(bufferedReader.readLine());
}

答案 2 :(得分:1)

使用awk,滥用String expression = "@name('EPA1') insert into EPA1 select * from SensorEvent where sensor11 >= 1"; String expression2 = "@name('EPA2') insert into EPA2 select * from SensorEvent where sensor12 >= 1"; String expression3 ="@name('EPA3') select * from EPA1#time(100) as epa1, EPA2#time(200) as epa2 where epa1.id = epa2.id"; EPStatement statement = epService.getEPAdministrator().createEPL(expression); EPStatement statement2 = epService.getEPAdministrator().createEPL(expression2); EPStatement statement3 = epService.getEPAdministrator().createEPL(expression3); MyListener listener = new MyListener(); statement.addListener(listener); statement2.addListener(listener); statement3.addListener(listener); 作为第二个文件,使每个字母成为其自己的列:

FS=""

另一个使用$ awk ' NR==FNR { a[$1]=$2; next } # hash positions and letters to a { for(i in a) # for all positions $i=a[i] # replace the letters in them }1' textpos FS="" OFS="" file ABXSISANEXAMPLEOFANINPUTFILEWITHALONGSTRINGOFTEXT forsubstra[]到char来构建变量char的方法:

$0