Replace spaces with underscores up to first comma but not after the comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace spaces with underscores up to first comma but not after the comma
# 1  
Old 08-25-2016
Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file:

Code:
EK ED,Elementary and Kindergarten Education
OBGYN,Obstetrics/gynecology
BL EB,"Engineering, Business, and Human Development"
CONTE,Continuing Education Students
STAFF,Student Affairs
BANTH,Biological Anthropology
C PSY,Community Psychology
LAIDS,Liberal Arts Interdisciplinary Programs

The first line should become:
Code:
EK_ED,Elementary and Kindergarten Education

The second line should remain unchanged
The third line should become:
Code:
BL_EB,"Engineering, Business, and Human Development"

etc

Last edited by Scrutinizer; 08-25-2016 at 12:41 PM.. Reason: code tags
# 2  
Old 08-25-2016
Hello tdouty,

Welcome to forums, hope you will enjoy learning and sharing knowledge here.
Could you please try following.
Code:
awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1'  Input_file

Output will be as follows.
Code:
EK_ED Elementary and Kindergarten Education
OBGYN,Obstetrics/gynecology
BL_EB "Engineering  Business  and Human Development"
CONTE,Continuing Education Students
STAFF,Student Affairs
BANTH,Biological Anthropology
C_PSY Community Psychology
LAIDS,Liberal Arts Interdisciplinary Programs

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-26-2016 at 12:47 AM.. Reason: Added welcome note for user.
# 3  
Old 08-25-2016
Awesome! That seemed to work. Thank you so much!

I used it this way:
Code:
cat all_major_codes.txt | awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1' > new_file


Last edited by Scrutinizer; 08-25-2016 at 12:42 PM.. Reason: code tags
# 4  
Old 08-25-2016
To keep the comas (based on RavinderSingh13 post):
Code:
awk -F, '{gsub(/[[:space:]]/,"_",$1)} 1' OFS=, infile

# 5  
Old 08-25-2016
Thank you. I did have to keep the commas so added that to the command.
# 6  
Old 08-25-2016
One more thing (in case the line has no coma):
Code:
awk -F, '/,/ {gsub(/[[:space:]]/,"_",$1)} 1' OFS=, infile

# 7  
Old 08-25-2016
The character class [:space:] may be sort of overkill if only spaces shall be replaced, as it consists of several chars, man isspace:
Quote:
these are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

2. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

3. UNIX for Dummies Questions & Answers

How to replace two or more spaces with one comma?

I'm using sh on hp-ux. I've got a file that looks like this. -5.65 175 -16.17 160 -13.57 270 -51.72 260 -8.30 360 -42.71 460 -.38 375 -.20 375 -4.15 170 -21.53 560 -18.84 360 I'd like to replace all the whitespace between the columns with one comma. I can't... (4 Replies)
Discussion started by: Scottie1954
4 Replies

4. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

5. 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

6. Shell Programming and Scripting

How to grep after the first comma till the next comma in a line

Hi Can any one pls tell me how to grep this line POPULATION,69691,20120509 I want the number 69691 from the above line. How to grep from the first comma till the next comma. Thank You.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

7. Shell Programming and Scripting

Replace the | with Comma

Hi, The input file structure is given below: The Col1 and Col2 will be there always. But from Col3 there can be more columns. And Col3 will be always Col4 and Col5 will always be with Col6. I need to replace the | with comma. There are scnearios where there wont be no data.Below, the row 2... (6 Replies)
Discussion started by: bharathappriyan
6 Replies

8. Shell Programming and Scripting

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 Replies

9. Shell Programming and Scripting

Pull Data After Comma if 2 word before comma

Hi, I am trying to truncate word after comma in a file ONLY if there are already 2 words BEFORE comma. If there is one word or 3 or more words BEFORE comma, then I have to leave the data AS IS. See below for example. Input File : John Smith, Manager Smith, John Frank J F K... (2 Replies)
Discussion started by: msalam65
2 Replies

10. Shell Programming and Scripting

Replace comma with newline

Hi, for some reason I cant seem to figure this out. I have a file which looks something like this word word word word word,word,word word word word,word,word,word,word word word Basically I want this whole thing to be a list with 1 word on each line like this... word word word... (1 Reply)
Discussion started by: eltinator
1 Replies
Login or Register to Ask a Question