Solved

unix increasing line length in file

Posted on 2009-04-07
15
501 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
Comment Utility
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
Comment Utility
This is a text file. ( Flat File).  I just want to add extra speces at the end.
0
 

Author Comment

by:n78298
Comment Utility
Any solution for the above problem.
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
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
Comment Utility
Did my awk script answer your question?

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

Author Comment

by:n78298
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:n78298
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now