How to replace specific space in files ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace specific space in files ?
# 1  
Old 02-27-2009
How to replace specific space in files ?

Hi..

Couls somebody help me on how to replace fisrt dan second space between string into comma in files ?

As example, this is an input file:-
033100012 1 Basic Voice Line
033100013 1 Basic Voice Line
033100015 1 Basic Voice Line

and i want it to be
033100012,1,Basic Voice Line
033100013,1,Basic Voice Line
033100015,1,Basic Voice Line
# 2  
Old 02-27-2009
sed 's/ /,/' file |sed 's/ /,/'
# 3  
Old 02-27-2009
Code:
sed -e 's/ /,/' -e 's/ /,/'  FILE

# 4  
Old 02-27-2009
Quote:
Originally Posted by cfajohnson
Code:
sed -e 's/ /,/' -e 's/ /,/'  FILE


Smilie Doh! can't believe I forgot -e ..
# 5  
Old 02-27-2009
Quote:
Originally Posted by rikxik
Smilie Doh! can't believe I forgot -e ..

You don't need -e:

Code:
sed '
s/ /,/
s/ /,/'  FILE

Or even:

Code:
sed 's/\([^ ]*\) \([^ ]*\) /\1,\2,/' FILE

# 6  
Old 02-28-2009
Quote:
Originally Posted by cfajohnson

You don't need -e:

Code:
sed '
s/ /,/
s/ /,/'  FILE



Hmm.. never thought like that but makes perfect sense.

Quote:
Or even:

Code:
sed 's/\([^ ]*\) \([^ ]*\) /\1,\2,/' FILE

This was the approach I took first... but repeatedly got "command garbled" so gave up. Maybe my regexs weren't in good shape Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 Replies

2. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

3. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

4. Shell Programming and Scripting

Help search and replace hex values only in specific files

perl -pi -e 's/\x00/\x0d\x0a/g' `grep -l $'GS' filelist` This isn't working :confused:, it's not pulling the files that contain the regex. Please help me rewrite this :wall:. Ideally for this to work on 9K of 20K files in the directory, I've tried this but I don't know enough about awk... (7 Replies)
Discussion started by: verge
7 Replies

5. Shell Programming and Scripting

highly specific search and replace for a large number of files

hey guys, I have a directory with about 600 files. I need to find a specific word inside a command and replace only that instance of the word in many files. For example, lets say I have a command called 'foo' in many files. One of the input arguments of the 'foo' call is 'bar'. The word 'bar'... (5 Replies)
Discussion started by: ksubrama
5 Replies

6. Shell Programming and Scripting

Replace text in SPECIFIC multiple files

I have a list of files with different file names and ext that i need replace(saved as file_list.txt.) i just want to replace from a specific file list. i obtain an error saying lint not found. Plz help. Thx perl -e "s/$NAME/$T_NAME/gi;" -pi $(find . -type f | xargs -0 lint -e < file_list.txt) (0 Replies)
Discussion started by: yvmy
0 Replies

7. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

8. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies
Login or Register to Ask a Question