LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-28-2007, 05:44 AM   #1
orfeo
LQ Newbie
 
Registered: May 2007
Posts: 3

Rep: Reputation: 0
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...
 
Old 05-28-2007, 07:58 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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.
 
Old 05-28-2007, 09:39 AM   #3
orfeo
LQ Newbie
 
Registered: May 2007
Posts: 3

Original Poster
Rep: Reputation: 0
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...
 
Old 05-28-2007, 10:07 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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
 
Old 05-28-2007, 10:32 AM   #5
orfeo
LQ Newbie
 
Registered: May 2007
Posts: 3

Original Poster
Rep: Reputation: 0
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...
 
Old 05-28-2007, 10:34 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Nice to read it works
 
  


Reply


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
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
Add file content to a variable (bash)? LinuxSeeker Programming 4 12-19-2005 01:41 PM
bash script with spaces Quantum0726 Programming 2 11-14-2005 09:26 PM
Spaces in bash parameters rose_bud4201 Programming 5 03-11-2005 06:57 AM
Bash crashes ? File names with () and spaces Danodare Slackware 1 02-27-2004 02:50 PM


All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration