|
Home > Archive > Unix Shell > February 2006 > How to insert a new line using BASH script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to insert a new line using BASH script
|
|
| linq936@hotmail.com 2006-02-22, 6:04 pm |
| Hi,
Let us say the file is
123
234
345
And I want to replace 123 to "12 \n 3" in a BASH script. I tried SED,
but it does not work. I googled this group, there are some suggestions,
but they all are in command line, or some sed scipt.
I wonder if I could do it in BASH script without resort to seperate
sed script.
Thanks.
| |
| Stephane CHAZELAS 2006-02-22, 6:04 pm |
| 2006-02-22, 12:11(-08), linq936@hotmail.com:
> Hi,
> Let us say the file is
>
> 123
> 234
> 345
>
> And I want to replace 123 to "12 \n 3" in a BASH script. I tried SED,
> but it does not work. I googled this group, there are some suggestions,
> but they all are in command line, or some sed scipt.
[...]
sed 's/123/12\
3/'
--
Stéphane
| |
| Chris F.A. Johnson 2006-02-22, 6:04 pm |
| On 2006-02-22, linq936@hotmail.com wrote:
> Hi,
> Let us say the file is
>
> 123
> 234
> 345
>
> And I want to replace 123 to "12 \n 3" in a BASH script.
Do you mean "12 \n 3" or:
12
3
?
> I tried SED, but it does not work.
What did you try? What does "doesn't work" mean? What happened
exactly?
Did you try:
sed 's/^123$/12\
3/'
Or:
sed 's/^123$/12\n3/'
Or:
sed 's/^123$/12 \\n 3/'
> I googled this group, there are some suggestions,
> but they all are in command line, or some sed scipt.
Anything typed at the command line can be put in a script.
> I wonder if I could do it in BASH script without resort to seperate
> sed script.
If the file is large, sed or awk will be much faster than a pure
bash script.
If you want to do it in bash (or any Bourne-type shell):
## If your file is different, you may want:
## while IFS= read -r line
while read line
do
case $line in
123) printf "%s\n%s\n" 12 3 ;;
*) printf "%s\n" "$line"
esac
done < FILE
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| linq936@hotmail.com 2006-02-22, 6:04 pm |
|
Stephane CHAZELAS wrote:
> 2006-02-22, 12:11(-08), linq936@hotmail.com:
> [...]
>
> sed 's/123/12\
> 3/'
>
>
> --
> St=E9phane
Thanks for your reply.
But this does not work for me. When I run it in command line, it works,
but it does not in BASH script.
When I have it in scipt, "123" =3D=3D> "12 3".
Any idea?
|
|
|
|
|