[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

unix increasing line length in file

Posted on 2009-04-07
15
Medium Priority
?
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

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 20

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 20

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 20

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 20

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 20

Accepted Solution

by:
simon3270 earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

656 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