Link to home
Start Free TrialLog in
Avatar of n78298
n78298

asked on

unix increasing line length in file

I have a file in unix in which line length of every record is 218 characters. I want a shell script which takes this file as input and increase the line length of each record to 562.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

do you want to join the lines or to add extra space?

can you give example? is this a text file?
Avatar of n78298
n78298

ASKER

This is a text file. ( Flat File).  I just want to add extra speces at the end.
Avatar of n78298

ASKER

Any solution for the above problem.
This is UNIX - there's always a way!

One would be awk:

  awk '{printf("%-562s\n",$0);}' flat_file_name > output_file_name

The minus befor the 562 is to add the spaces at the end of the line (otherwise it adds them at the beginning).
Did my awk script answer your question?

If not, is there anything else I need to add?
Avatar of n78298

ASKER

Hello Simon

Ur solution is wonderful and worked perfectly.  

But there is a small change , this number 562 is not fixed and we need to have it some variable and pass it to awk command. Is it possible to call same awk command not hardcoding the number as 562 but getting it from some other variable.
You canpass in a variable like that, but it's probably easier to leave it as a shell variable and do:

DESLEN=562

  awk '{printf("%-'"${DESLEN}"'s\n",$0);}' flat_file_name > output_file_name

(careful with the quotes!)
Avatar of n78298

ASKER

Hi Simon

This is not working. :-(

abcd=562
awk '{printf("%-"${abcd}"s\n",$0);}' ACCOUNT_IBBA-COUTTS_00_20090427_093446.data > 1

The error is

awk: syntax error  Context is:
>>>     {printf("%-"${  <<<
Avatar of n78298

ASKER

awk '{printf("%-'${abcd}'s\n",$0);}' ACCOUNT_IBBA-COUTTS_00_20090427_093446.data > 1

Missed the single quote before double qote. Working now.
Avatar of n78298

ASKER

1 more thing. now in the rest of the places i want to  put soem default value. Is it possible through this command ?

Earlier the file was 218 characters. Now changed to 562 through this command.

awk '{printf("%-'${abcd}'s\n",$0);}' ACCOUNT_IBBA-COUTTS_00_20090427_093446.data > 1

I have a string of 346 characters. ( 562-218) and I want to pad in every record. is it possible through some variation in above command only.
Do you want characters 219 to 562 to be the same character?  (e.g. all "1"s, or all "Z"s).  or do you want it to be a known, fixed, sequence of 346 characters?

If it is a string of all of the same character, it might be easier to switch to Perl:

perl -e '$a=@ARGV[0];while (<STDIN>) {chomp($_);printf("%s%s\n",$_,"Z"x$a);}' ${abcd} < infile > outfile

This will add $abcd lots of "Z" to the end of the line.  If the input lines are all the same length, then the output ones will be too!  It doesn't do any checking, so you must give exactly one parameter (the number of characters to add) and it must be a number.
Avatar of n78298

ASKER

Soory , the requirement keep on changing.

This is the final one.

I have got a file of 562 charcters now.  the first 218 characters are populated.The next 346 characters are populated with spaces.

These 346 characters are stored in a variable in the form of 346 character string.

I need that 346 characers to be appended after 218 characters in each line. This means the variable which is string of 346 characters to be appended after character number 218 in file.
Do you *need* to use the variable? Tthe awk script I sent on 21/04/09 (with your corrections!) will append the required 346 spaces.

Still, if the input line is *always* 218 chars, and you *must* use the variable, then the easiest might be to use something like this, with sed:

    SPACEVAR="        (346 spaces)         "
    sed "s/$/${SPACEVAR}/" < infile > outfile

I've used double-quotes round the whole sed command - you would normally enclose it in single quotes to avoid any of the sed constructs(such as the $ sign) being interprested by the shell, but the command we are using here is such that double quotes round the whole thing cause ${SPACEVAR} to be replaced with the contents of the variable, but leaves the rest of the command alone.
(I wonder how many different programs we can use for this solution?)
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of n78298

ASKER

Thanks a lot.