Removing spaces in the second field alone


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing spaces in the second field alone
# 1  
Old 12-20-2011
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
# 2  
Old 12-20-2011
Code:
awk -F'|'  '{gsub(" ","", $2); print $0} ' inputfile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-20-2011
Thanks!!

Initially,I tried the below code,but it din't work:
Code:
echo "abc|b f g|bj gy"|sed 's/`awk '{FS="|";print $2}'`/`awk '{FS="|";print $2}'|tr -d " "`/'

may i know what's wrong in the above code?

And , in awk -F option is not working in SunOS4.
So, i am using FS instead of that.

Thanks
# 4  
Old 12-20-2011
Use nawk instead of old awk.

Your code doesn't pass the other fields to output, just the one field you process for change.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 12-20-2011
Thanks!

My logic is :

a)Getting the Second field and passing that to sed:

Code:
sed  's/2nd field with spaces/2nd field without spaces/'

Thanks
# 6  
Old 12-20-2011
Yes, but what happens to the rest of the fields? The first awk statment gets rid of them. the next awk & sed never sees them to output them
This User Gave Thanks to jim mcnamara For This Post:
# 7  
Old 12-20-2011
ok,
Is there any sed solution possible for my original requirement?

thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing spaces between parenthesis ()

Hello, i 've go a file with the following text: oracle@das (J005) 0 oracle@das (J008) 0 oracle@das (J050) 0 oracle@das (J038) ... (15 Replies)
Discussion started by: nms
15 Replies

2. Shell Programming and Scripting

Removing blanks, spaces

I have pipe separated file with lots of blank spaces. After using sed -e 's/ *| */|/g' this command ,its giving me output as TT0000013101640| HCAMBLAMCNB010|Jul 3 2012 11:14AM| HARYANA| Bangali Mohalla | TCL-UBR|9368040005|9355264655|9218509220|NULL ... (5 Replies)
Discussion started by: sususa
5 Replies

3. UNIX for Dummies Questions & Answers

Need help removing leading spaces from one field in comma seperated file

Using awk or sed, I'd like to remove leading spaces after a comma and before a right justified number in field 6. Sounds simple but I can't find a solution. Each field's formatting must stay intact. Input: 40,123456-02,160,05/24/2012,02/13/1977, 10699.15,0 Output:... (5 Replies)
Discussion started by: Scottie1954
5 Replies

4. Solaris

Removing blank spaces

Hi , I want to go out of vi editor temporarily and execute a command in command prompt and again going back to the editor . Is it possible . Any help on this is really helpful. 1. Need to open vi 2. Temporarily come out and execute a command and go back to vi editor (6 Replies)
Discussion started by: rogerben
6 Replies

5. Shell Programming and Scripting

Removing spaces from record

HI i have record as shown below 402665,4X75,754X_FERNIE BC,12F2,008708,FERNIE BC,1,UTC ,UTC ,250 402665,4X75,754X_FERNIE BC,F212,008708,FERNIE BC,1,UTC ,UTC ,250 402665,4Y75,754Y_FERNIE BC,22F2,008708,FERNIE BC,1,UTC ,UTC ,250 here i want to remove multiple spaces into no... (3 Replies)
Discussion started by: raghavendra.cse
3 Replies

6. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 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. UNIX for Dummies Questions & Answers

Removing spaces...

Hey, I'm using the command from this thread https://www.unix.com/unix-dummies-questions-answers/590-converting-list-into-line.html to convert vertical lines to horzontal lines. But I need to remove the spaces that is created. Unfortunately I can't figure out where the space is in the code.. I... (2 Replies)
Discussion started by: lost
2 Replies

9. Shell Programming and Scripting

removing spaces

hey.. i had a problem with the unix command when i want to remove the white spaces in a string..i guess i cud do it with a sed command but i get an error when i give space in the square brackets.. string="nh hjh llk" p=`echo $string | sed 's/ //g'` i donno how to give space charater and... (2 Replies)
Discussion started by: sahithi_khushi
2 Replies

10. Shell Programming and Scripting

removing spaces after sperator

Hi friends i have problem 6000000001| CDC049| 109| CDC| 02/02/2006| Auto| New Add| 02/03/2006 6000000002| CDC033| 109| CDC| 02/02/2006| Auto| New Add| 02/03/2006 6000000003| CDC037| 109| CDC| 02/02/2006| Auto| New Add| 02/03/2006 6000000004| CDC031| ... (6 Replies)
Discussion started by: vishnu_vaka
6 Replies
Login or Register to Ask a Question