Tips for Shell

Source: Internet
Author: User

Tips for Shell

There'll is some case-based tips for Shell.

1. Replace The sequence space with newline

Input Example

a b  c   d

Output Example

abcd
Summary

sedis the best solution.

Solution
sed -r -e ‘s/[[:space:]]/\n/g‘
2. Sort the unordered word list (with duplication) by frequencysummary

It is very useful when counting word frequency.

Solution
sort | uniq -c | sort -rn
3. AWK print line without first columnsummary

This command actually clear the first column from the file.

Solution
awk ‘{$1=""; print $0}‘
4. Show the longest line in the Filesummary

Taking advantage of the const in lengthawk

Solution
awk ‘{print $0" "length}‘ | sort -k2 -nr | head -1
5. Ignore line, match some patternsummary and solution

Using

grep -v
6. batchly Replace Namespacesummary

Using to loop over the file and to for sed perform the replacement.

Solution
for f in `find ./src -type f`; do sed -i ‘s/oldnamespace/newnamespace‘ $f; done
7. Generate, ..., sequencesummary

PerlCan be a better solution.

Solution
perl -e ‘for{$i=2;$i<=24;$i++}{printf("%02d\n", $i)}‘
8. Command Line Calculatorawk Solution
awk ‘{print 1/10}‘

and additional enter is needed

Perl Solution

More flexiable (with formatting).

perl -e ‘printf("%f\n", 1./10);‘

Tips for Shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.