LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add spaces to a file using bash (https://www.linuxquestions.org/questions/linux-newbie-8/add-spaces-to-a-file-using-bash-557182/)

orfeo 05-28-2007 05:44 AM

Add spaces to a file using bash
 
Hello, I've been trying lately to replace certain characters in a file with that same character, adding spaces before and after. I tried with sed and awk, but it doesn't seem to work. My script reads like this right now:

#!/bin/bash

for i in $( cat char_file );
do
p_space=" $i "
gawk -v pattern="$i" -v p_space=" $i " '{gsub(pattern,p_space);}' target_file

done

The characters include numbers, @, #...

May anybody lend me a hand?

Thanks in advance...

druuna 05-28-2007 07:58 AM

Hi,

Here's a solution using sed:
Code:

#!/bin/bash

for i in $( cat char_file )
do
  sed "s/$i/ $i /g" infile
done

Hope this helps.

orfeo 05-28-2007 09:39 AM

It still doesn't work
 
Thanks for answering but it doesn't work... I already tried something like that and it seems to me that sed has problems with the spaces...

druuna 05-28-2007 10:07 AM

Hi,

What doesn't work/ is the error message?

It seems to work on my box:
Code:

$ cat char_file
a
|
@

$ cat infile
that same a
this@that
foo|bar

$ ./blaat.sh
th a t s a me  a
this@th a t
foo|b a r
that same a
this@that
foo | bar
that same a
this @ that
foo|bar


orfeo 05-28-2007 10:32 AM

I beg your pardon, the char file was corrupt. It works right now. I changed it and everything went into place inmediately. I just don't know why I wasn't getting a error message. Thanks a lot for your assistance...

druuna 05-28-2007 10:34 AM

Hi,

Nice to read it works :)


All times are GMT -5. The time now is 12:56 PM.