Hi,
I have a huge text file. It looks like
abcde bangalo country 12345 lastfield
i want to get first 3 characters from field1 and first 3 characters from field 2 and insert the result as a new field. example the result should be:
abcde bangalo abcban country 12345 lastfield
Please help me on this
Thanks in advance for your help.
Scott
2
Hi.
Would this work:
awk '{$2 = $2 " " substr( $1, 1, 3 ) substr($2, 1, 3); print}' file
Output:
abcde bangalo abcban country 12345 lastfield
(ps: your example showed getting only 3 characters, not 4)
awk '{ fld=substr($1,1,3) substr($2,1,3); print $1, $2, fld, $3, $4, $5} ' inputfile > newfile
echo "abcde bangalo country 12345 lastfield" | sed 's/\(...\)\([^ ]* \)\(...\)\([^ ]* \)/\1\2\3\4\1\3 /'
kshji
5
#!/bin/ksh
# or bash
# script
while read f1 f2 f3 f4 f5 fx
do
fnew="${f1:0:3}${f2:0:3}"
echo "$f1 $f2 $fnew $f3 $f4 $f5"
done
chmod a+rx script
./script < infile > outfile
---------- Post updated at 03:04 PM ---------- Previous update was at 03:02 PM ----------
Awk is faster.