Spaces in names between quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaces in names between quotes
# 1  
Old 12-14-2015
Spaces in names between quotes

command i'm running:

Code:
echo "services": [ "check_win_semsrv_process", "checking logs", "check_win_semwebsrv_process", "check_win_semlaunchsrv_process" ] "contactgroups": [ "HQ-InfoSec-DG", "nag-snower", ], | awk -F"]" '{print $1}' | cut -d'[' -f2- | sed 's_ _\n_g' | sed '/^\s*$/d'


which provides the following output:

Code:
check_win_semsrv_process,
checking
logs,
check_win_semwebsrv_process,
check_win_semlaunchsrv_process

As you can see here, it outputs everything just fine expect for when it got to the name that has a space in it "checking logs". the goal here is to have that be on one line like every other name. so the output should be:

Code:
check_win_semsrv_process,
checking logs,
check_win_semwebsrv_process,
check_win_semlaunchsrv_process


Any suggestions?
# 2  
Old 12-14-2015
You can do all the work just with awk and let the quotes be your friend.
Code:
echo '"services": [ "check_win_semsrv_process", "checking logs", "check_win_semwebsrv_process", "check_win_semlaunchsrv_process" ] "contactgroups": [ "HQ-InfoSec-DG", "nag-snower", ],' | awk -F'[][]' '{gsub(/",/, ",\n", $2);gsub(/ ?"/, "", $2);print $2}'

Code:
check_win_semsrv_process,
checking logs,
check_win_semwebsrv_process,
check_win_semlaunchsrv_process

This User Gave Thanks to Aia For This Post:
# 3  
Old 12-14-2015
I'm not clear on what you are trying to do.
The commands:
Code:
echo "services": [ "check_win_semsrv_process", "checking logs", "check_win_semwebsrv_process", "check_win_semlaunchsrv_process" ] "contact groups": [ "HQ-InfoSec-DG", "nag-snowed", ],

and:
Code:
echo services: [ check_win_semsrv_process, checking logs, check_win_semwebsrv_process, check_win_semlaunchsrv_process ] contact groups: [ HQ-InfoSec-DG, nag-snowed, ],

both produce exactly the same output:
Code:
services: [ check_win_semsrv_process, checking logs, check_win_semwebsrv_process, check_win_semlaunchsrv_process ] contact groups: [ HQ-InfoSec-DG, nag-snowed, ],

So why are you wasting time putting double-quotes in an echo command that you know are going to be stripped by the shell before echo sees its arguments? Is this echo command producing output that is an accurate representation of the data that you really want the rest of this pipeline to process?

Aia,
Since the submitter doesn't want the double quotes anyway. A simpler awk script (ignoring the unneeded double-quotes in the original echo command) would be:
Code:
echo "services": [ "check_win_semsrv_process", "checking logs", "check_win_semwebsrv_process", "check_win_semlaunchsrv_process" ] "contact groups": [ "HQ-InfoSec-DG", "nag-snowed", ], |
    awk -F" [][] " '{gsub(/, /,",\n",$2);print $2}'

But, of course, this only works if the character sequence <comma><space> never appears inside a what might have been intended to be a double-quoted string in the field that is being processed by awk.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 12-14-2015
im sorry guys. i actually want the double quotes.

the preferred output should be:

Code:
"check_win_semsrv_process",
"checking logs",
"check_win_semwebsrv_process",
"check_win_semlaunchsrv_process"

Under normal circumstances, i'd be quick to snatch up the awk commands that were provided so far in this thread. but in this particular case, i need a way to preserve the space between the "checking logs" and have it be on the same line as shown above.

the initial command i provided in my first post and which is also provided below is necessary in this scenario unless there's a better awk version of it that does exactly the same thing:


Code:
awk -F"]" '{print $1}' | cut -d'[' -f2- | sed 's_ _\n_g' | sed '/^\s*$/d'

# 5  
Old 12-14-2015
So, back to my suggestion, but this time preserving the double-quotes in your echo and looking for quoted, <comma><space> delimited fields:
Code:
echo '"services": [ "check_win_semsrv_process", "checking logs", "check_win_semwebsrv_process", "check_win_semlaunchsrv_process" ] "contact groups": [ "HQ-InfoSec-DG", "nag-snowed", ],' |
    awk -F" [][] " '{gsub(/", "/,"\",\n\"",$2);print $2}'

