Remove spaces from between words that are in a field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove spaces from between words that are in a field
# 1  
Old 07-16-2013
Remove spaces from between words that are in a field

Hi all,

Is there a sed/awk cmd that will remove blank space from between words in a particular field, replacing with a single space?

Field containing 'E's in the example below:

Example input file:
Code:
AAAAA AA|BBBB|CCCCCCC|DDDDDD      |EEEE        EEEEEE|   FFF   FFFFF|

Required output:
Code:
AAAAA AA|BBBB|CCCCCCC|DDDDDD      |EEEE EEEEEE|   FFF   FFFFF|

Any help much appreciated
# 2  
Old 07-16-2013
considering its 5th field..

Code:
 
awk -F"|" '{sub(/[ ]+/," ",$5);OFS=FS}1' filename

# 3  
Old 07-16-2013
Excellent! Thankyou.

Is there a solution for a columnar structure rather than a delimitted structure?

input:
Code:
AAAAA AA   BBBB    CCCCCCC    DDDDDD        EEEE        EEEEEE    FFF   FFFFF

output:
Code:
AAAAA AA   BBBB    CCCCCCC    DDDDDD        EEEE EEEEEE    FFF   FFFFF

# 4  
Old 07-16-2013
Unless you have a fixed length for each field, you cannot differentiate between fields.

For instance,
Code:
DDDD                 EEEEEEE

Suppose the above represent two adjacent fields, do you count the spaces after DDDD along with field 1 or as leading space for field 2. Where does field 1 end and where does field 2 start?
# 5  
Old 07-16-2013
Thanks for you reply,

What if I was able to supply the start position and length of the field?

Code:
StartPos=45
Length=22
 
AAAAA AA   BBBB    CCCCCCC    DDDDDD        EEEE        EEEEEE    FFF   FFFFF

or is beyond the capability of awk / sed?
# 6  
Old 07-16-2013
u can.. but bit messy though

Code:
 
awk '{v=substr($0,45,22);sub(/[ ]+/," ",v);print substr($0,0,44) v substr($0,68)}' filename

This User Gave Thanks to vidyadhar85 For This Post:
# 7  
Old 07-16-2013
Thankyou all for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep words with spaces and save the output

I have a file that contains the schedule for a tournament with 41 teams. The team names have spaces in them. I would like to search for each teams schedule and then save that to that teams file For example Team name: "Team Two" I would like to search for all the games for "Team Two" and... (8 Replies)
Discussion started by: knijjar
8 Replies

2. Shell Programming and Scripting

Concatenating words without spaces.

Hi All, I have written a C program to solve this problem but I am eager to know whether the same output can be obtained using sed or awk? This is the input: star ferry computer symbol prime time This is the output: starferry ferrycomputer computersymbol symbolprime primetime (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

3. UNIX for Dummies Questions & Answers

Removing spaces in the second field alone

Consider my input string as "abc|b f g|bj gy" I am expecting the output as "abc|bfg|bj gy". Please let me know how to achieve this in unix? Thanks (8 Replies)
Discussion started by: pandeesh
8 Replies

4. Shell Programming and Scripting

Insert varying length spaces between words

Hey all, Fist post, so be kind... I have written an expect script which logs into a terminal and gathers several screens of information. Unfortunately the log file gives me all the special escape and control characters from the terminal. I am hoping to use a combination of shell scripting, sed,... (1 Reply)
Discussion started by: mpacer
1 Replies

5. Shell Programming and Scripting

Help with removal of blank spaces from the second field!

Hi everyone.. I'm trying to eliminate multiple whitespaces from a file.. I must make use of shell script to eliminate whitespaces.. Take a look at the sample file 1 int main() 2 { 3 int a,b; 4 printf("Enter the values of a and b"); 5 scanf("%d%d",&a,&b); 6 if(a>b) ... (6 Replies)
Discussion started by: abk07
6 Replies

6. Shell Programming and Scripting

Trying to use sed to remove the value of one field from another field

I'm trying to use sed to remove the value of one field from another field. For example: cat inputfile 123|ABC|Generic_Textjoe@yahoo.com|joe@yahoo.com|DEF 456|GHI|Other_recordjohn@msn.com|john@msn.com|JKL 789|MNO|No_Email_On_This_One|smith@gmail.com|PQR I would like to remove the email... (2 Replies)
Discussion started by: bribri87
2 Replies

7. Shell Programming and Scripting

spaces in array field

I have a file, names(i) where each entry is 'first last' name. 'cat names' is fine. But in a shell script >for file in $(cat names) > do > echo $file > done the first and last name appear on 2 lines. I have tried escaping and quoting the space but to no avail. The names are to be... (4 Replies)
Discussion started by: chuckmg
4 Replies

8. Programming

Counting characters, words, spaces, punctuations, etc.

I am very new to C programming. How could I write a C program that could count the characters, words, spaces, and punctuations in a text file? Any help will be really appreciated. I am doing this as part of my C learning exercise. Thanks, Ajay (4 Replies)
Discussion started by: ajay41aj
4 Replies

9. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

10. Shell Programming and Scripting

Retaining spaces between words

Retaining Spaces within a word -------------------------------------------------------------------------------- Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces.... (7 Replies)
Discussion started by: RcR
7 Replies
Login or Register to Ask a Question