KSH: sed command to search and replace last occurrence of a string in a file -
i have tried search lastly occurrence in file using sed. in hp-ux tac alternative not available.
for ex: below info in file,
a|2121212|666666666 | 2|01|2 |b|1111111111 |234234234 |00001148| b|2014242|8888888888| 3|12|3 |b|22222222222 |45345345 |00001150| c|4545456|4444444444| 4|31|4 |b|3333333333333 |4234234 |00001148| i'm trying:
cat $filename | sed 's/00001148/00001147/g' it changing 00001148 00001147 both occurrence of 00001148.
i have search |00001148| of lastly occurrence , replace number. sed command changing both 2 instances of 00001148.
edit
to match lastly line, utilize $
sed '$s/00001148/00001147/g' $filename will give output as
a|2121212|666666666 | 2|01|2 |b|1111111111 |234234234 |00001148| b|2014242|8888888888| 3|12|3 |b|22222222222 |45345345 |00001150| c|4545456|4444444444| 4|31|4 |b|3333333333333 |4234234 |00001147| if matching line lastly line in file, utilize tail instead of cat
tail -1 $filename | sed 's/00001148/00001147/g' the tail command selects last(tail) lines form file, here specified take 1 line usint -1 option
if not lastly line,
grep "00001148" $filename | tail -1 | sed 's/00001148/00001147/g' the grep command finds occureences , tail selects lastly line , sed makes replacement.
sed ksh hp-ux
No comments:
Post a Comment