awk to add space between text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to add space between text
# 1  
Old 10-30-2015
awk to add space between text

I am trying to use awk to add a space in a string of text. My awk is close but does not produce the desured output. Thank you Smilie.

input
Code:
 
washington aveLansing, MI
washington streetLansing, MI

Desired output
Code:
washington ave Lansing, MI
washington street Lansing, MI

Code:
  awk -F"\t" '{printf("%s %s %s %s\n",$1,$2,$3, $4)}' input.txt

# 2  
Old 10-30-2015
You'd need a common criterion where to put the space, stk. like a certain position in the string, a token, a special character, an attribute, ...
What do you see the strings having in common in above samples?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-30-2015
Hello cmccabe,

Could you please try following and let me know if this helps.
Code:
awk '{sub("Lansing"," &");print}'  Input_file

Output will be as follows.
Code:
washington ave Lansing, MI
washington street Lansing, MI

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-30-2015
They all have a comma at the end of $3. Thank you Smilie.
# 5  
Old 10-30-2015
... and you want the space just in front of the comma?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-30-2015
No, separating $2 from $3

This seems close but the text before the comma isn't always the same:

Code:
awk '{sub("Lansing"," &");print}'  Input_file

Thank you Smilie.
# 7  
Old 10-30-2015
Hello cmccabe,

Still we are not sure how to tackle your problem as we may require more specific information on same. Could you please try following and let me know if this helps. Let's say following is the Input_file
Code:
washington aveLansing, MI
washington streetLansing, MI
washington streettest, MI

Then following may help you in same.
Code:
awk '{split("Lansing test", A," ")} ($2 ~ /\,/){for(i in A){sub(A[i]," &");}} 1'  Input_file

Output will be as follows.
Code:
washington ave Lansing, MI
washington street Lansing, MI
washington street test, MI

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
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 add text to each line of matching id

The awk below executes as expected if the id in $4 (like in f) is unique. However most of my data is like f1 where the same id can appear multiple times. I think that is the reason why the awk is not working as expected. I added a comment on the line that I can not change without causing the script... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

awk to add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to add value and text to specific lines

In the awk I have a very large tab-delimeted file that I am trying to extract the DP= value put it in $16 and add specific text to $16 with . (dot) in $11-$15 and $18. Only the lines (there are several) that have the formating below in file will have an empty $16. Other lines will be in a... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

7. Shell Programming and Scripting

Awk/sed - add space between dot and letter

I need to change . into . so that e.g. A.Jbecomes A. JI have tried sed 's/\./\.\ /g' but that didn't work. (9 Replies)
Discussion started by: locoroco
9 Replies

8. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

9. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

10. Shell Programming and Scripting

awk or sed to add field in a text file

Hi there, I have a csv file with some columns comma sepated like this : 4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00 4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00 4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00 As you can see the column n. 7 is a timestamp column, I need... (23 Replies)
Discussion started by: capnino
23 Replies
Login or Register to Ask a Question