wildcard for spaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers wildcard for spaces
# 1  
Old 11-19-2001
wildcard for spaces

I want to do a search and replace in a file but there may or may not be one space, two or none.

i.e.:

test 3,1
test   3,1
test,3,1
test   3,1

The output I will be looking for is this:

test 3,2
test  3,2
test,3,2
test   3,2


I have to match the first word, as the same numbers may appear through the file and I have to maintain the same number of spaces.

When I was using grep, I could use the dollar $ as a wildcard.

Can sed do this in some way? Or is there another way around it?

Thanks
Shakey21
# 2  
Old 11-19-2001
I think that
sed 's/,1/,2/'
is all you need.
# 3  
Old 11-19-2001
The above approach will work just fine. You could also use vi and simply do a global search and replace. vi the file and hit your colon key and then enter the following (thus your whole command will look like the following) ":%s/1/2/g"
# 4  
Old 11-19-2001
hmmm.... not really

I wont be using vi as I will be automating this from the script. As stated, I cannot subsitute on the comma one (,1) as it will appear in other places in the file.

I figure that I will just try this:

sed s/"test 3,1"/"test 3,2"/g
sed s/"test  3,1"/"test  3,2"/g
sed s/"test   3,1"/"test   3,2"/g
sed s/"test,3,1"/"test,3,2"/g

This is more processes than I want for the script, but for the moment it will have to do...
Shakey21
# 5  
Old 11-20-2001
You can always do several s commands with a single sed process. But I think you are looking for something like this...

sed 's/\(test[ ,]*3,\)1/\12/'
# 6  
Old 11-20-2001
Thank you, that works well...

But if it isn't asking too much, can you please provide a breakdown of that solution.

Thanks again.
Shakey21
# 7  
Old 11-21-2001
Code:
1 sed 's/\(test[ ,]*3,\)1/\12/'
2 sed 's/                /   /'  
3 sed 's/\(           \) /\1 /'
4 sed 's/\(test[ ,]*3,\) /\1 /'
5 sed 's/\(test[ ,]*3,\)1/\12/'

Line 1 and line 5 are the finished product.

Line 2 shows the basic structure of the sed substitute command. Whatever is matched in the first field is replaced by the second field.

In line 3 I have added the "\(" and "\)". Everything between them is a remembered sub-field. And I can use the remembered sub-field in the result by using "\1". I could also have other remembered sub-fields and then I would use \2 \3 etc.

In line 4 I have added what I am remembering. The only tricky part might be "[ ,]*". The [ ,] matches either a space or a comma. The * says I may have zero or more.

In line 5 I added the last two characters. That 1 is there so I can throw it away by not remembering it. The 2 is what I needed to add.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list and move files with spaces and wildcard?

I am writing a code that can move and archve all the files in a directory except the latest file based on file pattern provided in a controlfile. The filename is in the form of pattern. So basically we find the all the files of the pattern provided and archive all of them, leaving one latest file.... (3 Replies)
Discussion started by: Saanvi1
3 Replies

2. Shell Programming and Scripting

Expanding a list of wildcard filenames with spaces

I think I must be missing something obvious but I have a file containing a list of files and paths, some with wildcard, others with spaces. e.g. /this/is/a/file /this/is/a/directory/ /this/is/a/collection/* /this/has spaces/in/it /this/had spaces/and/list/of/files*... (6 Replies)
Discussion started by: mij
6 Replies

3. OS X (Apple)

Help with wildcard

CD_numb is AM017 this code: set the_Firstcom_CD to (do shell script "ls -d '/volumes/audioNAS/Firstcom/Access Music/' ") & CD_numb gives me this: "/volumes/audioNAS/Firstcom/Access Music/AM017" the item I am looking for is AM017Q. I can get the "*" syntax right so it never finder... (7 Replies)
Discussion started by: sbrady
7 Replies

4. Shell Programming and Scripting

Wildcard in ls

Hi Experts, I want to use ls in the below form: ls -l *.{txt,TXT} (working fine) but when i am declaring a variable, VAR="*.{txt,TXT}" ls -l $VAR is not working. Please help. Thanks. (4 Replies)
Discussion started by: sugarcane
4 Replies

5. Shell Programming and Scripting

How to use wildcard * in if?

Hi, Can anyone help me how to use * in if statement. File contains below line1:a|b|c|Apple-RED| line2:c|d|e|Apple-Green| line3:f|g|h|Orange| I need to find line by line 4th field contains 'Apple' or not. Please help me at the earliest. (6 Replies)
Discussion started by: jam_prasanna
6 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. Shell Programming and Scripting

wildcard

Hi, I have this code to search all "cif" files using wildcard for file in *.cif do grep "Uiso" $file | awk '{ print $3, $4, $5 }' > tet done I get this error "grep: *.cif: No such file or directory" Please where am I going wrong!!! Thank you in advance (6 Replies)
Discussion started by: princessotes
6 Replies

8. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

9. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question