1827445 Members
6286 Online
109965 Solutions
New Discussion

Re: awk Help

 
Ashwin_4
Frequent Advisor

awk Help

hi masters,
I have one file with 3 columns, i want to take the content of this file in a individual file based on substr(3,1,3) field.

B---A213---206934875
B---A213---20534875
B---A213---207934875
.....
.....

Space is indicated by "---"
i want three output files as below,
File F206==> B---A213---206934875
File F205==> B---A213---20534875
File F207==> B---A213---207934875

Thanks

7 REPLIES 7
Mark Grant
Honored Contributor

Re: awk Help

How about a little shell script

IFS="
"
for i in `cat datafile`
do
newfile=`expr "$i" : ".......\(...\)"`
echo "$i" > "F$newfile"
done
Never preceed any demonstration with anything more predictive than "watch this"
Steve Steel
Honored Contributor

Re: awk Help

Hi


Based on

B A213 206934875
B A213 20534875
B A213 207934875


in tmp/fil

cat tmp/fil|while read line
do
echo $line|read a b c
name="F"$(echo $c|cut -c1-3)
echo "$line" > $name
done


Based on

B---A213---206934875
B---A213---20534875
B---A213---207934875

cat tmp/fil|while read line
do
name="F"$(echo $line|cut -c12-14)
echo "$line" > $name
done




Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Kartik Gajjar
New Member

Re: awk Help

Hi,

Assuming the text in file "s.txt"

Run
awk '{print "echo " $0 " >> F" substr($0,12,3) }' s.txt > foo.sh

This will generate foo.sh

Just run foo.sh will give you the result.

Enjoy.

Kartik
Ashwin_4
Frequent Advisor

Re: awk Help

hi,
Thanks for quick reply. shall we do it using "AWK"

thanks
Mark Grant
Honored Contributor

Re: awk Help

Why do you have to use awk? Awk is good, don't get me wrong, but I don't think it's the best solution to your current problem.

The awk solution posted above does not give you the additional files you wanted by the way.
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: awk Help

Actually, re-reading Kartik's script above, properly this time, it does give you the files and is an interesting solution to the problem.

Seems a little over the top to use awk to create a shell script when you can do it in a shell script to start with, however, 10 points for style on that one Kartik :)
Never preceed any demonstration with anything more predictive than "watch this"
Rodney Hills
Honored Contributor

Re: awk Help

"awk" would be pretty straight forward-

awk '{print $0 >"F" substr($3,1,3)}'
HTH

-- Rod Hills
There be dragons...