Script: only converts long rows.

Source: Internet
Author: User

Script: only change a long row. For example, scan a file quickly to see which rows are too long: 1awk '{if (length ($0)> 72) another interesting method of {print $0} 'filename is to use the $ # vamname structure in shell. It returns the length of any variable that replaces vamname (content, not a variable name ). 01 #! /Bin/sh02 03 # toolong. sh -- use fmt. sh to format long lines that exceed the specified length 04 05 width = 7206 07if [! -R "$1"]; then08 echo "Usage: 'basename $ 0' filename "> & 209 exit 110fi11 12 while read input13do14 if [$ {# input}-gt $ width]; then15 echo" $ input "| fmt. sh16 else17 echo "$ input" 18 fi19done <$120 21 exit 0 is an interesting method to process input files. First, use a simple <$1 to achieve the purpose of the input file, and then use a read input to parse each line. If your shell environment does not have the $ {# var} notation, you can use the following method instead: 1 varlength = "$ (echo" $ var "| wc-c) "However, the wc command has a very annoying feature, and its output has a leading space to make the output list more beautiful. To avoid this annoying problem, there will be a slight change, that is, when the last pipe is passed, only the number is allowed through: [NOTE: In my Linux, I have not found this feature mentioned by the author and may be faulty when sed is used, you need to test] 1 varlength = "$ (echo" $ var "| wc-c | sed's/[^: digit:] // ') in your own environment. "Running result: 01 $ toolong ragged.txt 02So she sat on, with closed eyes, and half believed herself in03Wonderland, though she knew she had but to open them again, and04all wocould change to dull reality -- the grass wocould be only rustling05in the wind, and the pool rippling to the waving of the reeds -- the06rattling teacups wocould change to tinkling sheep-bells, and the07Queen's shrill cries to the voice of the shepherd boy -- and the08sneeze09of the baby, the shriek of the Gryphon, and all the other queer10noises, wocould change (she knew) to the confused clamor of the busy11farm-yard -- while the lowing of the cattle in the distance would12take the place of the Mock Turtle's heavy sobs.13 14 Notice that, unlike a standard invocation of fmt, toolong has retained line breaks where possible, so the word "sneeze," 15 which is on a line by itself in the input file, is also on a line by itself in the output.

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.