Counting leading spaces to a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting leading spaces to a character
# 1  
Old 11-23-2014
Counting leading spaces to a character

data.txt

Code:
{
    "auth_type": "role",
    "default_attributes": {
        "sudoers": {

i need to know how manyspaces are before an actual character in each line of a file.

for example. in the above data.txt,

Code:
There are 0 spaces leading up to {
There are 4 spaces leading up to the line "auth_type"
There are 8 lines leading up to the "sudoers

something that would just output the number of spaces and the line number would be perfect.

my attempt:

Code:
awk '{ print length($0),NR }'

however this only prints the total number of characters in a line and the line number.

Last edited by SkySmart; 11-23-2014 at 06:51 PM..
# 2  
Old 11-23-2014
Try using match and RLENGTH:

Code:
awk '{
   match($0, /^ */);
   printf("There are %d spaces leading up to %s\n", RLENGTH, substr($0,RLENGTH+1)) }'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-23-2014
Yet another way of doing the same thing with awk...
Code:
awk -F"[ ]" '{for(i=1;i<=NF && ($i=="");i++);print "there are "i-1" spaces leading upto " $i}' file

This User Gave Thanks to shamrock For This Post:
# 4  
Old 11-23-2014
Yet another one:
Code:
awk -F'[^ ]' '{print length($1),NR}' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-27-2014
so i'm using the following to count spaces leading up to a character:
Code:
awk -F'[^ ]' '{print length($1)","NR}'

however, there some files that have a mixture of both spaces and tabs leading up to the character. and there's no way to know in which order (if spaces, then tabs, or tabs then spaces).

i'm using this to get a total of all spaces and tabs before a character:

Code:
awk -F'[^ ^\t]' '{print length($1)","NR}' file

i was wondering if there's a better way to do this. the above code only counts if the line begins with spaces or tabs. i need something to sum up both tabs and spaces that may exist before a character in each line of a file.
# 6  
Old 11-27-2014
Code:
awk -F'[^ \t]' '{print length($1)","NR}'

would be the way to do it, or do you want to count them separately ?
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-27-2014
Quote:
Originally Posted by Scrutinizer
Code:
awk -F'[^ \t]' '{print length($1)","NR}'

would be the way to do it, or do you want to count them separately ?
yes, that too may help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to remove leading spaces

OS : RHEL 6.7 Shell : bash I am trying to remove the leading the spaces in the below file $ cat pattern2.txt hello1 hello2 hello3 hello4 Expected output is shown below. $ cat pattern2.txt hello1 hello2 hello3 hello4 (2 Replies)
Discussion started by: John K
2 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

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

4. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

5. Shell Programming and Scripting

Not able to remove leading spaces

Hi Experts, In a file tht i copied from the web , i am not able to remove the leading white spaces. I tried the below , none of them working . I opened the file through vi to check for the special characters if any , but no such characters found. Your advice will be greatly appreciated. sed... (5 Replies)
Discussion started by: panyam
5 Replies

6. Shell Programming and Scripting

Leading spaces of variable removed

#!/bin/bash TESTVAR=" 5spaces" echo $TESTVAR hostame:~# ./test.sh 5spaces The leading spaces from my variable are removed when the content is echo'd. I am trying to make some tabular data. `echo -e` also fails. Any suggestions? (2 Replies)
Discussion started by: grumm3t
2 Replies

7. Shell Programming and Scripting

remove leading spaces from a line

Hi friends I need some help, I have a file which looks as follows TEMP 014637065 014637065 517502 517502 RTE 517502 517502 RTE AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635 ANOTHER_TEST 12 5433 FTHH 5653 833 TEST 123 123 3235 5353 353 53 35 353 535 3 YTERS GJK JKLS ... (6 Replies)
Discussion started by: lijojoseph
6 Replies

8. UNIX for Dummies Questions & Answers

Stripping leading spaces on right justified name

I have a name field in a file which is right justified (yep - its true). I need to strip the leading spaces from the field and write the name out left justified. Again, I think I need to use a sed or awk command but so far, my results are at best disappointing. Thank you in advance from a UNIX... (2 Replies)
Discussion started by: Marcia P
2 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. Shell Programming and Scripting

Leading and Trailing Spaces

Hi, how to i remove leading and trailing spaces from a line? the spaces can be behind or in front of any field or line example of a line in the input data: Amy Reds , 100 , /bin/sh how to i get it to be: Amy Read,100,/bin/sh i saw something on this on the Man pages for AWK... (7 Replies)
Discussion started by: sleepster
7 Replies
Login or Register to Ask a Question