I would like to process below file:
01234000000000000000000+000000000000000000+
02586000000000000000000+000000000000000000-
12345000000000000000000+000000000000000000-
12122000000000000000000+000000000000000000+
I want to convert above file to:
01234,000000000000000000+,000000000000000000+
02586,000000000000000000+,000000000000000000-
12345,000000000000000000+,000000000000000000-
12122,000000000000000000+,000000000000000000+
Input file have fixed width columns 5,19,19 respectively.
I would like to solve using linux command.
I tried below command, but it is not working :(
awk 'BEGIN{FIELDWIDTHS="5 19 19";OFS=",";}{$1="$1,$2,$3"}' data.txt
Executing above command on ubuntu 14.04 LTS desktop OS, the output was nothing(blank).
Your attempt was quite close, although you forgot to {print}
:
awk 'BEGIN{FIELDWIDTHS="5 19 19";OFS=","}{$1=$1}1' file
{$1=$1}
assigns the first field to itself, which is enough to make awk "touch" each record. I've used the shorthand 1
, which is the shortest true condition. The default action is {print}
.
Note that FIELDWIDTHS
is a GNU awk extension, so if you're using a different version, you will have to go with a different approach. For example:
awk 'BEGIN{OFS=","}{print substr($0,1,5),substr($0,6,19),substr($0,25)}' file
$ sed -r 's/(.{5})(.{19})/\1,\2,/' file
01234,000000000000000000+,000000000000000000+
02586,000000000000000000+,000000000000000000-
12345,000000000000000000+,000000000000000000-
12122,000000000000000000+,000000000000000000+
that would be very easy:
sed -n 's/\(.\{5\}\)\(.\{19\}\)\(.\{19\}\)/\1,\2,\3/p' your_file
what it does, is to capture each line by 5, 19, 19 then print it out with ,
in between.
$ echo 01234000000000000000000+000000000000000000+ | sed -n 's/\(.\{5\}\)\(.\{19\}\)\(.\{19\}\)/\1,\2,\3/p'
01234,000000000000000000+,000000000000000000+
Perl救援:
perl -pe 'for $p (5, 25) { substr $_, $p, 0, "," }' data.txt
this is suitable task for cut
as well
$ cut --output-delimiter=',' -c1-5,6-24,25- data.txt
01234,000000000000000000+,000000000000000000+
02586,000000000000000000+,000000000000000000-
12345,000000000000000000+,000000000000000000-
12122,000000000000000000+,000000000000000000+
--output-delimiter=','
specify output field separator -c
to select specified character(s) 1-5
first field 6-24
second field 25-
rest of the line awk '{sub(/.0/,",0")sub(/+/,"+,")}1' file
0123,000000000000000000+,000000000000000000+
0258,000000000000000000+,000000000000000000-
1234,000000000000000000+,000000000000000000-
1212,000000000000000000+,000000000000000000+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.