Solved

unix increasing line length in file

Posted on 2009-04-07
15
524 Views
Last Modified: 2013-12-06
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.
0
Comment
Question by:n78298
  • 8
  • 6
15 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 24094527
do you want to join the lines or to add extra space?

can you give example? is this a text file?
0
 

Author Comment

by:n78298
ID: 24094796
This is a text file. ( Flat File).  I just want to add extra speces at the end.
0
 

Author Comment

by:n78298
ID: 24095752
Any solution for the above problem.
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 19

Expert Comment

by:simon3270
ID: 24095877
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).
0
 
LVL 19

Expert Comment

by:simon3270
ID: 24138121
Did my awk script answer your question?

If not, is there anything else I need to add?
0
 

Author Comment

by:n78298
ID: 24192315
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.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 24192672
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!)
0
 

Author Comment

by:n78298
ID: 24192715
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("%-"${  <<<
0
 

Author Comment

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

Missed the single quote before double qote. Working now.
0
 

Author Comment

by:n78298
ID: 24192892
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.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 24193976
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.
0
 

Author Comment

by:n78298
ID: 24213354
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.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 24213842
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?)
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 24214408
Just noticed that you said you had already added the 346 spaces to have 562-characeer lines - do you need the contents of the variable to *replace* those added spaces?  If so, somtheing like:

    cut -c1-218 < infile | sed "s/$/${SPACEVAR}/" > outfile

would do it, though it woudl be better not to have added the spaces already, and simply use the previous sed command on the 218-character lines.
0
 

Author Closing Comment

by:n78298
ID: 31567902
Thanks a lot.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question