PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Wednesday, August 17, 2022

[FIXED] How to fetch the 13th and the second last line from the standard output

 August 17, 2022     bash, line, linux, output     No comments   

Issue

I have a script that outputs messages to the standard output. Now I need to fetch the 13th line from the beginning, and the second last line from this output.

I was wondering if this could be done via a single command rather than redirecting the output to the file and then reading through it few times to pull my output.

line 1
line 2
...
line 12
line 13
line 14
...
...
line N-1
line N

I need to fetch the output like:

line 13
line N-1

I have separate commands to get my lines.

To fetch the 13th line:

scriptoutput | head -n 13

To fetch the N-1 line:

scriptoutput | tail -n 2 | head -n 1
OR
scriptoutput | sed 'x;$!d'

But being standard output, I am not able to get the output done in one command.


Solution

You never know which line is "second to last" when reading a stream until you read the actual last line and the stream ends. The only possible way, is to always buffer the previous line, and when the stream ends, output the line from the buffer.

For example with sed:

$ seq 20 | sed -n '13p;${x;p};h'
13
19

etc. with similar tools.

seq 20 | awk 'NR==13{print} {last=current; current=$0} END{print last}'

seq 20 | { n=1; while IFS= read -r line; do if ((n++ == 13)); then printf "%s\n" "$line"; fi; previous=$current; current=$line; done && printf "%s\n" "$previous"; }

But you can also just buffer the whole output and count the number of lines and output the second to last line....

# with a file
seq 20 > tempfile ; sed -n "13p;$(( $(wc -l < tempfile) - 1 ))p" file
# with memory
seq 20 | { buf=$(cat); sed -n "13p;$(( $(wc -l <<<"$buf") - 1 ))p" <<<"$buf"; }


Answered By - KamilCuk
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, July 29, 2022

[FIXED] How to add dotted or dashed line to png in Python?

 July 29, 2022     image, line, png, python     No comments   

Issue

Hello I want a draw a dashed or dotted line to png, I couldn't find How can I do that, Can someone help ?

im = Image.new('RGB', (2000,2000),tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))) print("...Saving...") im.save('C:\\Users\\th3m1s\\Desktop\\Lejant\\'+str(legend_code)+'.png', quality=100) Result is click here


Solution

Have you considered creating a new image with vertical lines as well as horizontal lines, slightly taller and wider than your original image, on which you paste your original image? That way you will have a dotted border and it works for every size.

This can be done as explained here: How do you composite an image onto another image with PIL in Python?

from PIL import Image,ImageDraw
#this is your own image
yourimage = Image.open('/home/vancha/Documenten/python/pillu/square.png', 'r')
img_w, img_h = yourimage.size
border_width = 5
#this is the new image which holds the stripes
borderimage = Image.new('RGBA', (2000+(border_width * 2), 2000+(border_width *2)), (255, 255, 255, 255))


# Draw the lines
draw = ImageDraw.Draw(borderimage)
#starts drawing vertical lines form the very top
start = 0
end = borderimage.height#width or height, doens't matter since the image is square
step_size = border_width*4

#starts from border_width * 2, so that the very top and very left aren't made black with lines
for x in range(border_width*2, borderimage.width, step_size):
    vertical_line = ((x, start), (x, end))
    #the width is the thickness of the "dots" in the border
    draw.line(vertical_line, fill=(0,0,0),width=border_width * 2)

    horizontal_line = ((start,x), (end, x))
    draw.line(horizontal_line, fill=(0,0,0),width=border_width *2)

#for good practice:
del draw


bg_w, bg_h = borderimage.size
#calculate the offset so that the image is centered
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)

#paste your old image over the one with the dots
borderimage.paste(yourimage, offset)

#save it wherever you want :)
borderimage.save('./border.png')

In your case, if you want your border to be 5px all the way around your image, and your image is 2000,2000, changing the size of the new image to be 2010 by 2010 leaves you with 5px to spare on both sides if you paste your own image in the center.



Answered By - Vancha
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, May 18, 2022

[FIXED] How to create a line space in a Rails partial view?

 May 18, 2022     line, partial, ruby-on-rails, views     No comments   

Issue

In my index.html.erb view, I have the following render to display each and every flight (without doing a .each:

<%= render @flights %>

However, as I have the flights partial written, all the flights are listed side-by-side. What I want is one flight listed per line. How do I create a line space after each flight?

<%= radio_button_tag :flight_id, flight.id %>
<%= flight.id %>
<%= flight.date.strftime("%B %d, %Y")  %>
<%= flight.date.strftime('%I:%M:%S %p') %>
<%= flight.from_airport.code %>
<%= flight.to_airport.code %>
<%= distance_of_time_in_words(flight.duration) %>

Solution

Why not use tables?

<table>
  <%= render @flights %>
</table>

and in your partial write

<tr>
  <td><%= radio_button_tag :flight_id, flight.id %></td>
  <td><%= flight.id %></td>
  <td><%= flight.date.strftime("%B %d, %Y")  %></td>
  <td><%= flight.date.strftime('%I:%M:%S %p') %></td>
  <td><%= flight.from_airport.code %></td>
  <td><%= flight.to_airport.code %></td>
  <td><%= distance_of_time_in_words(flight.duration) %></td>
</tr>

Arrgghhhh so much typing, and so error-prone. For educational purposes, why not use haml?

%table
  = render @flights

in your partial:

%tr
  %td= radio_button_tag :flight_id, flight.id 
  %td= flight.id 
  %td= flight.date.strftime("%B %d, %Y")  
  %td= flight.date.strftime('%I:%M:%S %p') 
  %td= flight.from_airport.code 
  %td= flight.to_airport.code 
  %td= distance_of_time_in_words(flight.duration) 


Answered By - nathanvda
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, May 16, 2022

[FIXED] How can I access MySQL via the command line for EasyPHP DevServer

 May 16, 2022     command, easyphp, line, mysql     No comments   

Issue

I am using a book and its example uses a different program.

Basically I have this entered:

C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin -u root

And I get this error:

"'EasyPHP-DevServer-14.1VC11\binaries\mysql\bin' is not recognised as an internal or external command, operable program or batch file."

I've looked around online and haven't found many things, the things I did find require some tinkering around and I don't want to mess something up by accident.. to be honest I'm not entirely sure what I'm doing here. I mean I know the basics of the command line but I don't know how to use it for mySQL. So could someone please assist me? Thanks.


Solution

Probably you have missed to write mysql after ".....\bin".
You should try as following:

C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin\mysql -u root

Or
add C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin to path environment variable and just use command like bellow:

mysql -u root


Answered By - mmuzahid
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing