no count of spaces in cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting no count of spaces in cut
# 1  
Old 04-13-2007
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?
# 2  
Old 04-13-2007
sed 's/ //g' will remove spaces

could use a simpler tr command also
# 3  
Old 04-13-2007
thanks AWK
# 4  
Old 04-14-2007
Given your input:

Code:
$ echo "12 345 6 78 9
123 456 789
1 2 3 4 5 6 7 8 9"|awk '$1=$1' OFS=
123456789
123456789
123456789


If you need to substring (ex. the input given is not complete) use:

Code:
awk '$1=$1{print substr($0,1,9)}' OFS=

Use nawk on Solaris.
# 5  
Old 04-15-2007
Quote:
Originally Posted by Tártaro
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?

Code:
x="12 345 6 78 9"
set -- $x
IFS=""
q="$*"
echo "$q"

# 6  
Old 04-16-2007
Code:
printf "%s" 12 345 6 78 9 | cut -c 1-9
printf "%s" 123 456 789 | cut -c 1-9
num="1 2 3 4 5 6 7 8 9"
printf "%s" $num | cut -c 1-9

# 7  
Old 04-17-2007
Hi,
you can use sed :
echo "12 34 567 89 " | sed 's/ *//g'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I remove leading spaces in UNIX when count of space character is not fixed? Example below-

Script showStreamsGLIS$reg.$env.ksh gives me output as below- Job Stime Etime Status ExitCode GLIS-AS-S-EFL-LOCK-B ----- ----- OI 103313880/0 GLIS-ALL-Q-EOD-FX-UPDT-1730-B ----- ----- TE 0/0 GLIS-TK-S-BWSOD-B ... (8 Replies)
Discussion started by: Tanu
8 Replies

2. Shell Programming and Scripting

How To Count Fields For Cut?

I am new to cut and I want to use the field option with a space delimiter on an Apache log file. For example, if I wanted to find the 200 HTTP code using cut in this manner on the file below cat access_abc.log | cut -d' ' -f7 | grep "200" 157.55.39.183 - - "GET /content/696-news041305... (4 Replies)
Discussion started by: sharingsunshine
4 Replies

3. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

4. Shell Programming and Scripting

To Count a particular string with spaces in a file

I want to count the number of occurence of a particular string with spaces from one text file. I found one posting of a similar kind but looking for a string without spaces, there were lots of reply's which work when the string does not have any spaces between but in my case i must look into a... (7 Replies)
Discussion started by: vpv0002
7 Replies

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

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

7. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

8. UNIX for Dummies Questions & Answers

"cut" problem with leading spaces

I'm trying to use the cut command on the following output to remove the first 2 columns (this is output of "find -ls", if anyone wondering, which for some bizarre reason inserts leading spaces on AIX): file.txt: 16 4 -rw-r--r-- 1 root tech 3186 Apr 15 04:00 ./file1 2140... (4 Replies)
Discussion started by: GKnight
4 Replies

9. UNIX for Dummies Questions & Answers

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: PROCS=`ps -fu ${USERID} | egrep "MONITOR" | grep -v grep | cut -d" " -f4` However, I spotted an issue where PID's of different lengths... (3 Replies)
Discussion started by: mikem22
3 Replies

10. Shell Programming and Scripting

byte count and cut command

1. Is there a way to count the number of bytes of a variable? example: abc@yahoo.com is 13 bytes 2. Cut command only allows one byte for delimiter example: cut -f1 -d'.' delimited by period. Is there a way to have two or more characters in the delimiter field? thanks in adavance. :) (4 Replies)
Discussion started by: hemangjani
4 Replies
Login or Register to Ask a Question