Problem with cut and wc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with cut and wc
# 1  
Old 05-24-2012
Problem with cut and wc

Hi, I am facing issue with cut and wc. here is the sample.

the data in file -
Code:
tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-58
WatsSaver - AGGREGATED PLAN1581 CALLS FOR 2872.6

tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-58|wc -c
        51
  
tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-59
WatsSaver - AGGREGATED PLAN1581 CALLS FOR 2872.6 M

 tail -1 05_19_BT_TBL_LOAD_20120524064242.bad|cut -c9-59|wc -c
      52

Its should give result as 50 and 51 respectively, why 51 and 52? Does it count any character twice?

Last edited by Scott; 05-24-2012 at 10:56 AM.. Reason: Code tags for code
# 2  
Old 05-24-2012
It counts the linefeed at the end of the line as a character.
Code:
echo "1234"|wc -c
5
echo "1234"|tr -d '\n'|wc -c
4

# 3  
Old 05-24-2012
When you are asking cut to cut columns from 9 to 58, aren't you asking it cut 51 columns?
# 4  
Old 05-24-2012
Quote:
Originally Posted by elixir_sinari
When you are asking cut to cut columns from 9 to 58, aren't you asking it cut 51 columns?

This data is getting loaded into database by using sql loader based on character position. The loader has thrown error that "value too large for column" (actual: 51, maximum: 50).

I saw the error and tried to test it on shell with actual data. I took the data from position 9-58 and counted. As other lines got loaded successfully, it has created error like this. That's what my confusion is, how its counting one more character?

Last edited by donadarsh; 05-24-2012 at 11:10 AM..
# 5  
Old 05-24-2012
OK..as methyl said, newlines are also counted. An octal dump will verify this.

Code:
tail -1 test1
done

tail -1 test1|cut -c 1-3
don

tail -1 test1|cut -c 1-3|wc -c
       4

tail -1 test1|cut -c 1-3|od -bc
0000000  144 157 156 012
           d   o   n  \n
0000004

You can use tr to delete the new-line...
# 6  
Old 05-24-2012
okay, thanks a lot for helping me to understand the wc -c work. But the confusion still remains (SQL loader part) for the same data other rows have been loaded but some are not. Is there any possibility that other character are included in the same line but not visible to me on shell prompt?
# 7  
Old 05-24-2012
What Operating System and version are your running and what Shell is this?

Are any Microsoft Operating Systems involved anywhere in the process?

What character set do you use? There is scope for UTF to give misleading character counts if the database was not set to expect UTF.

Is this all within the same computer? i.e. no file transfers, conversions or whatever?

Try the od command mentioned in earlier posts on a bad record.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in using cut command

Hi Friend , i have one file say xyz.lst and it has content like dn: cn=m.hariharan,cn=employee,cn=delhi circle,cn=users,dc=industowers,dc=c dn: cn=ajay.jain,cn=employee,cn=gujarat circle,cn=users,dc=industowers,dc=com dn: cn=ajitkumar.thakor,cn=employee,cn=gujarat... (4 Replies)
Discussion started by: harish anand
4 Replies

2. UNIX for Dummies Questions & Answers

Problem with echo and cut

Hi, My source file happens to be src.src with following contents: 1,liv 52,syrup,twice a day,children When I do a : for ii in `cat src.src` do echo $ii medicine_name=`echo $ii | cut -f -d2","` echo $medicine_name done I get output of echo $medicine_name as : an the output of... (2 Replies)
Discussion started by: dipanchandra
2 Replies

3. Shell Programming and Scripting

Problem in Cut and paste

Hi, I have a file like this --> Consider z as space #cat filename ABC <!--Nzzzzz--> <!--RESUMO--> EFG XYZ <!--Nzzzzz--> <!--RESUMO--> I need to cut the <!--RESUMO--> part and paste it to the previous line so that the file will look like this--> ABC <!--Nzzzzz--><!--RESUMO-->... (4 Replies)
Discussion started by: samsonata
4 Replies

4. Shell Programming and Scripting

Problem with cut command

Hi! I get a md5 hash of a file with this command: openssl md5 /Users/me/MyLogo.png | cut -f 2 -d ' ' "cut" because I just want the hash. But there is a problem -> that doen't work with a path with spaces: openssl md5 /Users/me/MyLogo 2.png | cut -f 2 -d ' ' The result is "2.png)=" ...... (1 Reply)
Discussion started by: Dirk Einecke
1 Replies

5. Shell Programming and Scripting

CUT command problem

Hi I have a strange problem when using cut command when i am using the below command, it is working fine,I am getting the data in new file xyz.dat cut -c 1-75 abc.dat > xyz.dat when i am using the below command, I am getting the data in new file abc.dat , but empty file cut -c 1-75... (4 Replies)
Discussion started by: vaas
4 Replies

6. Shell Programming and Scripting

Problem with cut

I need to read in a file and output it without the comments or newlines. The problem is that is not outputting it properly. STUDENTSDETAILFILE="../data/studentDetails.txt" for getlines in `sed '/#/d' $STUDENTSDETAILFILE` do STUDENTID=`echo $getlines | cut -d: -f1` ... (1 Reply)
Discussion started by: nerdbot
1 Replies

7. Shell Programming and Scripting

Cut and WhiteSpace Problem

Hi, I have a shell script that reads a parameter file to set variables. I have an issue when the parameter I try to read contains whitespace. e.g File Contents Code The result is SUBJECT is set to and I want subject set to I've tried different variations but nothing seems to... (19 Replies)
Discussion started by: Greygor
19 Replies

8. Shell Programming and Scripting

cut problem

Hi, sample datas are : drwxr-xr-x 2 beewin abidev 96 Jun 13 2006 bwin drwxrwxr-x 2 blsmg01 smg 96 Jun 13 2006 blsmg01 drwxr-xr-x 2 ccmdummy ccm_root 8192 Jun 13 2006 ccmdum drwxr-xr-x 5 dipayan users 8192 Oct 29 09:05 dip I want to cut the last field. I use ls -ll | grep... (7 Replies)
Discussion started by: samir_standing
7 Replies

9. Shell Programming and Scripting

Problem with cut command

I am trying to take one part of my text from file and save it to variable $x I tryed this... x=`cut -c 6-9 $fajl` my file looks like this fajl: 21890001277 89386911 23638FBCDC 28EE01A1 0000 26855 124 244326 21890001277 89766911 23638FBCDC 28E021A1 0000 26557 134 684326 21890001277... (7 Replies)
Discussion started by: amon
7 Replies

10. UNIX for Dummies Questions & Answers

Cut command problem !

Hi all! Here is my problem : $ more file yougli:passwd:123456:3265:Yepa Yepo:/home/yougli:/bin/ksh As you can see, in the field "information", there are two spaces between "Yepa" and "yepo". My problem is : $ PARAM='more file | cut -d":" -f5' $ echo $PARAM Yepa Yepo Now i only... (2 Replies)
Discussion started by: tomapam
2 Replies
Login or Register to Ask a Question