"cut" problem with leading spaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers "cut" problem with leading spaces
# 1  
Old 04-15-2009
"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:

Code:
   16    4 -rw-r--r--  1 root      tech          3186 Apr 15 04:00 ./file1
 2140    0 -rw-r--r--  1 root      security         0 Apr 15 06:30 ./file2
 2141   18 -rw-r--r--  1 root      security     18126 Apr 15 09:00 ./file3
 1072    1 -rw-r--r--  1 root      tech           112 Apr 15 09:00 ./file4
  637   15 -rwxr-----  1 root      system       14515 Apr 15 12:08 ./file5
   18    2 -rwxr-x---  1 root      tech          1977 Apr 15 12:16 ./file6
 1140    1 -rw-r--r--  1 root      tech           128 Apr 15 09:00 ./file7
 1320    1 -rw-r--r--  1 root      tech            37 Apr 14 14:54 ./file8

output of "cat test.txt | cut -d ' ' -f 3-"

Code:
 16    4 -rw-r--r--  1 root      tech          3186 Apr 15 04:00 ./file1
   0 -rw-r--r--  1 root      security         0 Apr 15 06:30 ./file2
  18 -rw-r--r--  1 root      security     18126 Apr 15 09:00 ./file3
   1 -rw-r--r--  1 root      tech           112 Apr 15 09:00 ./file4
637   15 -rwxr-----  1 root      system       14515 Apr 15 12:08 ./file5
 18    2 -rwxr-x---  1 root      tech          1977 Apr 15 12:16 ./file6
   1 -rw-r--r--  1 root      tech           128 Apr 15 09:00 ./file7
   1 -rw-r--r--  1 root      tech            37 Apr 14 14:54 ./file8

Seems like the leading spaces are causing this problem. Is there a workaround to this as far as cut is concerned? Perhaps it's easier to just remove the leading spaces (not sure how to do it), any help will be appreciated.
# 2  
Old 04-15-2009
You have more than one space as a field separator,
you may consider using another tool:

Code:
sed 's/\( *[^ ]* *\)\{2\}//' infile


Last edited by radoulov; 04-15-2009 at 02:18 PM.. Reason: 3 -> 2
# 3  
Old 04-15-2009
Quote:
Originally Posted by radoulov
You have more than one space as a field separator,
you may consider using another tool:

Code:
sed 's/\( *[^ ]* *\)\{2\}//' infile

This isn't working, I'm just getting the file content.

I'm guessing I just need a regular expression to cut out the leading spaces. I came up with this monstrosity, there's gotta be a better way to do this:

Code:
cat test.txt | sed 's/^ *//' | cut -d " " -f 2- | sed 's/^ *//' | cut -d " " -f 2-

I'm doing it twice because the second column has the same problem.
# 4  
Old 04-15-2009
As I already said it's not because of the leading but because of the multiple spaces. I suppose that the sed implementation you're using does not support re-interval, try this instead:
Code:
sed 's/ *[^ ]* *[^ ]* *//' infile

# 5  
Old 04-15-2009
This one works quite well, thanks! I think I understand what this regular expression does, the first is a bit too advanced for me Smilie
 
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 use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

2. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

3. Shell Programming and Scripting

How to replace "®" "™" "with Spaces in UNIX

do U know how to replace the registered trademark "®" symbol or trade Mark "™" "with Spaces in UNIX (4 Replies)
Discussion started by: MMeari
4 Replies

4. Shell Programming and Scripting

"read" command ignoring leading spaces

I have to read a file line by line, change it and then update the file. Problem is, when i read the file, "read" command ignores leading spaces. The file is a script which is indented in many places for clarity. How to i make "read" command read leading spaces as well. (3 Replies)
Discussion started by: vickylife
3 Replies

5. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

6. Shell Programming and Scripting

How to cut a file using " ", but fields can be separated with more than one " "

Hello, let's say I have a text file: word11 word12 word13 word21 word22 word23 word31 word32 word33 and I want to put the second field of each line into a list: set list = `cut -d" " -f2 ${1}` and I use space (" ") as a delimiter, only that there's a catch: there can be more than... (12 Replies)
Discussion started by: shira
12 Replies

7. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

8. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

9. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

10. Windows & DOS: Issues & Discussions

Unix "cut' and "awk" in Windows XP?

Hi, How can I execute Unix's ksh equivalent of "cut' and "awk" in Windows XP? For example, I want to execute ksh commands from Windows command prompt. Is there a place I can download "cut.exe" and "awk.exe" ? Thanks in advance (4 Replies)
Discussion started by: ihot
4 Replies
Login or Register to Ask a Question