Separate letters and replace whitespaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separate letters and replace whitespaces
# 1  
Old 06-16-2013
Separate letters and replace whitespaces

Input file:
Code:
aaaa bbb dd.
qqq wwww e.

Output file:
Code:
a a a a <s> b b b <s> d d .
q q q <s> w w w w <s> e .

Can I use sed to do so in one step?
# 2  
Old 06-16-2013
Code:
awk  '{for(i=1;i<=NF;i++) printf "%s ",$i; print ""}' FS="" infile
a a a a   b b b   d d .
q q q   w w w w   e .

# 3  
Old 06-16-2013
Thanks!
But how can I also add <s> in the previously existing spaces?
# 4  
Old 06-16-2013
Code:
awk  '{a="";for(i=1;i<=NF;i++) a=a sprintf("%s ",$i);gsub(/   /," <s> ",a);print a}' FS=""
a a a a <s> b b b <s> d d .
q q q <s> w w w w <s> e .

I thought the <s> was representing space, but this fix it.
# 5  
Old 06-16-2013
Code:
sed 's/./& /g; s/[[:blank:]]\{2,\}/ <s> /g'

Without an extra space at the end:
Code:
sed 's/./& /g; s/[[:blank:]]\{2,\}/ <s> /g; s/ $//'


Last edited by MadeInGermany; 06-16-2013 at 08:31 AM..
# 6  
Old 06-16-2013
Based on MadeInGermany sed you can tweak my regex some Smilie
Did forget the nice &
Code:
awk '{gsub(/./,"& ");gsub(/   /," <s> ")}1'

If you like to remove the extra space added after final letter . in all example above add $1=$1
Code:
awk  '{gsub(/./,"& ");gsub(/   /," <s> ");$1=$1}1'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find & Replace with same case letters

I have text with upper and lower case words. I want to find something and replace it with something new. But it should match the case - Meaning - it should replace old upper cased word with NEW upper case word and lower with lower. example: this text is very simple TEXT. now I want to replace... (5 Replies)
Discussion started by: grep_me
5 Replies

2. Shell Programming and Scripting

Retaining whitespaces...

There's an input file(input.txt) which has the following details : CBA BA <Please note the second record has a LEADING WHITESPACE which is VALID> I am using the following code to read the content of the said file line by line: while read p ; do echo "$p" done < input.txt This is the... (1 Reply)
Discussion started by: kumarjt
1 Replies

3. Shell Programming and Scripting

Replace trailing whitespaces with pipe symbol using perl

I want to replace the whitespace with Pipe symbol to do a multiple pattern matching for the whole text "mysqld failed start" and same as for other text messages Below are the messages stored in a file seperate by whitespace mysqld failed start nfsd mount failed rpcbind failed to start for... (6 Replies)
Discussion started by: kar_333
6 Replies

4. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

5. Shell Programming and Scripting

Replace lower case letters with N

Hi, I have a string of letters that are upper and lower case. I would like to replace all the lowercase letters with N. Also, there are only 4 letters, so it may be easier to replace each lower case letter with N. Either way, I do not know know to do this. Example line ... (6 Replies)
Discussion started by: mikey11415
6 Replies

6. Shell Programming and Scripting

Substring in string with whitespaces

hello, i have this string: variable="1234 /PARAMETER_1:text /PARAMETER_2:othertext" i tried to do expr match $variable '.*\(*\)' but i keep getting expr error i need to extract the word text... thank you (4 Replies)
Discussion started by: Aloush89
4 Replies

7. Shell Programming and Scripting

if $variable has whitespaces?

Hi, Here's my requirement... There's a SFTP script with batchmode which logs in to the remote site and list all .done files to pick real files (see the following snippet) echo "-ls $FTP_PATH/*.done" > temp_file fileNames=`sftp -b temp_file $user@$ip` isDoneFile=`echo $fileNames |... (2 Replies)
Discussion started by: dips_ag
2 Replies

8. Shell Programming and Scripting

Replace a line with a separate line in code

I have a bunch of files that are like this: <htmlstuffs>HTML STUFFS</endhtmlstuffs> <h1>Header</h1> <htmlstuffs>HTML STUFFS</endhtmlstuffs> <h1>Unique</h1> <html stuffs>HTML STUFFS</endhtmlstuffs> And Here's what I'd like it to look like: <htmlstuffs>HTML STUFFS</endhtmlstuffs>... (2 Replies)
Discussion started by: kason
2 Replies

9. Shell Programming and Scripting

replace nulls with whitespaces in a file

Hi, i have a file which contains data in fixed length. each row contains data of 10 characters fixed length. The data in the file appears like 4567782882 some times i may recieve dat less than fixed length of 10. in such a case i find nulls appended at the trailing spaces when i do a... (2 Replies)
Discussion started by: meeragiri
2 Replies

10. UNIX for Dummies Questions & Answers

trimming whitespaces in a file

Hello, Im trying write a bash sript to search a file and identify any file names that contain a space at the end of them. I was trying to use the grep command but I can find how I would identify the character I'm looking for. Any help would be grately appreciated. (2 Replies)
Discussion started by: gintreach
2 Replies
Login or Register to Ask a Question