head and tail trick

techbulk

New Member
Joined
Oct 9, 2023
Messages
9
Reaction score
2
Credits
68
Hi,

I have a file (ASCII text) called passwd which contains the following string: DirticaVduURPjDwfEkzjTAmPbDRelukkuraPhedgdnrKSXlIJgMrKayBqPTTFdngQXoCxJZMImwMrhFidkixjLSAwnqkhdYgWlkbwjEEtwBdZaJhakSbHQYVBknit

I need to extract the first 4 characters of the string, followed by the 2 characters at positions 56 and 57 (counting from 1), and then the last 2 characters of the string. For a total of 8 characters.

I have to do this in one go using only the head and tails commands. Pipes are allowed. The following are not: if, while, for, ;, &, &&, ${}, ||, $(command).

This seems impossible... I know the answer is supposed to be DirtyBit but I haven't been able to get it with this rules.

Any ideas?
Thanks ahead!
 


No, it's for a friendly competition. I've already given up coz I'm not super Linux literate, but I was curious to know the answer.
If this does sound too close to homework, you can remove my question. Wouldn't want to go against any guidelines.
Thanks.
 
It's all good.

If it was homework, we'd patiently help you learn the answer on your own by giving you hints.

We're pretty friendly here.

(I do not have a solution to your problem.)
 
You might look at the "cut" command. There is more than one way.
 
How kind! I'd take a hint any day to be frank.

I've come up with the following but those forbidden characters (semicolons) keep me up at night:
head -c 4 passwd; tail -c +56 passwd | head -c 2; tail -c 2 passwd
 
Last edited:
Well, head --bytes 4 will get you the first four characters.
 
Well, head --bytes 4 will get you the first four characters.
Yup, got it here, thanks for the confirmation!
Code:
head -c 4 passwd; tail -c +56 passwd | head -c 2; tail -c 2 passwd

Only problem is the challenge forbids using semicolons... So back to square one.
 
This is a nugget...

If they'd allow an & or two that'd help.

You need to come back and share the solution. You can't just leave us hanging like this.

Also, wait and be patient. We have some people far more adept than I am. We're spread across the globe, so (and this is a good thing) we're not all in the same timezone. They'll check in when they have time.
 
Yeah, it's a tough one... I'll share the answer when I have it for sure!
And thanks for the tip, I'll check in later, see if someone else has a light bulb moment.
 
The rules don't tell us not to do a script!

Here's the solution according to my understanding:
Bash:
#!/usr/bin/sh
head passwd -c 4
tail -c +56 passwd | head -c 2
tail -c 3 passwd
If we run this in the same directory as the file "passwd", we get the output DirtyBit.

The rationale for the last 2 letters to be taken as 3 characters is the EOL/EOF.

In a script, the output of each command is by default concatenated to the standard output
 
The rules don't tell us not to do a script!

Here's the solution according to my understanding:
Bash:
#!/usr/bin/sh
head passwd -c 4
tail -c +56 passwd | head -c 2
tail -c 3 passwd
If we run this in the same directory as the file "passwd", we get the output DirtyBit.

The rationale for the last 2 letters to be taken as 3 characters is the EOL/EOF.

In a script, the output of each command is by default concatenated to the standard output
Maybe I didn't explain myself too well, but we're to do it in one line, that's why it seems so impossible...
 
If that is in a single bash sentence it should be easy to turn into a function declaration and invocation. I'm now in a meeting, but I'll give it another go in a bit.
 
I got it!

You need to define a function in the bash terminal, and run it, all in the same command.

Code:
[gvisoc@vao Downloads]$ solution () {
> head passwd -c 4
> tail -c + 56 passwd | head -c 2
> tail -c 3 passwd
> } | solution
DirtyBit
[gvisoc@vao Downloads]$

This is how it works:
  • Type the name of the function, parenthesis and an open curly brace, and press enter.
  • This leaves the interpreter open for the user to type the function body, you'll notice that the prompt turns itself into an angle bracket, ">"
  • Type all the sentences, one per line, pressing enter, without semicolon
    • As the sentences are one per line, you can omit the semicolon.
  • The last line } | solution is critical, and here's how it works:
    • The closing curly brace loads the function into your bash session
    • You can pipe the result of defining a function into any command
    • Piping the function over itself, and being the function declared as without parameters, directly invokes it.
 
Last edited:
I guess it's the best I can come up with without typing any of the forbidden bash elements and by sending just one construction to bash. But I'd say that there should be some forking magic to explore if you're allowed to write temporary files.
 
I learned something useful here - had no idea head and tail could be used other than for the first/last few lines of the input. Using tail instead of cut is especially cool because (as far as I know) you need to know the string length to get the last n characters with cut.

I feel like I now need to revisit every filter command that I think I know so well and see what else I might be missing out on.
 
I got it!

You need to define a function in the bash terminal, and run it, all in the same command.

Code:
[gvisoc@vao Downloads]$ solution () {
> head passwd -c 4
> tail -c + 56 passwd | head -c 2
> tail -c 3 passwd
> } | solution
DirtyBit
[gvisoc@vao Downloads]$

This is how it works:
  • Type the name of the function, parenthesis and an open curly brace, and press enter.
  • This leaves the interpreter open for the user to type the function body, you'll notice that the prompt turns itself into an angle bracket, ">"
  • Type all the sentences, one per line, pressing enter, without semicolon
    • As the sentences are one per line, you can omit the semicolon.
  • The last line } | solution is critical, and here's how it works:
    • The closing curly brace loads the function into your bash session
    • You can pipe the result of defining a function into any command
    • Piping the function over itself, and being the function declared as without parameters, directly invokes it.
Bearer of bad news, I'm sooo sorry coz it's impressive as hell that you came up with this, but I'm not allowed to define functions either, it's really supposed to be a one-line thingy. I'm gonna ask to see the rules of the challenge again coz the only way this seems possible is if the semicolons are magically allowed here... Let's pray.
 

Staff online


Top