|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
split string
my old program is using cut to get the date of the file such:
for FILE in ATK* do file_year=$(echo $FILE| cut -c 13-16) file_month=$(echo $FILE|cut -c 13-18) ### echo $file_month --->just as example! done where filename is such: ATK_7B1_WIP_20041116160700.1.xml.dsc ATK_7B1_WIP_20041116160700.2.xml.dsc ATK_7B1_WIP_20041116160700.3.xml.dsc ATK_7B1_WIP_20041116160700.4.xml.dsc problem is how can i enhance it to all filename? such: AIT_7B1_WIP_20050101010101.dsc ASECL_7B1_WIP_20041209151027.xml.dsc ASEKR_7B1_WIP_20050101020202.dsc so i cant use the ' for FILE in ATK* ' rite? should replace with what? all the filename will be the same unless the front name: xxxx_7B1_WIP_20041116160700xxx help!!! |
|
#2
|
|||
|
|||
|
Quote:
Filter out only the appropirate string of your digits and then you can cut the different fields as usualy. Try this script: : while read F; do DIGITS=$(echo $F | awk ' { match($0,"WIP_[0-9]+") print substr($0, RSTART+4, RLENGTH-4) } ' ) echo $DIGITS done <<EOF AIT_7B1_WIP_20050101010101.dsc ASECL_7B1_WIP_20041209151027.xml.dsc ASEKR_7B1_WIP_20050101020202.dsc EOF As yu can see, I have used your file names. The script gives you the time stamp only. (Copy and store the script, then chmod +x and run it) Have fun. ![]() |
|
#3
|
|||
|
|||
|
my version using sed
#!/bin/sh fname=abc_20050201113355.xx fname=lskdfldlfgjhlabc_20050201113355.xx ###REM a very generic way ###REM eval `echo $fname|sed 's/.*_\(....\)\(..\)\(..\)\(..\)\(..\)\(..\).*/Y=\1 M=\2 D=\3 h=\4 m=\5 s=\6/'` ###REM this is better, nota: sed does not know the '\d+' notation eval `echo $fname|sed 's/.*_\([1-9][0-9]\{3\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\).*/Y=\1 M=\2 D=\3 h=\4 m=\5 s=\6/'` echo year $Y echo month $M echo day $D echo hour $h echo min $m echo sec $s |
|
#4
|
|||
|
|||
|
Quote:
Very clever, handy, faster than awk! But badly readable by a man. To use awk or sed? It is a matter of taste and of personal skil. The computer time factor today is not too significant, because the saved seconds of computer time are cheaper than hours of programing. If the program in the future should be administered by another person, I would prefer the awk version because of its better readability. Regards ![]() |
|
#5
|
|||
|
|||
|
hello zlutovsky
i assume a programmer at least should know regexp in *nix that's basics so the long sed expr is really easy to read awk is a formidable tool, in this case awk has no chance vs sed because of the way awk is working awk ... imagine an excel, split and full rows and fields, then work sed ... read a line, scans cmds and apply eachh cmds on the readed line as long you DONT need formatting or compilation, sed will be faster i mean a+=b; no probls in awk you sure can convince 'sed' to do it, it's a longer way |
|
#6
|
|||
|
|||
|
thanks all
i already solve my problem
![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > split string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|