I’m running into some issues reading from a text file (again!) and I was wondering if you guys could shed any light?
I have the above file as a text file “output.txt” and I would like to be able to read each line in and do something with it. i.e the “15182486” is a item number that I want to open up and do something with.
Once this is done, I want to add some data back to this file. The item number may refer to multiple IP addresses in another file, which it should then add to a new file, appending the information from the old file.
Flow is as follows-
Read line.
Pull item number from file eg. “15182486”
With that number do some logic to go and find the information from this item, which will be multiple IP Addresses.
Add those IP Addresses to another file, appending the other information.
So, say the item number “15182486” refered to three IP Addresses- 192.168.0.1, 192.168.0.2, 192.168.0.3
The new file should contain:
192.168.0.1,R123,15182486
192.168.0.2,R123,15182486
192.168.0.3,R123,15182486
THEN it should move onto the next item and continue to add this to the file. So you could end up with something like:
192.168.0.1,R123,15182486
192.168.0.2,R123,15182486
192.168.0.3,R123,15182486
10.0.1.2,R456,15147199
10.0.1.3,R456,15147199
17.232.123.1,R789,13820697
I am getting hideously stuck acheiving this, with nested if’s and reads/writes.
If anyone has any tips they can give me I would be greatful as the tips and walkthroughs on this site are only getting me so far.
You should really improve your workflow, and use a spread sheet or something.
Anyways, I wrote a bash script that you’ll save and execute in a backup folder of the files you want to process.
You save it as fixit.bash, chmodu u+x fixit.bash in the Terminal window, and then you issue the command below:
cat output.txt |./fixit.bash >result.txt
Here is the script, I assume some familiarity with a Terminal window, just ask if there is something you wonder about!
#!/bin/bash
IFS=','
while read first second ; do
if test -s $second ; then
cat $second | /usr/bin/sed -n '$! s/$/,'"$first"','"$second"'/p'
else
echo $first,$second
fi
done
I know, I am too lazy to look up the join command today.
Edit
I realized that not every itemnumber has an accompanying file, and updated the shell script accordingly, so that it in that case echoes, the two fields in the original file.
If I’ve understood correctly then this should get you close:
set newTxtFile to (path to desktop as string) & "NewOutput.txt"
set theData to every paragraph of (read (choose file))
repeat with thisLineOfData in theData
set thisItem to my getItemNumber(thisLineOfData)
----add your routine for finding the data----
--For now I'll assume you return a list of ip's like this
set ipList to {"192.168.0.1", "192.168.0.2", "192.168.0.3"}
----end of your finding routine----
repeat with thisIP in ipList
set newData to thisIP & "," & thisLineOfData
do shell script "echo " & quoted form of newData & " >> " & quoted form of POSIX path of newTxtFile
end repeat
end repeat
on getItemNumber(theText)
set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set theText to theText's text items
set AppleScript's text item delimiters to oldDels
return item 2 of theText
end getItemNumber