Hi,
In files, I have a field Date Of Birth (DOB). After that line I need to add Date of Joining (DOJ)
DOB:19-Apr-1981
needs to become
DOB:19-Apr-1981
DOJ:20-Jun-2005
What can be a sed/perl line that can do it for me. Please note that DOB/DOJ I have in variables
I am doing in a sh script
'line' represents file name which has names of many files which need to be enhanced.
var1=`echo $line | cut -d ":" -f1`
var2=`echo $line | cut -d ":" -f2`
perl -e 'while (<>) { s/(DOB:)/$1\/DOJ: $var2/g; print }' $var1.txt > $va
r1\_new.txt
var1 - filename
var2 - DOJ data
Please advise.
Used two lines of input to show what happens:
PAT="DOJ:20-Jun-2005"
sed '/$/a\'${PAT}'' infile
DOB:19-Apr-1981
DOJ:20-Jun-2005
DOB:20-Apr-1981
DOJ:20-Jun-2005
That's what you wanted?
Currently my files have
DOB:........ line
I need to add DOJ:..... after that line
1 clarification.
DOB line is not the last line.
The file would have something like
Name:
DOB:
Address:
Contact:
Qualification:
etc
Alternatively if I can induce that in a specified line, that is equally great
Better show a larger snippet of your input file to make circumstances easier to see/understand, please.
PAT="DOJ:20-Jun-2005"
sed '/^DOB.*$/a\'${PAT}'' infile
Will now only add that pattern as a separate line if, if the former line starts with DOB.
Thanks a lot for your help.
My original file looks like
Name: Eager
DOB: 19-Apr-1981
Address: 2nd Cross, Church Street
Contact: 407 ...
Qualification: Bachelor Of Comp Science
Needs to become
Name: Eager
DOB: 19-Apr-1981
DOJ: 20-Jun-2005
Address: 2nd Cross, Church Street
Contact: 407 ...
Qualification: Bachelor Of Comp Science
I have a file like DOJAdd.txt which mentiones Eager.txt which needs to be altered
DOJAdd would look like
Eager: 20-Jun-2005
Seeker: 18-Sep-2007
So I am extracting 20-Jun-2005 for Eager file and adding that content
Try this:
awk 'BEGIN{FS=OFS=": "}
NR==FNR{a[$1]=$2;next}
a[$2]{s=a[$2];print;getline;print;print "DOJ: " s;next}
1' DOJAdd.txt Eager.txt
If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.
Regards