Original: Coding diary (wechat official ID: Codelogs), welcome to share, reprint please reserve the source.
Introduction to the
When I was a Linux newbie with Linux command proficiency on my resume, I was stunned when an interviewer asked me how to quickly view the 200th line of a file, just that line.
The interviewer was probably smirking at my perfunctory response that he had a good idea of how good I was at Linux.
However, I have to say that this is a very good interview question, for someone who only knows the basic command CD ls cp rm, completely impossible to answer, and for someone who is really familiar with Linux, the answer is quite handy, let’s take a look.
1. If you know that cat + -n can print the line number:
cat -n temp.log | grep -w '^\s*200'
Copy the code
Yes, start each line with a line number and grep to filter it out.
2. If you know anything about the sed command:
cat temp.log | sed -n '200p'
Copy the code
The sed command can be used to directly match the line number and output the matched line with the p command.
3. If you know anything about the AWK command:
cat temp.log | awk 'NR==200'
Copy the code
This is similar to the sed command, except NR stands for line number.
4. If you’re lucky enough to have learned a little Perl:
cat temp.log|perl -lne 'print $_ if $.==200'
Copy the code
It is similar to AWK, but the syntax is different.
Head,tail, head,tail,tail
cat temp.log | head -n 200 | tail -n1
Copy the code
For example, use tail to fetch the last row from the first 200 rows.
6. If you know cut, you should take a turn.
cat temp.log | cut -z -d $'\n' -f200
cat temp.log | awk -F '\n' -v RS='\ 0' '{print $200}'
Copy the code
Well, think of the entire file as a record, think of the newline as a column separator, and just pick the 200th column.
7. If you’re wondering if XARgs will work:
cat temp.log | xargs -d'\n' bash -c 'echo $200' -
Copy the code
This is similar to the above, except this time use Xargs to turn all the lines into arguments to the bash command, and then print the 200th argument.
Note: There is no detailed explanation of how to use each of these commands. If you want to know more about these commands, you can check out the two previous articles I wrote. Linux text command Tips (part 1)
conclusion
Yes, the use of Linux commands, to achieve a function, there can be many solutions, it and ordinary programming thinking is very different, as long as you do not limit the way of thinking in programming implementation, gradually solutions will be like inspiration, from your brain.
Content of the past
Mysql timestamp: timestamp (‘ timestamp ‘, ‘timestamp’); Hex, Base64,urlencode encoding scheme comparison character encoding solution