Q.) Explain the output :
cat /proc/loadavg
25.72 23.19 23.35 42/3411 43603
Bonus Q.) Where else do you see the first 3 data frequently ?
Q.) You need to use any combination of linux commands using pipe in a single line to just filter out the IP addresses ( eg. 192.x.x.x and 127.0.0.1 ) from the ifconfig or ip a s command.
Q.) A file contains a string “Chetan-Tiwary” . Your task is to replace the “-” with “:” ( without using vim/vi or echo or any text editor that has an interface) so that the string becomes “Chetan:Tiwary”.
Level - L2 and above.
I'll be posting a series of Linux-related questions covering various skill levels. Feel free to share your insights and expertise. Your contributions will benefit learners at all stages, from those in current roles to those preparing for Linux interviews.
Bonus question of the first question: Where else do you see the first 3 data frequently ?
Answer: I'm going to take a wild guess - the output ot the uptime command.
@Trevor great job ! you've got to give me one more command and you use it frequently!
That 3rd question is screaming at me as well, so I won't be able to ignore
it either:
A file contains a string “Chetan-Tiwary” . Your task is to replace the “-” with “:” ( without using vim/vi or echo or any text editor that has an interface) so that the string becomes “Chetan:Tiwary”.
To satisfy this request, that will involve my old friend - the sed utility. Yes, I referred
to it as a utility because to me it's always been much more than just a command.
Okay, I won't get bogged down in that - call it a command if you must, that won't
change it's utility!!!
Here's the "command" that will be needed to accommodate the question:
$ sed -i 's/Chetan-Tiwary/Chetan:Tiwary/g' filename
I promise you, that every occurrence of the string "Chetan-Tiwary" that exists in
"filename", will be replaced with the string "Chetan:Tiwary".
Okay, that's all for this episode of "Replace That String". Thanks Chetan for
all the wonderful batting practice!!!
If you answer at this pace, very soon I will be out of questions !
sometimes I have seen, the interviewer out of his/her sheer experience wants to test the candidates especially if they are beginners like L1 & L2 level , whether they know some stream editors basics or manipulative programming scripts like awk ( just the basics ). It is always recommended to have an idea about these 2 tools before you appear for a Linux interview.
Recommended learning resources : https://www.redhat.com/sysadmin/manipulating-text-sed
https://www.redhat.com/sysadmin/beginners-guide-gawk
We have a nice interactive learning commentary on awk here : https://learn.redhat.com/t5/Expert-Extra-Videos/Expert-Extra-Video-Parsing-Output-with-AWK/ba-p/3505...
For
Q.) You need to use any combination of linux commands using pipe in a single line to just filter out the IP addresses ( eg. 192.x.x.x and 127.0.0.1 ) from the ifconfig or ip a s command.
one can use
ip addr show | sed -n -e 's;^.*inet \([^/]\+\).*$;\1;p'
@TM What a beautiful command, looks like schrodinger wave equation!
Can you give a more simplified command for beginners using grep, awk etc so that they will learn the utilities and how to use them for filtering.
Thanks for sharing your knowledge/ experience with the community!
Hi @Chetan_Tiwary_ ,
... Well, I do think that just one command, here indeed sed, is simpler than several grep awk with pipes.
I will try to explain it below.
The command ip addr show outputs several lines among which ony the ones containing "inet " are needed. Remark the space at the end of "inet " that excludes the lines with inet6 (that gives IPv6 addresses)
Now the command sed -n -e 's;^.*inet \([^/]\+\).*$;\1;p' , behave as
-n prevent it to print out anything, unless instructed to, and it will be instructed at the end
-e is not really required here. I am just used to put it in order to not have to add it if I need to add more sed subcommands.
s;;; is used here instead of the standard s/// in order to not have to back-slashed the / required later.
Now s;^.*inet \([^/]\+\).*$;\1; identifies a line that starts with anything (^.*), then has inet followed by a space (inet ), then idenitifies and captures anything but a slash (\([^/]\+\)), finally followed by anything till the end (.*$). All that found line is then replaced by what had been captured that is not a slash (;\1;). After the substition is done, just print the line as it is now (
I hope it clarifies, and does not still look as Schrodinger wave equation. Otherwise we might have then deal with his cat, and we cannot even know if the Schrodinger cat is alive or dead
Regards,
Tshimanga
@TM ok very well, I do have a simpler combination that one can use to filter out the IP for the task :
The first one had the subnet mask, which I cut out in the second combination using cut with a / delimiter and then extracting the first field i.e IP address.
For learners, they can see the use of grep, egrep, awk and cut and how can they combine those to get a filtering task done!
If you dont want to use "cut" ad continue with "awk" in the last part, you can do this as well :
it is doing the same task as "cut" in the previous example.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.