udelalv
1
Hello.
I have a file (old.txt) that I need to copy into another file (new.txt).
Each line on old.txt ends with CR/LF but the position of CR/LF varies from one record to another.
I need to copy each line of record to new.txt and move CR/LF in pos 165.
Can I use awk to achieve this? How? If not, what other options can be used?
Thank you. Your response(s) are greatly appreciated.
panyam
2
For copying the lines except the last character ,you can use something like this :
awk '{ print substr($0,1,length($0)-1)}' filename.txt
bittoo
3
Did u mean u need to append spaces for the rest of the line till 165th character...
Then u can adapt
awk '{printf "%-165s\n",$0}' old.txt > new.txt
All lines will be left justified for 165 characters...
udelalv
4
If CR/LF is found at position 100, I would need to copy all characters from 1-99, and append spaces from position 100-164.
How do we do this?
panyam
5
Ohh.. is it something like this ?.
awk '{ print substr($0,1,length($0)-1);for(i=length($0)+1;i<165;i++) print " "}' filename.txt
bittoo
6
Can u try the above comamnd on ur file and let me know the result.. If that does not work, let us give a simple input and ur expected output
udelalv
7
this is my test data:
HEADER BBBBB_gllvdcout 20091106.1700040715
AA * EAA EAAAAAA 6AAAAAAAA0 Y AAAA AAA SAAAAA MAAAAAAAAAA
AB * EBB EBBBBBB 6BBBBBBBB0 BBBB BBB BBBBBB BBBBBBBBBBB
TRAILER 351845 20091106.170004
when I tried the above command I get this:
HEADER BBBBB_gllvdcout 20091106.170004071
.
.
.
.
.
AA * EAA EAAAAAA 6AAAAAAAA0 Y AAAA AAA SAAAAA MAAAAAAAAA
.
.
.
.
.
AB * EBB EBBBBBB 6BBBBBBBB0 BBBB BBB BBBBBB BBBBBBBBBB
.
.
.
.
---------- Post updated at 01:10 AM ---------- Previous update was at 01:06 AM ----------
when i tried this:
awk '{printf "%-165s\n",$0}' old.txt > new.txt
I get this:
HEADER BBBBB_gllvdcout 20091106.1700040715
AA * EAA EAAAAAA 6AAAAAAAA0 Y AAAA AAA SAAAAA MAAAAAAAAAA
AB * EBB EBBBBBB 6BBBBBBBB0 BBBB BBB BBBBBB BBBBBBBBBBB
TRAILER 351845 20091106.170004
CR/LF is at position 166.
panyam
8
Try this :
awk '{pr=substr($0,1,length($0)-1);for(i=length($0);i<166;i++) pr=pr"-" ; print pr}' filename.txt
Note i used "-" instead of " " for better readability
bittoo
9
awk '{printf "%-164s\n",$0}' old.txt > new.txt .. should do the job..