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)

1^RAJ^M
2^LEE^M
3^RAMYA^F

I need to append the date from a seperate file (/dir/Temp/test_file_header)
1^RAJ^M^9998-12-31
2^LEE^M^9998-12-31
3^RAMYA^F^9998-12-31

I have used the below command but it was not working could you please suggest a solution
code:
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 •

#1
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:

#!/bin/ksh

# 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 •

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

1^RAJA^M^9999-12-31
2^JUBLEE^M^
3^SOWMYA^F^

I am using the below code

main
{

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 •

#3
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