Solved

unix increasing line length in file

Posted on 2009-04-07
15
513 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX 5.3 group password policy changes 7 27
auto mounter on solaris 1 52
Write an app 10 66
Searching File Names and Eliminating Duplicates in Linux 4 63
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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
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…

937 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

7 Experts available now in Live!

Get 1:1 Help Now