简体   繁体   English

如何使用AWK附加文本

[英]How to attach text using AWK

Sorry if i may have missed this out while searching on SO. 对不起,如果我在搜寻SO时可能错过了这一点。 I have a text file with the following structure 我有一个具有以下结构的文本文件

AAAA21346A  
AAAA21346A  
AAAA21346A  
AAAA21346A 
.  
.  
.  

till more than 5 lakhs records (half a million records). 直到超过50万记录(一半记录)。

I want to attach ^N^1 in front of every row using awk . 我想使用awk在每行的前面附加^ N ^ 1。 How can it be done? 如何做呢?

Update: 更新:

My output has to look like this: 我的输出必须如下所示:

AAAA21346A^N^1
AAAA21346A^N^1
AAAA21346A^N^1
AAAA21346A^N^1

and so on till EOF. 直到EOF。 There has to be a line break after every record. 每条记录后都必须有一个换行符。

** some more explanation * ** * ** 更多说明 * ** *
I am using db2 and linux and hence i thought of Awk. 我正在使用db2和linux,因此我想到了Awk。 What i am trying to do is, i need to insert the data into table with 3 columns using "load from" command in db2 and using a ^ as delimiter. 我想做的是,我需要使用db2中的“ load from”命令并使用^作为分隔符,将数据插入3列的表中。 But Since the text file contains only one column and i want to attach ^N^1 to make it into 3 column. 但是由于文本文件只包含一列,我想附加^ N ^ 1使其变成3列。 i hope yall understand now. 我希望你们现在都明白。

What you want appears to be very easy: 您想要的东西看起来很简单:

awk '{print $0 "^N^1"}' data_file

The only problem with adding thread IDs is deciding on the algorithm for doing so. 添加线程ID的唯一问题是确定执行此操作的算法。 For example, alternating is very easy too: 例如,交替也很容易:

awk '{printf "%s^N^%d\n", $0, (NF % 2) + 1}' data_file

Here are a couple more ways to do it using AWK: 以下是使用AWK的几种其他方法:

awk 'BEGIN { ORS = "^N^1\n"} 1' inputfile

or, more explicitly: 或者,更明确地说:

awk 'BEGIN { ORS = "^N^1\n"} {print}' inputfile

or 要么

awk 'BEGIN { OFS = "^"} {$2 = "N"; $3 = 1}1' inputfile

or 要么

awk 'BEGIN { OFS = "^"} {$2 = "N"; $3 = 1; print}' inputfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM