Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need a script that inputs a file and echos its contents with linebreaks removed - "word wrapped" so to speak.

I've tried:

#!/bin/bash
find $HOME '$1' | while read line; do echo "${line}"; done

but it just returns nothing.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Try doing this:

echo $(cat $1)
share|improve this answer
    
Perfect, thanks! –  southpointingchariot Sep 13 '12 at 0:35
    
Glad I could help you out finding one-out-of-many-ways-to-get-there, but try Googling a bit more next time. This is what I got as a second result popping up using the keywords bash, line, end, remove. –  gertvdijk Sep 13 '12 at 0:46

I found this relevant answer in stackoverflow:

http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n

The quickest way appears to be using tr:

tr '\n' ' ' </your/file

or for your example:

cat `find $HOME '$1"` | tr '\n' ' '

To learn more about the tr command do, of course, man tr.

The stackoverflow anser contains ways to do this with sed (would have been my first choice, but sed is very line-oriented and getting it to not interpret line breaks as field delimiters is arcane) and a way using read and bash, close to your initial approach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.