2

I am looking to do this in python or a basic shell script.

I have a file with multiple entries that I would like to manipulate its data and store them in variables.

The file has rows with multiple columns. The first column is a person's name (i.e., Joe, Mary, etc). The second (after the comma) is an ID. I would like to store each ID into a variable and then construct some links as shown below. The problem is that one name can have only one ID or multiple, as you can see below:

Joe, 21142 21143 21909 24125
Mary, 22650 23127
John, 24325
Mike, 24683 24684 26973

How can I store each value in the "second column" into a variable so I can then construct links like this:

http://example/Joe/21142
http://example/Joe/21143
http://example/Joe/21909 
http://example/Joe/24125
http://example/Mary/22650 
http://example/Mary/23127

Thank you in advance!

  • Omar

5 Answers 5

1

can be done with GNU awk

awk -F'[, ]+' '{for (i=2; i<=NF; ++i) print "http://example/"$1"/"$i }' input.txt
http://example/Joe/21142
http://example/Joe/21143
http://example/Joe/21909
http://example/Joe/24125
http://example/Mary/22650
http://example/Mary/23127
http://example/John/24325
http://example/Mike/24683
http://example/Mike/24684
http://example/Mike/26973

Or in Python

s = '''Joe, 21142 21143 21909 24125
Mary, 22650 23127
John, 24325
Mike, 24683 24684 26973
'''
from StringIO import StringIO
from contextlib import closing
with closing(StringIO(s)) as f:
    for line in f: 
            x, y = line.split(',')
            x = x.strip()
            y = y.strip().split()
            leader = 'http://example/{}'.format(x)
            print '\n'.join('{}/{}'.format(leader, z) for z in y)
Sign up to request clarification or add additional context in comments.

Comments

1

bash answer: the read command operates line-wise over the file and grabs comma-or-whitespace-separated words into an array

while IFS=$', \t' read -ra words; do
    for ((i=1; i<${#words[@]}; i++)); do
        printf "http://example/%s/%s\n" "${words[0]}" "${words[i]}"
    done
done < file

Comments

0

First off since you are reusing the url, its better to create a reusable template. Next since a name could have many ids, you need to run another loop inside the main loop to generate each url. Below code should work.

url_template = "http://example/%s/%d"
with open("input.file") as f:
    for line in f:
        name  = line.split(',')[0].strip()
        n_ids = line.split(',')[1].strip().split(' ')
        for n_id in nids:
            print url_template % (name, nid)

2 Comments

Thanks! Unfortunately I get this error: $ python test.py Traceback (most recent call last): File "test.py", line 2, in <module> with open("test.file") as f: IOError: [Errno 2] No such file or directory: 'test.file'
hey, you have to put your filename there. What is your filename? Did you even understand my code ?
0

Try

myfile = open('input','r')
link = dict()
for line in myfile:
    line = line.split(",")
    IDs = line[1].split()
    link[line[0]]=IDs
myfile.close()

for name in link.keys():
    for ID in link[name]:
        print ''.join(["www.whatever.com/",name,"/",ID])

Comments

0

I guess i'm late to this party, might as well share:

lines  = '''Joe, 21142 21143 21909 24125
Mary, 22650 23127
John, 24325
Mike, 24683 24684 26973'''

    linesList = lines.split("\n")
    for line in linesList:
        lineList = line.split(",")
        lineName = lineList[0];
        lineNumbers = lineList[1].split(" ")
        for lineNumber in lineNumbers:
            if lineNumber.isdigit():
                print("http://example.com/" + lineName + "/" +lineNumber)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.