简体   繁体   中英

How to attach text using AWK

Sorry if i may have missed this out while searching on SO. I have a text file with the following structure

AAAA21346A  
AAAA21346A  
AAAA21346A  
AAAA21346A 
.  
.  
.  

till more than 5 lakhs records (half a million records).

I want to attach ^N^1 in front of every row using awk . 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. 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. 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. But Since the text file contains only one column and i want to attach ^N^1 to make it into 3 column. 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. 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 '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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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