Shell Scripts - removing line endings
#1
Scooby Regular
Thread Starter
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes
on
0 Posts
Shell Scripts - removing line endings
I'm using bash/sh and have a script which does the following:
touch NameOfThefile
echo "Data In the File" >> NameOfThefile
The resulting file is used by an application, and it reads the data from the file and does something with it. However, the app requires that the data is on a single line with no new line or carriage return characters at the end and the echo command is adding a newline/carriage return to the end of the file.
So the question is, how can I preserve the text in the file but remove the line ending? Is there another way, via shell, to write data to a file that would not result in the line ending being put there, if so, how do I do it?
Thanks in advance.
touch NameOfThefile
echo "Data In the File" >> NameOfThefile
The resulting file is used by an application, and it reads the data from the file and does something with it. However, the app requires that the data is on a single line with no new line or carriage return characters at the end and the echo command is adding a newline/carriage return to the end of the file.
So the question is, how can I preserve the text in the file but remove the line ending? Is there another way, via shell, to write data to a file that would not result in the line ending being put there, if so, how do I do it?
Thanks in advance.
#2
Scooby Regular
Simple script to remove all eol markers from a file:
#!/bin/bash
#
# remove_eol.sh - script to remove eol markers from file
#
INPUT="NameOfThefile"
while read line
do
echo -n ${line}
done < ${INPUT}
When running the script you will need to redirect the output to a new filename otherwise it will simply write to screen:
remove_eol.sh > NewFilename
Hope this helps.
Jonny.
#!/bin/bash
#
# remove_eol.sh - script to remove eol markers from file
#
INPUT="NameOfThefile"
while read line
do
echo -n ${line}
done < ${INPUT}
When running the script you will need to redirect the output to a new filename otherwise it will simply write to screen:
remove_eol.sh > NewFilename
Hope this helps.
Jonny.
Last edited by Jonnys3; 17 October 2009 at 03:21 PM.
Thread
Thread Starter
Forum
Replies
Last Post