Help needed counting spaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help needed counting spaces
# 1  
Old 06-30-2006
Question Help needed counting spaces

I was wondereing if somebody could point me in the right direction - below is a small script I have put together to check that lines in a file I recieve are the correct length (371) and to output any incorrect lines to a .txt file. However when I run this it does not seem to count spaces as characters Smilie - could anybody help me to include the spaces in the count?


#!/bin/ksh
break_length=371

get_values() {

echo "Start Check" > "errorrec.txt"
while read INREC
do
length=$(echo ${INREC} | wc | awk '{print $3}')
if ((${length} != ${break_length} )); then
echo ${INREC} >> "errorrec.txt"
fi
done < "47050001_2005.1"
}

get_values
# 2  
Old 06-30-2006
is this ok !!! ?

Code:
awk '{ if(length > 327) print }' input > output

# 3  
Old 06-30-2006
Code:
#!/bin/ksh
break_length=371

get_values() {

echo "Start Check" > "errorrec.txt"
while read INREC
do
  length=${#INREC}
  if [[ ${length} -ne ${break_length} ]] ; then
    echo ${INREC} 
  fi
done < "47050001_2005.1"  >> "errorrec.txt"
}

get_values

A couple of code suggestions on your ksh script:
...ksh has a builtin for length ${#variable}
...when you want the output of a loop you can redirect the whole loop if you want
# 4  
Old 07-03-2006
I managed to get it to work just thought I'd post my final version for completeness.

Code:
#!/bin/ksh
break_length=371

get_values() {
        export IFS=:
        export OLDIFS=$IFS
        echo "Start Check" > "errorrec.txt"
while read INREC
do
   length=`echo "${INREC}" | wc | awk '{print $3}'`
   if ((${length} != ${break_length} )); then
      echo ${INREC} >> "errorrec.txt"
   fi
done < "47050001_2005.1"
}

get_values

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Counting leading spaces to a character

data.txt { "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, There are 0 spaces leading up to { There are 4 spaces leading up to the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. UNIX for Dummies Questions & Answers

counting?

Hi all, I promise this is my very last dumb question.. but how to you count how many unique names you have. My dataset is: >Bac1 afdsgrr >Bac4 egege >Bac8 dgrjh >Bac1 afdsgrr >Bac1 afdsgrr >Bac8 dgrjh What i want to know is that how many unique names there is, so the output would... (3 Replies)
Discussion started by: Iifa
3 Replies

4. Shell Programming and Scripting

Counting

Hi, The following output shows how many pmon process are started by users named : oracle or yoavb $ ps -ef |grep pmon |grep -v grep |grep -v ipmon oracle 11268 1 0 Sep 2 ? 36:00 ora_pmon_qerp oracle 17496 1 0 Oct 11 ? 8:58 ora_pmon_bcv oracle 15081 1 0 ... (5 Replies)
Discussion started by: yoavbe
5 Replies

5. Shell Programming and Scripting

Counting

Hi, I want to count how many rows are in a file for a specific column. eg. K NM K NM K NM K JK K NM K JK K NM so the file is tab-delimited. I want to count how many rows are in column 2 and how many NMs there are. I used awk awk '{OFS="\t"}; {count++} {print i,... (3 Replies)
Discussion started by: phil_heath
3 Replies

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

7. Shell Programming and Scripting

counting users?

Is it possible to count the number of users? or specifically emac users? I know that you can count certain file sizes, like find /usr/bin/ -size 11k -exec ls {} \;|wc -1 but how would I count users? (3 Replies)
Discussion started by: gordonheimer
3 Replies

8. Programming

Counting characters, words, spaces, punctuations, etc.

I am very new to C programming. How could I write a C program that could count the characters, words, spaces, and punctuations in a text file? Any help will be really appreciated. I am doing this as part of my C learning exercise. Thanks, Ajay (4 Replies)
Discussion started by: ajay41aj
4 Replies

9. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

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