head and tail trick

OK @techbulk, if this doesn´t work either I want your friends to sign up this very forum and solve it in front of us:

Code:
[gvisoc@vao Downloads]$ head -c 4 passwd > /tmp/dirt.txt | tail -c +56 passwd | head -c 2 > /tmp/yB.txt | tail -c 3 passwd > /tmp/it.txt | tail -q /tmp/dirt.txt /tmp/yB.txt /tmp/it.txt
DirtyBit
[gvisoc@vao Downloads]$
 
Last edited:


OK @techbulk, if this doesn´t work either I want your friends to sign up this very forum and solve it in front of us:

Code:
[gvisoc@vao Downloads]$ head -c 4 passwd > /tmp/dirt.txt | tail -c +56 passwd | head -c 2 > /tmp/yB.txt | tail -c 3 passwd > /tmp/it.txt | tail -q /tmp/dir.txt /tmp/yB.txt /tmp/it.txt
DirtyBit
[gvisoc@vao Downloads]$

0h, wow... That's an impressive amount of spaghetti.
 
hello gentlemen, may i propose this solution :3 ? i registered just to post this. i hope the post formatting is correct.
tail -c 3 passwd | head -qc 57 passwd - | tail -c 5 | head -qc 4 passwd -
 
i registered just to post this.

LOL Welcome aboard!

If I was smarter than I am, I'd suggest we do these sort of challenges once a week. Alas, I'd be unable to think of challenges.
 
hello gentlemen, may i propose this solution :3 ? i registered just to post this. i hope the post formatting is correct.
tail -c 3 passwd | head -qc 57 passwd - | tail -c 5 | head -qc 4 passwd -
Nice solution! This could be improved furthur,
Given that my ASCII file isn't terminated by a new line,
Code:
echo -n "DirticaVduURPjDwfEkzjTAmPbDRelukkuraPhedgdnrKSXlIJgMrKayBqPTTFdngQXoCxJZMImwMrhFidkixjLSAwnqkhdYgWlkbwjEEtwBdZaJhakSbHQYVBknit" > passwd

Then my soln would be

Bash:
head -qc +57 passwd | tail -qc2 - passwd | head -qc 4 passwd -

Note: I took ideas from your solution
Note 2: Use the echo command, passwd must not contain a \n at the end of the file.
 
Last edited:
LOL Welcome aboard!

If I was smarter than I am, I'd suggest we do these sort of challenges once a week. Alas, I'd be unable to think of challenges.
thanks. i absolutely agree with you. i would be one of the first people to participate because i love bash so much.
Given that my ASCII file isn't terminated by a new line,
damn i can't believe i forgot to make sure that it wasn't there...
Then my soln would be

Bash:
head -qc +57 passwd | tail -qc2 - passwd | head -qc 4 passwd -
i wonder what the organizers' opinion is since both of our solutions rely on a newline either being or not being at the end of the file.
 
i would be one of the first people to participate because i love bash so much.

There's nothing stopping you from organizing these contests.

I'm sure the admins would be on board and love the content.
 
There's nothing stopping you from organizing these contests.
except for the fact that i'm not any smarter than you are lol. however, for starters, i do think that there should be a thread to discuss how we might go about organizing the bash challenges. i bet that there are probably a few members that can at least provide us with helpful suggestions
 
hello gentlemen, may i propose this solution :3 ? i registered just to post this. i hope the post formatting is correct.
tail -c 3 passwd | head -qc 57 passwd - | tail -c 5 | head -qc 4 passwd -
That's a very good one. What is the explanation behind these trailing hyphens? Where I can learn from them?

007f8445-df60-44df-99e5-a78d3cf4a8e4_text.gif


I think that's the most elegant solution to the problem, I was trying to solve some of my shortcomings using temporary files, which uses a lot more resources than those.

EDIT: I find some information about those hyphens in stack exchange, they mean that we're telling the command to go and get data from the standard input (the pipe on the left in that pipeline) instead of suppliyng an argument. That's something I wasn't aware that could be done, and as we see is super powerful. Kudos to @mahiro_oyama!
 
Last edited:
What is the explanation behind these trailing hyphens?
the hyphens basically tell the command to get its input from the output of the previous command.
Where I can learn from them?
i don't know. i didn't learn what i know about them all from one place. i accumulated it over time. and, this actually depends on whether the program you're using supports it or not. not all programs support piping the output of a command into them, so you could start by reading the manual of the command you want to use the hyphens with i guess :P. you could also ask google since this is not an obscure feature
 
Last edited:
And it keeps giving.

This being Linux, there's almost always more than one way to solve a problem.

I'd undertake some regular contests if I was capable and had the free time to devote to something regular. I already give the Linux community a bunch of my time.

It'd be great if someone stepped up to the challenge.
 
i do think that there should be a thread to discuss how we might go about organizing the bash challenges.

This could go a number of ways.

There could be a challenge a week, perhaps with a small prize.

There could be a challenge a week but only those who solve the problem get to compete in the next challenge, like a tournament. Again, perhaps with a small prize.

There could be a person who runs the thing and people can submit challenges to that person (and then not compete in said challenge).

There could be a really smart person who is able to consistently create new and interesting challenges.

There's room for challenges outside of bash specifically.

There are lots of ways for it to go.

As mentioned in an above post, I already give the Linux community an exceptional amount of time. (Participating here is just one of my self-imposed obligations.)

So, I will (almost certainly) not be the one to organize this. I suppose I could contribute to the prizes.
 
This could go a number of ways.

There could be a challenge a week, perhaps with a small prize.

There could be a challenge a week but only those who solve the problem get to compete in the next challenge, like a tournament. Again, perhaps with a small prize.

There could be a person who runs the thing and people can submit challenges to that person (and then not compete in said challenge).

There could be a really smart person who is able to consistently create new and interesting challenges.

There's room for challenges outside of bash specifically.

There are lots of ways for it to go.

As mentioned in an above post, I already give the Linux community an exceptional amount of time. (Participating here is just one of my self-imposed obligations.)

So, I will (almost certainly) not be the one to organize this. I suppose I could contribute to the prizes.
i think i'll start a new thread that's simply about weekly challenges to see what the language the members would like to see the most, who might be interested in coming up with challenges and discuss the topic in more detail for example. how about that?
 
I'm really keen to find out the solution, my bash-fu stops there!
Best I've found so far:
Code:
head -c 4 passwd > part1 | tail -c +56 passwd | head -c 2 > part2 | tail -c 3 passwd > part3 | head -q part1 part2 part3

Works at times, but not always, so I don't think this is an optimized solution.. But maybe we're getting there!
 
Top