2

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"
2
$ cat file
"COL1"|"COL2"|"COL3"|"COL4"|"COL5"
"A1"|"B1"|"C1"|"D1"|"E1"
"A2"|"B2"|"C2"|"D2"|"E2"
"A3"|"B3"|"C3"|"D3"|"E3"

1)

$ awk '{printf "\"%s\"|%s\n", NR==1 ? "ID" : NR-1, $0}' file
"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) rearrange the output of 1)

$ awk '{printf "\"%s\"|%s\n", NR==1 ? "ID" : NR-1, $0}' file |\
    awk 'BEGIN{FS=OFS="|"} {print $2,$3,$1,$4,$5,$6}'
"COL1"|"COL2"|ID|"COL3"|"COL4"|"COL5"
"A1"|"B1"|"1"|"C1"|"D1"|"E1"
"A2"|"B2"|"2"|"C2"|"D2"|"E2"
"A3"|"B3"|"3"|"C3"|"D3"|"E3"

2) or directly awk

$ awk 'BEGIN{FS=OFS="|"} {print $1,$2,NR==1 ? "ID" : "\""NR-1"\"",$3,$4,$5}' file
"COL1"|"COL2"|ID|"COL3"|"COL4"|"COL5"
"A1"|"B1"|"1"|"C1"|"D1"|"E1"
"A2"|"B2"|"2"|"C2"|"D2"|"E2"
"A3"|"B3"|"3"|"C3"|"D3"|"E3"
0

Although you tagged this question with awk and sed, python can do this same job just as good. The script below has two functions to address each of your questions. Comment/uncomment them as necessary. Note, that the script takes input file as first positional argument on command line and writes to a temp file. Uncomment last line in main function to actually edit the original file .

Script Source

#!/usr/bin/env python
import sys
import os

def prepend_column(input,output):
    for index,line in enumerate(input):
        if index == 0:
            output.write(line)
            continue
        output.write('"' + str(index) +'"|' + line)

def alter_column(input,output):
    for index,line in enumerate(input):
        if index == 0: 
            output.write(line)
            continue
        vals = line.split('|')
        vals[2] = '"' + str(index) + '"'
        output.write('|'.join(vals))

def main():
    temp = '/tmp/temp_text'
    with open(sys.argv[1]) as f:
         with open(temp,'w') as t:
            # alter_column(f,t)
            prepend_column(f,t)
    # uncomment line below to actually replace original file
    # os.rename(temp,sys.argv)

if __name__ == '__main__': main()

Demo.

Appending column:

bash-4.3$ python process_columns.py data.txt 
bash-4.3$ cat /tmp/temp_text
"COL1"|"COL2"|"COL3"|"COL4"|"COL5"
"1"|"A1"|"B1"|"C1"|"D1"|"E1"
"2"|"A2"|"B2"|"C2"|"D2"|"E2"
"3"|"A3"|"B3"|"C3"|"D3"|"E3"

Altering a column:

bash-4.3$ python process_columns.py data.txt 
bash-4.3$ cat /tmp/temp_text
"COL1"|"COL2"|"COL3"|"COL4"|"COL5"
"A1"|"B1"|"1"|"D1"|"E1"
"A2"|"B2"|"2"|"D2"|"E2"
"A3"|"B3"|"3"|"D3"|"E3"
  • Whoever downvoted my answer, please explain yourself. The answer works and addresses the question , so I don't see a valid reason for this downvote – Sergiy Kolodyazhnyy Oct 14 '16 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.