I want to collect the characters from 1-10 and 20-30 from each line of the file and take them in a file in the following format.Can someone help me with this :
string1,string2
string1,string2
string1,string2
:
:
:
:
I want to collect the characters from 1-10 and 20-30 from each line of the file and take them in a file in the following format.Can someone help me with this :
string1,string2
string1,string2
string1,string2
:
:
:
:
yea
use cut and paste command
cut -c1-10 filename > temp1
cut -c20-30 filename > temp2
paste temp1 temp2 > final
or
can use sed also
sed "s/\(.\{10\}\).\{9\}\(.\{10\}\).*/\1,\2/" file
awk '{ printf "%s,%s\n", substr($0, 1, 10), substr($0, 20, 30) }' filename
the above script may give other output if we have spaces or blank lines, so better would be to use cut and paste command or the above sed script is again a correct one for your search
You are right, it will have a "," if there are blank lines
this would solve that,
awk ' { if( length($0) > 0 ) {printf "%s,%s\n", substr($0, 10, 20), substr($0, 20, 30) } }' filename
What if there are spaces? I dont get that really! Could you please explain?
If you have Python, here's an alternative:
o = open("newfile","a")
for line in open("input"):
o.write( "%s , %s\n" % (line[0:10] , line[19:30] ) )
o.close()
Missing positions are replaced by spaced :
awk '{printf("%-10.10s,%-10.10s\n", substr($0,10,10), substr($0,20,10))}' filename
Jean-Pierre.