Removing Double Spaces for cut command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing Double Spaces for cut command
# 1  
Old 05-22-2008
Removing Double Spaces for cut command

Hi

I have a shell script that looks for running processes in order to kill them. The script (ksh) gets the PID of these processes using the following:

Code:
PROCS=`ps -fu ${USERID} | egrep "MONITOR" | grep -v grep | cut -d" " -f4`

However, I spotted an issue where PID's of different lengths would cause the logic to fail (since the ps -fu command aligns columns.. therefore there is sometimes a double space between the UID and PID columns (issue a ps -fu $USER to see this)

So, I thought using sed to globally replace the double space with a single would be the way to go....

Code:
PROCS=`ps -fu ${USERID} | egrep "MONITOR" | grep -v grep | sed -e "s/  / /g" | cut -d" " -f3`

This returns the PID fine for instances where there is a double or single space between the UID and PID coulmns of the ps -fu command but fails if there are 3 or more spaces. I guess the sed pattern matching doesn't cope with instances like this (where, say 4 spaces should be replaced with a single one).

Any ideas on how to get around this problem using either the above as a starting point, or maybe an alternative approach?

Thanks in advance.

Mike

Last edited by Yogesh Sawant; 05-22-2008 at 12:43 PM.. Reason: added code tags
# 2  
Old 05-22-2008
Hammer & Screwdriver what about trimming out all extra spaces

You could do something like:

Code:
ps -fu $USER | tr -s " " | cut -d" " -f3

the tr -s " "
command eliminates extra " " space characters
# 3  
Old 05-22-2008
that's it

great.. that's what I wanted. I looked at the tr command but not with the -s switch. That now works fine, thanks.

Mike
# 4  
Old 05-22-2008
Solaris ps command can do this....

ps -fo 'pid= args=' -u $procUser

Making the PID the first field in the output obviating cut, tr, etc. Note that you can specify the order of any field in the full output. In the above case I was only interested in the pid and args to the command, but all the others are available too...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing double spaces in code

I want to be able to spot double spaces in code and then also replace it with a single space. Double spaces sometimes occur accidentally in code, but most often they are indents or seperate code from comments ("//"). In summary: I want to replace double spaces with single spaces where they do not... (6 Replies)
Discussion started by: figaro
6 Replies

2. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

3. Shell Programming and Scripting

Cut command skips spaces

Hi everyone I have a file looks like this X01 1 JOE 20100312 X02 TOD 20100312 X03 3 SAM 20100312 I wrote a script to assign the values in 5 columns to 5 variables. When I use cut command to assign column 2 or 3, it skips it if it is a space (see below) ... (3 Replies)
Discussion started by: nimo
3 Replies

4. Shell Programming and Scripting

Include white spaces while using CUT command

Hi I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing. file 1 <name>Kumar Rajasekaran</name> cut -c7-26 "file1" the out put i received is ... (48 Replies)
Discussion started by: Sekar1
48 Replies

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

6. Shell Programming and Scripting

Removing spaces from the output of a head command

Im running the below commands in a script. Total_confirms=`cat $OUTPATH/count_po* | head -4 | tail -1` # echo "Total Confirms records we need: $Total_confirms" >> $LOG2 The problem is its always returning 4 spaces before the result.. Can I pipe the result into something else to remove the... (5 Replies)
Discussion started by: Jazmania
5 Replies

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

8. UNIX for Advanced & Expert Users

How can I use double character delimiter in the cut command

Hi All, Can the cut command have double character delimiter? If yes, how can we use it. if my data file contains : apple || mango || grapes i used cut -f1 -d"||" filename but got an error. Plz help.... Thanks. (1 Reply)
Discussion started by: AshishK
1 Replies

9. Shell Programming and Scripting

no count of spaces in cut

i want to give numbers to cut without worrying about the spaces; echo "12 345 6 78 9" | cut -c 1-9 echo "123 456 789" | cut -c 1-9 echo "1 2 3 4 5 6 7 8 9" | cut -c 1-9 the output of these three must be allways; 123456789 is that posible? (6 Replies)
Discussion started by: Tártaro
6 Replies

10. 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
Login or Register to Ask a Question