add semicolumn and remove spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add semicolumn and remove spaces
# 1  
Old 05-25-2010
add semicolumn and remove spaces

Hi

I have a file like:
Code:
one  two         three  four          five
six   seven      eight  nine          ten

and I'd like to get
Code:
one:two:three:four:five

I'm trying to do it with sed, but the problem is also that the spaces between the words are not constant.
Any idea please?

thanks

D.

Last edited by Scott; 05-25-2010 at 10:11 AM.. Reason: Replaced QUOTE tags with CODE tags for better readability
# 2  
Old 05-25-2010
Code:
$ sed "s/  */:/g" file1
one:two:three:four:five
six:seven:eight:nine:ten

# 3  
Old 05-25-2010
Try:
Code:
sed 's/  */:/g' infile

Code:
awk '$1=$1' OFS=: infile

Code:
tr -s ' ' ':' < infile


Last edited by Scrutinizer; 05-25-2010 at 10:20 AM..
# 4  
Old 05-25-2010
thx a lot for the fast reply
D.
# 5  
Old 05-25-2010
Code:
sed 's/\s\+/:/g' < file

# 6  
Old 05-25-2010
i just wanted to ask something here. if i have a file very similar to the given above

Code:
one  two      3748901   90  NULL    0   3892031               three  four          five
six   seven      eight  nine          ten

and i want the output in the way as u said earlier. (given below)
Code:
one:two:three:four:five


will this one command will work ???

Code:
sed 's/\s\+/:/g' < file

If yes. then my 2nd question..

my file has like 2000 records and each record is given like :-


Code:
one  two      3748901   90  NULL    0   3892031               three  four          five
six   seven      eight  nine          ten                                          one  two      3748901   90  NULL    0   3892031               three  four          five
six   seven      eight  nine          ten                              one  two      3748901   90  NULL    0   3892031               three  four          five
six   seven      eight  nine          ten

first record starts from (a random number like 3748901) how will get the desired output in the way like :-

Code:
3748901:one:two:three:four:five:9748934
3748901:one:two:three:four:five:9748934
3748901:one:two:three:four:five:9748934

what i am trying to say is there is anther data in my file in each record that is exactly similar to the first record. the only difference between the first record and the between lying data is that.
my 1st record starts with integer value 9 and other one is always something else.

any ideas on this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to remove leading spaces

OS : RHEL 6.7 Shell : bash I am trying to remove the leading the spaces in the below file $ cat pattern2.txt hello1 hello2 hello3 hello4 Expected output is shown below. $ cat pattern2.txt hello1 hello2 hello3 hello4 (2 Replies)
Discussion started by: John K
2 Replies

2. Shell Programming and Scripting

Remove spaces from the file

Hi All, The output file contains data as below. "20141023","CUSTOMER" ,"COMPANY" ,"IN0515461" ,"" ,"JOSHUA" There are spaces in between the ending " and ,. The number of spaces is random. How can I remove that from the file so that the final output is:... (4 Replies)
Discussion started by: aarsh.dave
4 Replies

3. Shell Programming and Scripting

How to remove spaces on a line?

Hi, suppose I have the following data: albert music=top40 age=20 bob music=punk rock age=25 candy music=r n b age=22 dave music=mozart or bach only age=30 I want to extract and manipulate the music column but it's got spaces in it. How can I substitute the space with an underscore... (2 Replies)
Discussion started by: almonds
2 Replies

4. Shell Programming and Scripting

Remove spaces in a file

Hi friends, I have a file1.txt 1 | a | 4757634 | jund jdkj | erhyj 2 | a | 4757634 | jnd jdkj | rhje hjrhwj i have used tr -d '\040' to remove the spcaes output file cat file1.txt | tr -d '\040' 1|a|4757634|jundjdkj|erhyj... (5 Replies)
Discussion started by: i150371485
5 Replies

5. Linux

How to remove spaces in the file?

hiii i have a file that contains spaces in the begining of a file till the middle the from there the txt would appear. hw can i remove those spaces and bring the text to the begining portion file1 text starts from here (12 Replies)
Discussion started by: anurupa777
12 Replies

6. Shell Programming and Scripting

remove spaces

Hi folks, I need to remove spaces at the end of each line in a *.txt file. it looks like this word 1 word 2 . . . word n i found some sed commands but any of them didnt work so far thank you for your posts (6 Replies)
Discussion started by: Jimmy7
6 Replies

7. UNIX for Dummies Questions & Answers

remove spaces in between file

hey, I have this file: ATOM 2510 HG12 VAL 160 8.462 15.861 1.637 ATOM 2511 HG13 VAL 160 9.152 14.510 0.725 ATOM 2512 CG2 VAL 160 6.506 16.579 -0.088 ATOM 2513 HG21 VAL 160 5.499 16.421 -0.478 ATOM 2514 HG22 VAL 160 6.417 16.984 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

8. Shell Programming and Scripting

remove spaces between tags

I am having the data.txt file as follows. ------- <RMService> <ResControl> <ResultCode>FATAL</ResultCode> <ServiceTime>38</ServiceTime> <DWLControl> <requesterLanguage>100</requesterLanguage> <requesterLocale>en</requesterLocale> <requesterName>NCO A Batch... (6 Replies)
Discussion started by: kmanivan82
6 Replies

9. Shell Programming and Scripting

Remove Spaces

Hi All, I have a comma seperated file. I wanna remove the spaces from column 2. I mean i don't wanna remove the spaces those are presnt in column 1. ex: emp name, emp no, salary abc k, abc 11, 1000 00 bhk s, bhk 22, 2000 00 the output should be: emp name, emp no, salary abc k, abc11,... (4 Replies)
Discussion started by: javeed7
4 Replies

10. Shell Programming and Scripting

adding semicolumn at end of line

Hi , I need to add semicolumn at the end of each line in a file. can any one help me in this? Thanks in advance (2 Replies)
Discussion started by: scorpio
2 Replies
Login or Register to Ask a Question