Seq processing in a shell
Background: use shell to extract the content in the file. The file name is generated using the serial number below. There are nearly 400 million files as follows:
Original www.2cto.com script
#! /Bin/sh
# Str1 = ""
# Filecount = 'LS-l/root/gjj | wc-l | awk '{print $1 }''
# Echo $ filecount
For n in 'seq $1 $2'
Do
Filename = "/windows_gjj/" $ {n} ". txt"
Echo $ filename
Dos2unix $ filename
Sed-I '1, 76d' $ filename
Sed-I '41, $ d' $ filename
Sed-I's/<. * "> // G' $ filename
Sed-I's/<. *> // G' $ filename
Sed-I's/^ [[: space:] * // G' $ filename
Sed-I '/^ $/d' $ filename
# Sed-I's/; // G' $ filename
# Cat $ filename>/tmp/all_gjj.log
Flag = 'grep "& nbsp" $ filename | wc-l | awk '{print $1 }''
If [$ flag-ne 10]; then
Cat $ filename>/tmp/all_gjj.log
Echo "************************************** **************************************** * ************* ">/tmp/all_gjj.log
LCOUNT = 'wc-l $ filename | awk '{print $1 }''
Str1 = ""
For I in 'seq 1 10'
Do
Sed-I '1d '$ filename
Str = 'head-n 1 $ filename'
Echo $ str>/tmp. log
Str1 =$ {str1 }$ {str} "|"
Echo $ str1
Sed-I '1d '$ filename
Done
Echo $ str1>/root/gjj.txt
Fi
Done
In the script, $1 and $2 represent the starting serial number.
At the beginning, it was normal to use this script to extract the file content. However, when the file name contains seven digits, the following error occurs:
As follows:
[Root @ ALL ~] # Sh tiqu. sh 2908637 2908640
/Windows_gjj/2.90864e000006.txt
Dos2unix: converting file/windows_gjj/2.90864e000006.txt to UNIX format...
Dos2unix: problems converting file/windows_gjj/2.90864e000006.txt
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Grep:/windows_gjj/2.90864e000006.txt: the file or directory does not exist.
Cat:/windows_gjj/2.90864e000006.txt: the file or directory does not exist.
Wc:/windows_gjj/2.90864e000006.txt: the file or directory does not exist.
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Head: Unable to open "/windows_gjj/2.90864e000006.txt" to read data: no such file or directory
|
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Head: Unable to open "/windows_gjj/2.90864e000006.txt" to read data: no such file or directory
|
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Sed: unable to read/windows_gjj/2.90864e000006.txt: No file or directory
Head: Unable to open "/windows_gjj/2.90864e000006.txt" to read data: no such file or directory
Analysis: The main cause of this problem is that the shell expresses the 7-digit number in the form of an exponential, resulting in the failure to find the corresponding file.
Solution: in order to enable the 7-Digit Display as a number, the-f,-w and other options in seq fail to achieve the expected effect. Finally, the compromise method is adopted, the highest bit is replaced by characters, and the last 6 is generated by seq. The parameter-w is used to keep the width of the digits consistent. The script for repairing this is as follows:
#! /Bin/sh
# Str1 = ""
# Filecount = 'LS-l/root/gjj | wc-l | awk '{print $1 }''
# Echo $ filecount
For n in 'seq-w $1 $2'
Do
N = "2" $ {n}
Filename = "/windows_gjj/" $ {n} ". txt"
Echo $ filename
Dos2unix $ filename
Sed-I '1, 76d' $ filename
Sed-I '41, $ d' $ filename
Sed-I's/<. * "> // G' $ filename
Sed-I's/<. *> // G' $ filename
Sed-I's/^ [[: space:] * // G' $ filename
Sed-I '/^ $/d' $ filename
# Sed-I's/; // G' $ filename
# Cat $ filename>/tmp/all_gjj.log
Flag = 'grep "& nbsp" $ filename | wc-l | awk '{print $1 }''
If [$ flag-ne 10]; then
Cat $ filename>/tmp/all_gjj.log
Echo "************************************** **************************************** * ************* ">/tmp/all_gjj.log
LCOUNT = 'wc-l $ filename | awk '{print $1 }''
Str1 = ""
For I in 'seq 1 10'
Do
Sed-I '1d '$ filename
Str = 'head-n 1 $ filename'
Echo $ str>/tmp. log
Str1 =$ {str1 }$ {str} "|"
Echo $ str1
Sed-I '1d '$ filename
Done
Echo $ str1>/root/gjj.txt
Fi
Done
[Root @ ALL ~] # Sh tiqu. sh 908636 908640
/Windows_gjj/2908636.txt
Dos2unix: converting file/windows_gjj/2908636.txt to UNIX format...
|
/Windows_gjj/2908637.txt
Dos2unix: converting file/windows_gjj/2908637.txt to UNIX format...
Achieved the expected goal!