which produces the output:
Code:
"check_win_semsrv_process",
"checking logs",
"check_win_semwebsrv_process",
"check_win_semlaunchsrv_process"

And, as before, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun 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

Spaces in double quotes in variable ?

Hi got this issue and was wondering if someone could please help out ? var='." "' echo $var ." " I 'll get ." " and not ." with 10 spaces in between " Thanks (3 Replies)
Discussion started by: stinkefisch
3 Replies

2. Shell Programming and Scripting

Remove spaces from start of file names

Hi, I have a directory with the following file names 01 - abc hyn 02-def 03-ghi.dir 04 - jhu.dir abc1 kil def bil The last two file names abc1 starts with one space and def starts with double space. I want these files in my directory to be renamed as ABC HYN DEF GHI.dir... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

3. Shell Programming and Scripting

How to replace spaces excluding those within double quotes and after backslash?

In bash or perl, I would like to know how to substitute a null character (0x00) for every white space without changing the white spaces inside the block of double quotes and the white space immediately following a backslash. Suppose that sample.txt consists of the following line. "b 1" c\ 2 ... (2 Replies)
Discussion started by: LessNux
2 Replies

4. Shell Programming and Scripting

How to strip the spaces in file names?

please somebody tell me what is wrong with this, while the thumbnail grabbing works and encoding works, but what is not working is, mv $i.jpg /var/www/thumbs/ and mv $i.mp4 /var/www/uploads/ #!/bin/bash # MINT 9 - FFMPEG - QT-FASTSTART - X264 - MP4 DIR=/var/www/tmp for i in... (9 Replies)
Discussion started by: mysoogal
9 Replies

5. Shell Programming and Scripting

Opening File names with spaces inside it !- PERL

I developed a perl code..And the excerpt from it is given below... open(HANDLE,$cmp_path) ; #reading the xml file from the file path while($file_path = <HANDLE>) I have list of XML files to read from a folder. It has some spaces inside the name of the file...I used "\"... (2 Replies)
Discussion started by: gameboy87
2 Replies

6. Shell Programming and Scripting

sed replace spaces between quotes with a variable

I have lines with: elseif (req.http.host ~ "^(www.)?edificationtube.com$|www.edificationtube.org www.edificationtube.net edificationtube.org www.edificationtube.com edificationtube.net") { elseif (req.http.host ~ "^(www.)?collegecontender.com$|www.collegecontender.com collegecontenders.com... (3 Replies)
Discussion started by: EXT3FSCK
3 Replies

7. Shell Programming and Scripting

Remove spaces between file names

Hi All, I have spaces in between file names. "Material Header.txt" "Customer Header.txt" "Vendor Header.txt" And how can I remove spaces between file names like below MaterialHeader.txt CustomerHeader.txt VendorHeader.txt Thanks Srimitta (10 Replies)
Discussion started by: srimitta
10 Replies

8. Shell Programming and Scripting

Strong quotes and spaces

We ran into a problem because of a shop that uses Windows and UNIX. The file names that Windows uses have spaces in them. When they get moved to the unix system they still have spaces. This produces a problem in our script that moves them again from one unix system to another. I've made up a... (2 Replies)
Discussion started by: jimcampanella
2 Replies

9. Shell Programming and Scripting

File names with spaces? Is it possible?

Gurus - I got one simple TXT file with long file name with blank spaces in between the words. I am trying to display that full file name, but it breaks while displaying. Could somebody shed some light here? Script ------ for i in `cat ~\temp\employee.txt` do echo $i done (5 Replies)
Discussion started by: Eric_2005
5 Replies

10. Shell Programming and Scripting

Directory names that contain spaces and other suggestions?

The script below was written to select files and convert a particular string to something other and replace that file. However, I came across some issues with filenames that contain spaces, any suggestions to get around this? Any other suggestions that may apply to this code would also be... (5 Replies)
Discussion started by: Shakey21
5 Replies
Login or Register to Ask a Question