String Function in UNIX

Hi -
Have file1 which has the below date

08/31/2018

And also have file2 which has the below texts

ASOF:<CMODate>

FUND

I need to read the second file if it has colon \(:\) then move the date from first file to second file
like this

ASOF:08/31/2018

have used cut -d":" -f1 and moved the value ASOF: into a variable but not able to find the length and move the date value. Can somebody help me please.

dt=$(head -1 file1) && sed -i "1 s|:.*$|:$dt|" file2

Your requirements are ambiguous as to whether or not there is anything in file1 other than the date and, if there is, where in that file the date is located; whether or not there might be more than one line containing a colon in file2 ; and, if there is more than one line containing a colon, whether or not all lines containing a colon should be modified. And, again, you have not told us what operating system and shell you're using.

Assuming that the date in file1 is alone on the first line in that file (and not caring whether or not there might be other lines in that file), that you only want to change lines in file2 that contain the string ASOF: and that all (zero or more) lines containing that string should be modified, and that you're using a shell that is based on Bourne shell syntax, then you might want to try something like:

IFS= read -r dt < file1
ed -s file2 <<-EOF
	g/ASOF:/s|:.*|:$dt|
	w
	q
EOF
1 Like