Reputation: 23
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!
Upvotes: 2
Views: 342
Reputation: 246877
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
Upvotes: 1
Reputation: 23364
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)
Upvotes: 1
Reputation: 7399
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)
Upvotes: 0
Reputation: 1788
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])
Upvotes: 0
Reputation: 73638
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)
Upvotes: 0