Reputation: 127
I am using sed to update various date formats within a text file and appending the result to another file.
The file dates are stored as YYYMMDD etc so I am replacing the YYYMMDD with the actual date.
The only issue is the final line of each file is missing a new line character, meaning the last and first lines are incorrectly aligned.
File 1:
00 YYMMDD TEST
05 3452256 MMDD 33456
80 File Trailer
File 2:
00 YYMMDD TEST
05 445674 MMDD 234456
80 File Trailer
What I need is the YYMMDD and MMDD updated with the current date and both files appended together like this:
00 180129 TEST
05 3452256 0129 33456
80 File Trailer
00 180129 TEST
05 445674 0129 234456
80 File Trailer
But what I am actually getting is:
00 180129 TEST
05 3452256 0129 33456
80 File Trailer00 180129 TEST
05 445674 0129 234456
80 File Trailer
Code:
YYYY=$(date +"%Y")
YY=$(date +"%y")
MM=$(date +"%m")
DD=$(date +"%d")
HH=$(date +"%H")
MI=$(date +"%M")
SS=$(date +"%S")
JJJ=$(date +"%j")
sed -- "s/yyyymmdd/$yyyyMMdd/g;s/yymmdd/$yyMMdd/g;s/mmdd/$mmdd/g;s/yyjjj/$yyjjj/g" "$full_path" >> $deploy_path
Does anyone know why this is happening and if here is an easy fix?
Edit
It turns out the problem was with the source files I was using. Specifically: the file trailers were missing a CRLF. So the code above is working fine.
Upvotes: 2
Views: 90
Reputation: 20032
I first wanted to suggest to replace "$full_path"
with <(cat "$full_path"; echo)
, but than realized that your "$full_path" will have 2 files and my fix is for one file only.
You can use sed
for appending a newline with $s/$/\n/
, so change your command into
sed -- "\$s/$/\n/; s/yyyymmdd/$yyyyMMdd/g;s/yymmdd/$yyMMdd/g;s/mmdd/$mmdd/g;s/yyjjj/$yyjjj/g" "$full_path" >> $deploy_path
Upvotes: 1