bash/sed/awk fill each line in text file with space to fixed length
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
bash/sed/awk fill each line in text file with space to fixed length
hello all,
i need to reformat a text file to have the length of each line is fixed at 1968bytes and fill the line with space if the line length is less than 1968bytes. How to accomplish this using sed/awk/bash.
currently i'm using dd (with conv=block) and loop through each line to accomplish this task but it really slow on big file (around 20000 of lines) and assuming my sample data have no CR/LF characters.
Code:
...
for (( i = 0; i <= 19999; i++ ))
do
dd if=${FILENAME} bs=123 cbs=1968 conv=block,sync count=1 skip=${i} >> ${OUTPUTFILE}
done
...
thx a lot ghostdog74 and radoulov for the solutions. all those is working for files that have CR/LF.
but my situation is like this;
the 'data' file is currently a single line file (as on magnetic tape) which have no CR/LF characters, but i know the length for each records is 123bytes so i can get the above 'data' if i run;
Code:
fold -w 123 data > DataTemp
and i run your awk/bash commands on DataTemp to fill the space for each line to become 1968bytes.
but this will only receive the DataTemp with CR/LF.
Code:
visually, i want to format this kind of data (no cr/lf in both input and output)
|---123bytes---|---123bytes---|---123bytes---|
to become
|-----1968bytes-----|-----1968bytes-----|-----1968bytes-----|
currently
Code:
the awk script format from this
|---123bytes---|
|---123bytes---|
|---123bytes---|
to
|-----1968bytes-----|-----1968bytes-----|-----1968bytes-----|
my task is to split this file from HDR1 to EOF2 to become individual file, so in this example there will be 3 individual file. i'm trying to use only sed/awk/dd/bash for this task. Can anyone out there can help me?
in addition i also want each section to become 1968bytes in length (fill with space like earlier post in this thread).
thanks a lot radoulov. your solution works for the sample file, but what if the data block count is variable and not fixed to only five in each section (from HDR1 to EOF2).. i may have more or less than five data in each section. in the real file (which the size was 7MB) i have almost 32000 data block in each section.
I understand.
If the number of 80 bytes records is fixed
(i.e. always four: HDR1, HDR2, EOF1 and EOF2)
and the number of 123 bytes records varies,
the FIELDWIDTHS could be calculated: (length-320)/123,
in my example: (length-240)/123 as the EOF2 section
is part of the RS/RT.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.