Traverse the file and format the output file (Shell script implementation)

Source: Internet
Author: User
Tags shebang

Background
1. In project development, it is urgent to generate files in the following three formats according to the files under the resource path Res.
Format one:

#define IDR_CEF_0001    101#define IDR_CEF_0002    102...#define     IDR_CEF_0122    222

Format two:
{"about.html", idr_cef_0001},
{"Addprobe.html", idr_cef_0002},
...
{"IMG/HELPIMG/HELP17. PNG ", idr_cef_0122},

Format three:
idr_cef_0001 HTML "res\about.html"
idr_cef_0002 HTML "res\addprobe.html"
idr_cef_0122 HTML "res\img\helpimg\help17. PNG "

"Anxious situation semi-manually implemented as follows"
The 1.c++ implementation gets the file name, outputs it to the TXT document, and copies it to Excel for column processing.
2. Construct a format one left data, combined into a format one data.
3. Combine notepad++ regular expression matching, construct format two or three content, combine into format two or three data.

The disadvantages are obvious:
1. Manual, easy path error, file Less, late bug can not be evaluated.
2. It takes nearly 3 hours to take time.
3. If there is a new document such as (Inner,outer,other) three copies of the resource file, it will need to be operated 3 times.
In short, very silly, very stupid.

"Shell script Implementation"
The source code is as follows:

#!/bin/bashfunctionRead_dir(){ forFileinch' ls $` Doif[- D  $"/"$file]; ThenRead_dir $"/"$fileElseEcho  $"/"$filefi Done}#output FilesTouch Out_files.txtRead_dir"/home/laoyang/test/res"> Out_files.txt#recurse Files#1. Delete the front Path/home/laoyangCat Out_files.txt | Sed' s/\/home\/laoyang\///g '> Out22_files.txt#get Line NumsLinenums= ' Cat Out22_files.txt | Wc- L`Echo $linenums#construct format_files.txt#1. #define IDR_CEF_0001 101RM-RF Format_file1.txt for((i=1; i<=$linenums; i++)) Do Echo "#define IDR_CEF_"${i}$[ -+${i}] >> Format_file1.txt Done#2. {"about.html", idr_cef_0001},Cat Out22_files.txt | Sed' s/test\/res\///g '> Out33_files.txtawk' {print $} 'Format_file1.txt > Format_file2_1.txt#IDR_CEF_0001 FormatSed' s/$/},/g 'Format_file2_1.txt > Format_file2_2.txt sed' s/^/{'/g 'Out33_files.txt > out4_file.txtsed' s/$/',/g 'Out4_file.txt > Out5_file.txtpaste- D "  "Out5_file.txt format_file2_2.txt > Format_file2.txt#3. idr_cef_0001 HTML "res\\about.html"RM-RF Format_file3_2.txt for((i=1; i<=$linenums; i++)) Do Echo "HTML">> Format_file3_2.txt DoneCat Out22_files.txt | Sed' s/test\///g '> Out44_files.txtcat out44_files.txt | Sed' s#\/#\\\\ #g '> Out55_files.txt Cat Out55_files.txt | Sed' s#^# ' #g '> Out66_files.txtcat out66_files.txt | Sed' s#$# ' #g '> Out77_files.txtpaste- D "  "Format_file2_1.txt format_file3_2.txt > Format_file3_tmp.txtpaste- D "  "Format_file3_tmp.txt out77_files.txt > Format_file3.txtmkdir format_rstmv format_file1.txt format_file2.txt format _file3.txt./format_rst/

Advantages:
1. Fast, not lost, how many files is the number of files.
2. Can be reused, add files or other modules have similar files, go straight through the script.

Shell scripts have inherent advantages in text-by-line reading, column matching, and regular matching. So the shell implementation is a good choice.
If you implement code in C + + with hundreds of or even thousands of lines, the match can be very complex.

Ming Yi World
Reprint please indicate source, original address: http://blog.csdn.net/laoyang360/article/details/49834859
If you feel this article is helpful, please click on the ' top ' support, your support is I insist on writing the most power, thank you!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Traverse the file and format the output file (Shell script implementation)

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.