Rename file using sed or awk

I have a filename like 1_DATE_3_4.5_888 and I want to modify the date field (ie the last 4 digits ) alone and remove the last field.

Old filename:1_DATE_3_4.5_888
Given date (for eg):120606259532

modified date:120606259899

new filename:1_<modified date>_3.4.5

Hello, please edit your post above and change the all caps.

In case you forgot to read the forum rules, here is quick copy.

Cheers.

The UNIX and Linux Forums

FILE="1_120606259532_3_4.5_888"
NEW_FILE=$(echo "$FILE" | nawk -F_ -v n="120606259899" -v OFS=_ '$2=n')
echo $NEW_FILE
1 Like

thanks a lot.. but the output i'm getting is

echo $NEW_FILE
1_120606259899_3_4.5_888

the last segment is 888, which is not removed.

 
NEW_FILE=$(echo "$FILE" | nawk -F_ -v n="120606259899" -v OFS=_ '$2=n;$NF="_"' | sed 's/__$//')

1 Like

thanks a lot,

but m getting 2 outputs,

$echo $NEW_FILE
1_120606259899_3_4.5_888 1_120606259899_3_4.5

---------- Post updated at 03:46 AM ---------- Previous update was at 03:35 AM ----------

suppose ther are 100 files. i need to remove the 888 segments for all the 100 at the end, and the date field is constant it always contains 12 digits,and i need 2 remove the last 4 digits in it, and replace it with "9899".
Is this possible with script??

use this

 
NEW_FILE=$(echo "$FILE" | nawk -F_ -v n="120606259899" -v OFS=_ '{$2=n;$NF="_";print}' | sed 's/__$//')
1 Like

thanks a lot , i'm getting the desired output..

---------- Post updated at 04:04 AM ---------- Previous update was at 03:51 AM ----------

INPUT:1_120606259532_3_4.5_888

if there any many inputs..in all the inputs the date length (bold) is constant ie 12 digits,i need 2 remove the last 4 digits in date field and replace with "9899" and finally the last segment in the file ie 888 needs to be removed.

s t possible??

---------- Post updated at 05:59 AM ---------- Previous update was at 04:04 AM ----------

by assigning some variable as 8899..and then substituting the date field,in the part of the script,is that possible.?

what is the desired output ?

# echo $INPUT|sed -r 's/(._.{8}).{4}(_.*_).*/\19899\2/'
1_120606259899_3_4.5_
1 Like

sorry am getting the folllowing error,while executing the above

sed: Not a recognized flag: r
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]

u can try this

# echo $INPUT|sed 's/\(._.\{8\}\).\{4\}\(_.*_\).*/\19899\2/'
1 Like

thanks a lot its working fine, can you explain that sed command, i am not getting it & more over i want the desired output like 1_120606259899_3_4.5

and second

# val=9899 ; echo $INPUT|sed 's/\(._.\{8\}\).\{4\}\(_.*_\).*/\1'$val'\2/' 

can the command be changed so that the "_" is not coming at the end

---------- Post updated at 07:41 AM ---------- Previous update was at 07:40 AM ----------

current output:1_120606259899_3_4.5_
required:1_120606259899_3_4.5

what is your O.S (solaris or aix or ?_)
with Gnu Sed

# val=9899
echo $INPUT|sed 's/\(._.\{8\}\).\{4\}\(_.*_\).*/\1'$val'\2/'
1_120606259899_3_4.5_

so it is

# echo $INPUT|sed 's/\(._.\{8\}\).\{4\}\(_.*\)_.*/\1'$val'\2/'
1_120606259899_3_4.5

and little explanation

._ --> one char (first char) (1)
.\{8\}\ --> 8 chars
\( ........ \)  --> save portion of our string for write in the RHS (cut and save it to buffer and it is equal to \1 [backreference numbered 1] , 
the sed structure is named "back reference headache(sed buffer) for after the write the stdoutput in the RHS[the right-hand side] 
liitle expl --> sed 's/THIS/CHANGE/'  --> LHS is the "THIS" in here and RHS is the "CHANGE")
.\{4\}  --> remaining 4 chars from our string

so we get the ._ -> first char from string --> "1" and "_"
and 8 chars --> "12060625"  
and 4 chars --> "9532"
\(._.\{8\}\) --> (\1 --> 1_12060625)
\(_.*\)  --> "_3_4.5"  (it is our back reference numbered 2 --> it expressed like --> \2)
_.*  --> "_888" 

and in the RHS(right-hand side sed 's/...../...../') we can the write that the our values

so ....../\1'$val'\2/
\1 --> 1_12060625
$val --> "9899"
\2 --> "_3_4.5"

result is "1_120606259899_3_4.5"

regards
ygemici

1 Like