Now suppose in row 1 I need to append 6589 with respective value from mapping file i.e 7879. Similarly 8787 should be replaced as 0909.
The new value should fill the space in the col2:
So after processing it should look like this:
If tried getting character 28 to 37 from column:
s= substr($1,28,10)
This didn't work:
As I need to replace character 28to 37 in first column with value from mapping file.
Plz help !
This script creates another script. The new script uses sed, so it is quite fast, and the entire process is data-driven by the lookup file:
#!/usr/bin/env sh
# @(#) s1 Demonstrate creation of sed script with awk.
set -o nounset
echo
## Use local command version for the commands in this demonstration.
echo "(Versions used in this script displayed with local utility "version")"
version bash awk sed
echo
echo " Input file of lookup tokens:"
cat -tv data1
./a1 data1 > script
chmod +x script
echo
echo " This is the created sed script:"
cat -n script
echo
echo " Results of running script:"
./script data2
exit 0
You notice that there is an "a1" script. It's in awk, because awk facilitates handling text fields. However, the quoting is a bit complicated:
#!/usr/bin/awk -f
# @(#) a1 Demonstrate creation of sed script from lookup file.
echo
BEGIN { print "sed \\" }
{ print "-e 's/" $1 " /" $1$2 " /' \\" }
END { print " data2" }
When s1 runs, it then produces:
% ./s1
(Versions used in this script displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4
GNU sed version 4.1.2
Input file of lookup tokens:
6589 7879
8787 0909
4343 4576
This is the created sed script:
1 sed \
2 -e 's/6589 /65897879 /' \
3 -e 's/8787 /87870909 /' \
4 -e 's/4343 /43434576 /' \
5 data2
Results of running script:
6767879898009965897879 65656576687878
7887576576757687870909 88787878756446
3232476568769843434576 42341242542345
summer_cherry,
this is not the expected input for file 'b' - reread the OP.
Plus, there's a better/preferable way to distiguish between 2 input file: rather than relying on different number of fields (as is the case for this particular example) - take advantage of NR and FNR values when processing 2 input files. There're plenty of examples in the previous awk-related solutions/threads.
It's working perfectly fine !
Tons and Tons of thanks ....
But only 1 small issue came
I am loosing the original blank spaces in the final output :
So if in main file values are like this :
1US6786868976897[space][space][space]0[space][space]0
1US6786868976897[space][space][space]0[space][space]0
1US6786868976897[space][space][space]0[space][space]0
My out put after processing is coming like this:
1US6786868976897[space]0[space]0
1US6786868976897[space]0[space]0
1US6786868976897[space]0[space]0
So multiple space is getting truncated:
How to solve this?
Also I need to save the output in a separate file ...Kindly suggest !