Processing of seq in a shell
Background: Using the shell to extract the contents of the file, the filename is generated with the serial number as follows, the file nearly more than 400 W is as follows:
Www.ahlinux.com
Original script
#! /bin/sh
#str1 = ""
#filecount = ' Ls-l/root/gjj | Wc-l | awk ' {print '} '
#echo $filecount
For n in ' seq '
Do
Filename= "/windows_gjj/" ${n} ". txt"
Echo $filename
Dos2unix $filename
Sed-i ' 1,76d ' $filename
Sed-i ', $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 ' $filename | Wc-l | awk ' {print '} '
If [$flag-ne 10]; Then
Cat $filename >>/tmp/all_gjj.log
echo "********************************************************************************************" >>/tmp /all_gjj.log
Lcount= ' Wc-l $filename | awk ' {print '} '
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 $str 1
Sed-i ' 1d ' $filename
Done
echo $str 1 >>/root/gjj.txt
Fi
Done
The $1,$2 in the script represents the starting sequence number.
In the beginning, it is normal to use this script to extract the contents of the file, but when the filename is 7 digits, there is a problem:
As follows:
[[Email protected] ~]# sh tiqu.sh 2908637 2908640
/windows_gjj/2.90864e+06.txt
Dos2unix:converting file/windows_gjj/2.90864e+06.txt to UNIX format ...
Dos2unix:problems converting File/windows_gjj/2.90864e+06.txt
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
grep:/windows_gjj/2.90864e+06.txt: No file or directory
Cat:/windows_gjj/2.90864e+06.txt: No file or directory
WC:/windows_gjj/2.90864e+06.txt: No file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
Head: Unable to open "/windows_gjj/2.90864e+06.txt" read data: No file or directory
|
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
Head: Unable to open "/windows_gjj/2.90864e+06.txt" read data: No file or directory
||
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
SED: Unable to read/windows_gjj/2.90864e+06.txt: no file or directory
Head: Unable to open "/windows_gjj/2.90864e+06.txt" read data: No file or directory
Analysis: This problem occurs primarily when the shell represents a 7-digit number in the form of an exponent, resulting in the failure to find the corresponding file
Workaround: In order to be able to make 7 digits still in the form of numbers, try the SEQ in the-F, the options such as-W did not achieve the desired effect, and finally adopted a compromise method, the highest bit with the character instead of 6 for the SEQ generation, with the parameter-W hold the width of the number consistent, repair the script as follows:
#! /bin/sh
#str1 = ""
#filecount = ' Ls-l/root/gjj | Wc-l | awk ' {print '} '
#echo $filecount
For n in ' seq-w '
Do
N= "2" ${n}
Filename= "/windows_gjj/" ${n} ". txt"
Echo $filename
Dos2unix $filename
Sed-i ' 1,76d ' $filename
Sed-i ', $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 ' $filename | Wc-l | awk ' {print '} '
If [$flag-ne 10]; Then
Cat $filename >>/tmp/all_gjj.log
echo "********************************************************************************************" >>/tmp /all_gjj.log
Lcount= ' Wc-l $filename | awk ' {print '} '
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 $str 1
Sed-i ' 1d ' $filename
Done
echo $str 1 >>/root/gjj.txt
Fi
Done
[[Email protected] ~]# 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 ...
Achieve the desired goal!
- This article is from: Linux Learning Network
Processing of seq in a shell