add only one space after each word in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add only one space after each word in a string
# 8  
Old 02-15-2012
Quote:
Originally Posted by Scrutinizer
And here is how to do it in awk:
Code:
echo "$a" | awk '$1=$1'

Superb!Could you please explain how it works in awk?

Thanks

Last edited by pandeesh; 02-15-2012 at 01:16 AM..
# 9  
Old 02-15-2012
Quote:
Originally Posted by Scrutinizer
And here is how to do it in awk:
Code:
echo "$a" | awk '$1=$1'

Thanks I'll use this one.. Smilie
# 10  
Old 02-15-2012
@pandeesh. If you assign a value to one of the fields of a record ( $1, $2, ... ) then the entire record $0 gets recalculated and the FS (input field separator) gets replaced by the output field separator (OFS). Both are at the default in this case. The default FS is " ", a single space and this has a special meaning, namely any mixture of consecutive tabs and spaces. The default OFS is also a single space, but in this case it means just that, a single space. So the end result is that every occurrence of any amount of whitespace gets replace by a single space.
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 11  
Old 02-15-2012
Is it possible to achieve the same using tr?
# 12  
Old 02-15-2012
Perhaps like so:
Code:
echo "$a" | tr -s '\t ' ' '

This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 02-15-2012
Quote:
Originally Posted by Scrutinizer
And here is how to do it in awk:
Code:
echo "$a" | awk '$1=$1'

And it seems this is equivalent to:
Code:
echo "$a" | awk '$0=$0'

# 14  
Old 02-15-2012
Quote:
Originally Posted by pandeesh
And it seems this is equivalent to:
Code:
echo "$a" | awk '$0=$0'

No, it outputs the input file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert space in a word using sed

Hi all, I have a file that looks like this- ----------------------------- . . ATOM 8 O2' U A 5 135.452 109.687 7.148 1.00 48.99 A16S ATOM 9 C1' U A 5 135.282 111.512 5.641 1.00 48.99 A16S ATOM 10 N1 U A 5 134.647 112.595 ... (2 Replies)
Discussion started by: asmi_g
2 Replies

2. Shell Programming and Scripting

How to add a character after the first word/space on each line?

Hi:) I have a large file ( couple thousand lines ) and I'm trying to add a character after the first word/space on each line. eg: First line of text Second line of text Third line of text Fourth line of text Fifth line of text I'd like to accomplish: First - line of text Second... (8 Replies)
Discussion started by: martinsmith
8 Replies

3. UNIX for Dummies Questions & Answers

Adding word in blank space

Hi, var=QSB SBD SDN SGJ SJP SKB SKD SLP SML SNB SRE SRG STP TAJ UMP UNO VKS VND VNS WAH ZRR I have to put *.sql after every word What I did echo $var>/root/file1.sql sed -i 's/ /*.sql /g' file1.sql cat file1.sql Output QSB*.sql SBD*.sql SDN*.sql SGJ*.sql SJP*.sql SKB*.sql... (9 Replies)
Discussion started by: kaushik02018
9 Replies

4. Shell Programming and Scripting

Grepping word based on white space.....

Hi, I am having a text file with following contents: word word I want to grep the first line i.e. word that is being preceded with three space characters. So i tried sed -n '/ {3}/p' filename grep " {3}" filename But is not returning any result. If i don't use {}, then it... (5 Replies)
Discussion started by: sarbjit
5 Replies

5. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

Bash take word after specific point and till next space?

Hello, I have an output like Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - and I want to take only the 'wlan0' string. This can be done by a="Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - " b=${a:25:6} echo $bThe thing is that wlan0 can be something else, like eth0 or... (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

Word up to first space

Hi All, my string is: Alabama Tax Experts I want to take Alabama in one separate variable, means every first element of a string in perl.... please help (5 Replies)
Discussion started by: gentleDean
5 Replies

10. Shell Programming and Scripting

How to get just the word and clean the white space?

Hi, I need to check if the value returned by this query is bigger then 20000. It's not working! I think that the problem is that the return is with white spaces. How to solve this? Tks, Paulo Portugal. ####################### RESPOSTA=`/oracle/app/product/10.2/bin/sqlplus -s <<EOF / as... (2 Replies)
Discussion started by: paulofp
2 Replies
Login or Register to Ask a Question