Solved Need to append date from a text file to each record of other

June 10, 2012 at 12:17:38
Specs: Windows XP
Hi I have an input file test_file_detail as below (/dir/Temp/test_file_detail1)


I need to append the date from a seperate file (/dir/Temp/test_file_header)

I have used the below command but it was not working could you please suggest a solution
H_DATE= cut -c 2-11 /dir/Temp/test_file_header
sed 's/$/^'"${H_DATE}"'/' /dir/Temp/test_file_detail1 > /dir/Temp/expected_outfile

See More: Need to append date from a text file to each record of other

Report •

June 10, 2012 at 14:26:31
Probably there is more than one way of doing this. Based on cutting your data files and pasting them into my Solaris 9 system, the lines are terminated with control characters with the detail file while the header file has control characters embbed within.

Therefore, I think the safest thing is to use an awk script to print only the last field (with the Field Seperator being a ^ character) and then use the paste command to append each line of the awk output to the header file:


# leave out the full path to the files.  files are in the current directory
# for test purposes
awk ' BEGIN { FS="^" }
print $NF
} '  ./test_file_header > ./mytmp.txt

paste  -d"^" ./test_file_detail1 ./mytmp.txt > ./expected_outfile
# end script

Let me know if you have any questions

Report •

June 10, 2012 at 15:26:59
Hi ,
I am not getting the desired output .Please find the output below.


I am using the below code


head -1 /dir/Temp/test_file > /dir/Temp/test_file_header
sed '1d;$d' /dir/Temp/test_file > /dir/Temp/test_file_detail1

H_DATE= cut -c 2-11 /dir/Temp/test_file_header
echo $H_DATE

sed 's/$/^'"${H_DATE}"'/' /dir/Temp/test_file_detail1 > /dir/Temp/expected_outfile


In the above code i am trying to pass the parameter value in sed command but it was not working..
Please suggest me a solution

Report •

June 10, 2012 at 15:54:52
✔ Best Answer
First, a unix lesson:

To capture the output of a command, use command substitution. In ksh/bash, it's like this:

H_DATE=$(cut -c 2-11 /dir/Temp/test_file_header)

With bourne shell, sh, it's like this:

# that is a backtick character - not a single quote
H_DATE=`cut -c 2-11 /dir/Temp/test_file_header`

Second, why do you think the date is stored in characters 2 to 11:

cut -c 2-11 /dir/Temp/test_file_header

Try this:

H_DATE=$(awk ' BEGIN { FS="^" }
print $NF
exit # get the date only on the first line.
} ' ./test_file_header)
echo ${H_DATE}

sed 's/$/^'"${H_DATE}"'/' ./test_file_detail1 > ./expected_outfile

Report •
Related Solutions

Ask Question