Add an incrementing identity column to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Add an incrementing identity column to a file
# 1  
Old 10-12-2016
Add an incrementing identity column to a file

Two questions:
1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number?
for example:
(Current file has headers and is PIPE delimited.)

**data looks like this**
"COL1"|"COL2"|"COL3"|"COL4"|"COL5"
"A1"|"B1"|"C1"|"D1"|"E1"
"A2"|"B2"|"C2"|"D2"|"E2"
"A3"|"B3"|"C3"|"D3"|"E3"

**would like for it to be like**
"ID"|"COL1"|"COL2"|"COL3"|"COL4"|"COL5"
"1"|"A1"|"B1"|"C1"|"D1"|"E1"
"2"|"A2"|"B2"|"C2"|"D2"|"E2"
"3"|"A3"|"B3"|"C3"|"D3"|"E3"

2) Is there a way to replace an existing column with a New column for an existing file containing data. The new column would be "ID" and populate as an auto incrementing number?
for example:
(Current file has headers and is PIPE delimited.)
**data looks like this**
"COL1"|"COL2"|"COL3"|"COL4"|"COL5"
"A1"|"B1"|"C1"|"D1"|"E1"
"A2"|"B2"|"C2"|"D2"|"E2"
"A3"|"B3"|"C3"|"D3"|"E3"

**would like for it to be like**
"COL1"|"COL2"|"ID"|"COL4"|"COL5"
"A1"|"B1"|"1"|"D1"|"E1"
"A2"|"B2"|"2"|"D2"|"E2"
"A3"|"B3"|"3"|"D3"|"E3"

Hoping someone can help provide a solution for both scenarios. I have tried so many methods (i.e. awk, sed, ect) with no luck.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Incrementing the New File Version Number

Hi, This is my first post here. I am using cygwin on Windows 7. I am starting with a data file with filename "name_1.ext", like "20180831_snapgenotypes_1.csv". The "_1" before ".ext" is a version number. Integers (0-99) are sufficient. They don't have to be like "1.0.0". The filename may... (2 Replies)
Discussion started by: minimalist
2 Replies

2. UNIX for Advanced & Expert Users

Add an incrementing identity column to a file

Two questions: 1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number? for example: (Current file has headers and is PIPE delimited.) **data looks like this** "COL1"|"COL2"|"COL3"|"COL4"|"COL5"... (4 Replies)
Discussion started by: timlisa20
4 Replies

3. UNIX for Dummies Questions & Answers

Incrementing the New File Version Number

Hello All, In the below script i am trying to check and list the file names, get the last file with highest version number and then increment the version number when i create another file. Example: file1 is COBANK_v1.xml and file2 i want to put it as COBANK_v2.xml, to achieve this i am using awk... (15 Replies)
Discussion started by: Ariean
15 Replies

4. Shell Programming and Scripting

Change numbers in a file, incrementing them

Hello, Here's a file of mine: key1:431 key2:159 key3:998 I need to change these keys to something bigger - and I actually need to shift them all by a range of 3. The output would be: key1:434 key2:162 key3:1001 I can't find the propper sed/awk line that would alter all my... (4 Replies)
Discussion started by: fzd
4 Replies

5. Shell Programming and Scripting

Auto incrementing a particular column values

Hi All, I have the input file(.csv) like below. SNo,Step,Data 1,0,1 2,0,2 3,1,1 4,2,2 5,0,3 6,0,4 and I need the following output. 1,0,1 2,0,2 3,1,1 4,2,2 5,3,3 6,3,4 (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

incrementing lines in the file & format output.

Hi All, I need read the file and out put format as below using ksh, I wrote below script its keep on repeating first line in the file. may i know the best way to get the below out put while incrementing line in the file. cat b.txt |awk '{print $0}' |while read line do aa=`cat $line |head -1... (7 Replies)
Discussion started by: ashanabey
7 Replies

7. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

8. Shell Programming and Scripting

Port incrementing using one file

Hi I wrote this script with the help of you guyz.My next challenge is to increment the port using single file.So far iam using this code to increment hp1 and hp2 .To increment port numbers, iam using two different files (.default_port_hp1 and .default_port_hp2).The challenge for me is to use... (0 Replies)
Discussion started by: coolkid
0 Replies
Login or Register to Ask a Question