ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
PU12829,24869;PD15733,24869;PD15733,19785;PD12829,19785;PD12829,24869;
PU4599,20915;PD9924,20915;PD9924,18898;PD4599,18898;PD4599,20915;
PU12829,24869;PD15733,24869;PD15733,19785;PD12829,19785;PD12829,24869;
PU4599,20915;PD9924,20915;PD9924,18898;PD4599,18898;PD4599,20915;
PU1723,3423; #this line is ignored to short
Code:
cp $HOME/hpgl-hot-folder/temp.plot $HOME/hpgl-hot-folder/temp2.plot
sed -i '/SP2;/,$d' $HOME/hpgl-hot-folder/temp2.plot
OBJECT=OBJECT + int(1)
while [ "sed -i '/^PU;/d' $HOME/hpgl-hot-folder/temp2.plot > $HOME/hpgl-hot-folder/$OBJECT" ]
do
OBJECT=$(($OBJECT + 1))
done
What I'm trying to do is while true, cut each line from file that begins with PU and thats longer than 12 characters and write to a increasing numbered file for each line. Stating with object1 etc...
I am not exactly sure what you are trying to accomlish, but this
Code:
OBJECT=OBJECT + int(1)
doesn't seem right. You will also need to clarify if the semicolon has any special meaning, i.e. do only lines qualify for exclusion that have more than 12 cahracters up to the first occurrence of the semicolon? I am wondering because you included it in your regex.
Anyway, try this
If that doesn't meet the requirement than you will have to post some more representative sample data and the output you expect.
[EDIT]
You can (probably) also curb the first sed statement in your script and do it all with one sed. I am not sure if you really need the -i option since this will leave you with a file that contains all the lines of your newly created scripts. It is redundant.
Your while loop is all wrong. Shell loops check for the exit status of the command used, usually test. In this case it's only trying to check for the existence of the command string, and not iterating over the file itself.
In any case, this sounds like something that can be done entirely in bash to me, as long as the file isn't gigantic. But I'm not sure I fully understand the requirements. Are you trying to keep the lines starting with "PU", or remove them? Your description implies that you want to keep those lines, but sed -i '/^PU;/d' deletes them.
Assuming you want to keep the PU lines, you can do something like this:
Code:
#!/bin/bash
file=file.txt
OBJECT=1
while read line ; do
if [[ "${line:0:2}" = "PU" && ${#line} -gt 12 ]]; then
echo "${line}" > "$OBJECT"
OBJECT=$(( OBJECT + 1 ))
fi
done <"$file"
If you want to remove the PU lines, simply negate the first part of the test:
Code:
if [[ ! "${line:0:2}" = "PU" && ${#line} -gt 12 ]]; then
BTW, what is "+ int(1)" doing there? I don't believe that's proper shell syntax.
Last edited by David the H.; 01-01-2011 at 02:48 PM.
Reason: fixed a bad bracket
perfect....
but thats the first half of an awk script that if you don't mind you could take a look at.
Attached is the launch file
contours.sh
it sends the sections or PU lines you helped me get separated to the minmax.awk script that determines the minmax of xy that passes it along to conv-contours.sh (this is the big problem one and is commented)
When all is done I'm trying to get descending contours inside of the objects that were separated in the first shell question based upon an input variable and continue in a loop with contours getting smaller till a reasonable level.
As you can see in this test svg file the inside of the "e" "a" and "f" will be treated as contour areas also. Is the a way to calculate if the coordinates all fall inside another object it can be ignored?
Can you give an example of the input data and what output you expect exactly? Also elaborate, if the long lines are to be kept or deleted. Your initial example indicates that you only want to keep the short lines. However, the solution you adapted now indicates the contrary. I also do not understand what your awks are exactly supposed to accomlish.
Can you elaborate a bit more on what you mean by getting 'descending conrours'? Sounds like some sort of graphics manipulation but I just don't get the big picture.
Have to say I am with crts in being fairly confused here. I have looked at all 3 attached files and am at a loss to comprehend how they work.
For starters, looking at minmax.awk you have a variable (cmd) that is never used and a call to next that appears to not serve any value?
Your on the fly awk script creation in contours.sh looks to mimic minmax.awk but fails to include the FS and RS settings. So maybe the file it is acting on is
setup differently???
And the last file, conv-contours.sh, should really just be an awk script:
Code:
# Instead of this at top of bash script
CONTOUR_SPACING="$(sed -n '54p' $HOME/bin/camm2.defaults |sed 's/Contours=//')" ##for example use 3
CONTOUR_SPACING=$(($CONTOUR_SPACING * 40)) ## 3mm equals 120 plotter units
# add the following in (g)awk script
BEGIN {
while(getline line < "camm2.defaults"){
if(line ~ /Contours/){
split(line,arr,"=")
break
}
print line
}
CONTOUR_SPACING = arr[2] * 40
RS="[;\n\r]";
FS="[, \t]+";
}
Those scripts ignore anything but PU and PD commands. I did have the last script as an awk only but thought I needed to add those extra shell commands but will remove them.
The long PU/PD lines are to be extracted and deleted from the original file as it's only a temp. Once those snipits are in the numbered files they get piped through these three script files to produce contours. The example that needs these scripts applied can be found below (no line wraps) it should be one long line when pasted on your computer.
attached is the original script(contours.sh) to split attached temp.plot file into objects (gawk ' error)
then object file (1-?) gets piped through the other scripts one object at a time applying descending contours till filled and then gets the next object file eg:#2 and so on. I know these are all screwed up now but I've been hacking away at this for so long that it's starting to become a blur. For my first code experiment I should have picked another mp3 player not this 3500 line python monster! I do have a lot of people getting and using it and it is free but this is one feature request I'd like to add.
Attached is drawing.png and the oval with red contours is what I'd like to end up with and each object in the drawing represents the split object files initially done on the hpgl file resulting files 1 to ?
I ran contours.sh with the original temp.plot file it found ok but in contours.sh it refers to conv-contours.sh is that right or is it .awk? As .sh it produces code in temp-contours.plot but it appears to be the same as the original? Is it taking each numbered file 1,2,3, etc that gets split initially and applying decending contours to those and then putting them all in temp-contours.plot? Running ./contours.sh < 1
yields what looks like the original file also.
Thanks again for your help
Gary
By the way, could you please put your text data inside [code][/code] tags too? The long lines force my screen to scroll horizontally, making the thread hard to read.
Sorry gary ... in my playing around I forgot to place back the locations you originally had
Try this new contours.sh ... notice that conv-contours.txt is not really a script file so this is as good as any extension
I would also probably not place it in the BIN directory as it is not an executable, but rather a stub
which I don't know if $SPACING will work in a "stub"?
I don't see any ref. to processing the split up objects files named 1 to xx those are the objects that need contouring like in the pic attached a few posts back. I'm guessing that max x and max y will have to be /2 and if new x is larger than max x /2 it get $SPACING subtracted and if smaller it gets $SPACING added in order to place the contour inside the original outline.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.