LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-17-2011, 04:17 AM   #16
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233

also, if the OP wants a wrapping version:
Code:
#!/bin/bash

SPACES='                                                                                '

while read LINE; do
    while I=${#LINE}; [[ I -gt 80 ]]; do
        echo "${LINE:0:80}"
        LINE=${LINE:80}
    done

    if [[ I -eq 80 ]]; then
        echo "$LINE"
    else
        echo "$LINE${SPACES:0:80 - I}"
    fi
done
or less logic consistent but simpler form
Code:
#!/bin/bash

SPACES='                                                                                '

while read LINE; do
    while I=${#LINE}; [[ I -gt 80 ]]; do
        echo "${LINE:0:80}"
        LINE=${LINE:80}
    done

    echo "$LINE${SPACES:0:80 - I}"
done

Last edited by konsolebox; 04-17-2011 at 04:24 AM.
 
2 members found this post helpful.
Old 04-21-2011, 05:59 PM   #17
di11rod
Member
 
Registered: Jan 2004
Location: Austin, TEXAS
Distribution: CentOS 6.5
Posts: 207

Original Poster
Rep: Reputation: 32
Wow. I am really impressed at the response I've received to this request for help. This is amazing. I wish to extend a BIG thanks to everyone who chimed in here with advice.

I've applied the AWK suggestion provided by Telengard, but there seem to be a bit of unexpected output in the resulting file. From what I can tell, there are now 80 characters on each line, but embedded at the end of the real text on each line is a ^M, which looks like a linefeed. I didn't get that on my other set of files that I applied the AWK command against. Just this one batch.

Code:
awk '{printf "%-80s\n", $0}' onefile > twofile
The recipient of this file is trying to import it into some kind of system that expects a very strict format of data being at exact character locations. The import failed and I'm wondering if it's due to these embedded ^M characters. Does anyone know how I can scrub these from the output?

Screen capture attached.

Thanks again for the help provided thus far and for any additional help you may send my way.

Appreciatively,


di11rod
Attached Images
File Type: png 80_car_snip_7M.PNG (52.0 KB, 7 views)
 
Old 04-21-2011, 06:12 PM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by di11rod View Post
've applied the AWK suggestion provided by Telengard, but there seem to be a bit of unexpected output in the resulting file. From what I can tell, there are now 80 characters on each line, but embedded at the end of the real text on each line is a ^M, which looks like a linefeed. I didn't get that on my other set of files that I applied the AWK command against. Just this one batch.
That's becasue you're using Windows.

Linux uses linefeeds to separate lines, Windows does it differently (carriage return followed by linefeed).
 
1 members found this post helpful.
Old 04-21-2011, 06:38 PM   #19
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Usually, ^M's (carriage returns) are a result of editing the file with a DOS-oriented tool. Sometimes Linux editors (nedit, for example) can read and/or write in DOS end-of-line format, so you might try an editor in your collection. There are also various small tools around that convert between DOS and Unix. This should also work:
Code:
tr -d '\r' < dosfile > unixfile
--- rod.
 
1 members found this post helpful.
Old 04-21-2011, 07:03 PM   #20
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
I'm not exactly certain why this is happening, but Windows' 0x0d 0x0a line endings are most likely complicating things. I wonder what will happen if you try this.

Code:
awk 'BEGIN {RS = "\r\n"} {printf "%-80s\n", $0}' onefile > twofile
 
1 members found this post helpful.
  


Reply

Tags
awk, line, pad, printf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
head adds chars to end of each line (Red Hat Enterprise Linux) CheckiSt Linux - General 13 04-14-2010 03:52 AM
Shell scripting: How to add characters at the end of the line Micro420 Programming 7 05-18-2007 01:56 AM
deleting all spaces at end of line in vi Tonatiuh Linux - General 4 08-16-2006 02:34 PM
got a syntax error which shows unexpected end of line when tried to run a shell scrip racer_mec Linux - Newbie 1 01-10-2005 01:43 AM


All times are GMT -5. The time now is 04:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration