sed : replace space and end-of-line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed : replace space and end-of-line
# 1  
Old 03-19-2010
sed : replace space and end-of-line

Hi !

I'm rather new with sed ... learned a lot already by googling etc ...

The following script should replace all spaces and ends-of-lines with "something (see below).

Code:
#!/bin/bash

i=0
while read line
do 
fam="H`printf "%06d" $i`" 
echo $line | sed -e 's/[$ ]/\t'$fam'\n/g' 
i=$(($i+1)) 
done < $1

However, this only seems to replace the spaces, not the ends-of-line.
If I put only
Code:
echo $line | sed -e 's/$/\t'$fam'\n/g'

it does replace the ends-of-line.

To be more specific, if my file looks like this :

a b c
d e

I want it to become

a H000000
b H000000
c H000000
d H000001
e H000001

If anyone has a suggestion : thanks !
Jos
# 2  
Old 03-19-2010
With awk:
Code:
awk '{for(i=1;i<=NF;i++){printf("%s H%06d\n", $i, c++)}}' file

Or shorter:

Code:
awk '{printf("%s H%06d\n", $0, c++)}' RS="[ \n]" file

# 3  
Old 03-19-2010
Code:
$
$ cat f1
a b c
d e
$
$ cat f1.sh
#!/bin/bash
i=0
while read line
do
fam="H`printf "%06d" $i`"
echo $line | sed -e "s/$/\t$fam/g" -e "s/ /\t$fam\n/g"
i=$(($i+1))
done < $1
$
$ . f1.sh f1
a       H000000
b       H000000
c       H000000
d       H000001
e       H000001
$
$

tyler_durden

And using Perl:

Code:
$
$ cat f1
a b c
d e
$
$ perl -plne 'chomp; $x=sprintf("H%06d",$.); s/ /\t$x\n/g; s/$/\t$x/g' f1
a       H000001
b       H000001
c       H000001
d       H000002
e       H000002
$
$

# 4  
Old 03-19-2010
Thanks tyler_durden !

Of course, the solution is two sed commands, which gets rid of a superfluous newline as well ... I told you I was new :-)

This leaves me with the question why [\t ] seems to work while [$ ] doesn't ...
but I have a working solution, so no need for philosophical questions.

Franklin52, your awk solution gets the last number wrong.

Thanks again !
jos
# 5  
Old 03-19-2010
Quote:
Originally Posted by jossojjos
Franklin52, your awk solution gets the last number wrong.
jos
Oops, misread the question, try this one:
Code:
awk '{for(i=1;i<=NF;i++){printf("%s H%06d\n", $i, c)}c++}'  file

# 6  
Old 03-19-2010
Yes, that one does the job.

I appreciate the awk and perl suggestions of both of you ... they seem more concise than sed, so I'll try get familiar with them soon !
# 7  
Old 03-19-2010
If you have python installed,

Code:
fh = open ("file")
global i
i = 0
for line in fh:
        i+=1
        for each in line.strip().split(" "):
                print '%s H%06d' % (each,i)
fh.close

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

2. Shell Programming and Scripting

simple sed question - replace from phrase to end of line

I have the following line an in input file I want to digest with sed and simple replace the bold part with a variable defined in my bash script. I can do this in several sed operations but I know there must be a way to do it in a single sed line. What is the syntax? Line in file:... (1 Reply)
Discussion started by: graysky
1 Replies

3. Shell Programming and Scripting

How to Add space at the end of each line in linux

hi,. I am writing a small script in csh... Can any one tel me how to add space at end of each line in a file (9 Replies)
Discussion started by: Manju87
9 Replies

4. Shell Programming and Scripting

Replace end of line with a space

for eg: i have i/p file as: ================ i wnt to change end of line ================= my require ouput is like: i wnt to change end of line ==================== (7 Replies)
Discussion started by: RahulJoshi
7 Replies

5. Shell Programming and Scripting

how to replace new line ( \n ) with space or Tab in Sed

Hi, how to replace new line with tab in sed ... i used sed -e 's/\n/\t/g' <filename > but its not working (1 Reply)
Discussion started by: mail2sant
1 Replies

6. Shell Programming and Scripting

gnu sed replace space with new line

please help in making sed singleline command i need to insert dos new line (CR LF) before " 34 matching device(s) found on \\cpu1." " 6 matching device(s) found on \\cpu7." " 102 matching device(s) found on \\mainserver." the problem is that sometimes there are both CR LF before strings and... (1 Reply)
Discussion started by: xserg
1 Replies

7. Shell Programming and Scripting

Add white space to the end of a line with sed

Im trying to add 5 blank spaces to the end of each line in a file in a sed script. I can figure out who o put the spaces pretty much anywhere else but at the end. thanks Karl (7 Replies)
Discussion started by: karlanderson
7 Replies

8. Shell Programming and Scripting

Replace space, that is not in html tags <> with new line using sed

Hi, I am working on transforming html code text into the .vert text format. I want to use linux utility sed. I have this regexp which should do the work: s/ \(?!*>\)/\n/g. I use it like this with sed: echo "you <we try> there" | sed 's/ \(?!*>\)/\n/g' ... The demanded output should be: you <we... (5 Replies)
Discussion started by: matt1311
5 Replies

9. UNIX for Advanced & Expert Users

how can I read the space in the end of line

cat file1|while read i do echo "$i"|wc done with this command the space in the end of the line not considered how can solve that for example: read h "hgyr " echo "$h"|wc 4 (2 Replies)
Discussion started by: Ehab
2 Replies

10. Shell Programming and Scripting

to see space, tab, end of the line chracters

what can I use ?? In vi, I can use :set list <-- and see end of line $.. or use cat -A but I am wondering if there is command or program that allows me to see all the hidden characters( space, tab and etc) Please help thanks. (3 Replies)
Discussion started by: convenientstore
3 Replies
Login or Register to Ask a Question