Replace only first found white spaces with some other characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace only first found white spaces with some other characters
# 1  
Old 04-17-2009
Replace only first found white spaces with some other characters

Anybody can help me

How can I replace only four first white spaces with , or any other characters

aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64
bedad 08/16/2001 08/15/2011 permanent Logical Processors in System: 64
badnv14 05/31/2008 05/30/2013 permanent Logical Processors in System: 96
badob10 03/30/2006 03/29/2011 permanent Logical Processors in System: 16
badb19 05/31/2008 05/30/2013 permanent Logical Processors in System: 56
bhpar20 04/01/2009 04/30/2009 temporary Logical Processors in System: 32

I think we can do it in one command in unix but I am not able to thanks
I will appreciate the help
# 2  
Old 04-17-2009
There maybe a way to do it in one line, but this is what I came up with.

This will replace the first instance
Code:
echo "aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64" | sed 's/ /,/'

This will replace the second instance
Code:
echo "aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64" | sed 's/ /,/2'

This will replace the thirdinstance
Code:
echo "aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64" | sed 's/ /,/3'


If you run the first one 3 times on the string it should output what you want.
# 3  
Old 04-17-2009
Code:
# cat t.log
aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64
bedad 08/16/2001 08/15/2011 permanent Logical Processors in System: 64
badnv14 05/31/2008 05/30/2013 permanent Logical Processors in System: 96
badob10 03/30/2006 03/29/2011 permanent Logical Processors in System: 16
badb19 05/31/2008 05/30/2013 permanent Logical Processors in System: 56
bhpar20 04/01/2009 04/30/2009 temporary Logical Processors in System: 32

# cat test.sh
declare -a FILES
COUNT=0
while read line
do
        line=`echo $line | sed 's/ /,/'`
        line=`echo $line | sed 's/ /,/'`
        line=`echo $line | sed 's/ /,/'`
        echo $line
done < "t.log"

# sh test.sh
aaaa,08/31/2004,08/31/2009,permanent Logical Processors in System: 64
bedad,08/16/2001,08/15/2011,permanent Logical Processors in System: 64
badnv14,05/31/2008,05/30/2013,permanent Logical Processors in System: 96
badob10,03/30/2006,03/29/2011,permanent Logical Processors in System: 16
badb19,05/31/2008,05/30/2013,permanent Logical Processors in System: 56
bhpar20,04/01/2009,04/30/2009,temporary Logical Processors in System: 32

# 4  
Old 04-17-2009
You can do it that way, but it is probably easier to do the changes in a single sed run. It is possible to combine several sed-commands to a single script:

Code:
cat file | sed -n 's/ /,/;s/ /,/;s/ /,/;s/ /,/;s/ /,/p' > newfile

Of course it is possible to create a more complex regexp to change the first four spaces to commas in one sed-command, but it will probably be not any faster (or any more readable) than the presented solution:

Code:
cat file | sed 's/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) /\1,\2,\3,\4,/' > newfile

I hope this helps.

bakunin
# 5  
Old 04-17-2009
Code:
awk '{for(i=1;i<=4;i++){$(i)=$(i)","}}1' file

# 6  
Old 04-18-2009
Hi.

The GNU version of sed allows substitution of a specific numbered occurrence of a string in a line. Here is an example showing that feature for blanks and 4 other characters:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate (GNU) sed substitution of nth occurrence of string.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
sed -e 's/ /,/4' -e 's/ /=/3' -e 's/ /;/2' -e 's/ /@/1' $FILE

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
GNU sed version 4.1.2

 Data file data1:
a b c d e
x y z

 Results:
a@b;c=d,e
x@y;z

This is not significantly different from the other solutions with sed in this case, but may be handy to know about in other situations.

You can modify the form to use a single instruction by separating the individual sed commands with semicolons as in other posts ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing blank/white spaces and special characters

Hello All , 1. I am trying to do a task where I need to remove Blank spaces from my file , I am usingawk '{$1=$1}{print}' file>file1Input :- ;05/12/1990 ;31/03/2014 ; Output:- ;05/12/1990 ;31/03/2014 ;This command is not removing all spaces from... (6 Replies)
Discussion started by: himanshu sood
6 Replies

2. Shell Programming and Scripting

Replace white spaces of multiple files at once

How to rename multiple files by replacing the white spaces with underscore for ex: 293TrexFH\ \ \ GSM855007RINGB_lhb11_binary.txt 293TrexFH\ \ \ GSM855007RINGB_lhb12_binary.txt 293TrexFH\ \ \ GSM855007RINGB_lhb13_binary.txt 293TrexFH_GSM855007RINGB_lhb11_binary.txt... (1 Reply)
Discussion started by: quincyjones
1 Replies

3. AIX

Replace all TAB characters with white spaces

Dear Gurus Can you please advise me on how to Replace all TAB characters with white spaces in a text file in AIX? Either using vi or any utilities (2 Replies)
Discussion started by: tenderfoot
2 Replies

4. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

5. Shell Programming and Scripting

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 Replies

6. UNIX for Dummies Questions & Answers

replace characters with spaces between tag

I have a file where in some records are having the <Start> and <End> tag. There is data before the start tag , between the tages and after the End tag. I want to replace everything between the start & end tag with equivalent spaces. Input File afsdfaksddfs<start>12678<end>sgdfgdfsf... (6 Replies)
Discussion started by: varunrbs
6 Replies

7. Solaris

removing special characters, white spaces from a field in a file

what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code:: Code: awk -F":"... (2 Replies)
Discussion started by: priyanka3006
2 Replies

8. Shell Programming and Scripting

(g)awk how to preseve white spaces (FS characters) or read a right subpart of $0?

Hi, I am using gawk (--posix) for extracting some information from something like the following lines (in a text file): sms_snath_hp_C/CORE BUILD PREREQUISITE: total 1556 drwxrwxrwx 2 sn sn 4096 2008-06-27 08:31 ./ drwxrwxrwx 13 sn sn 4096 2009-07-22 14:48 ../ -rwxrwxrwx 1 sn sn ... (14 Replies)
Discussion started by: shri_nath
14 Replies

9. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

10. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies
Login or Register to Ask a Question