Problem using cut command in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem using cut command in shell script
# 1  
Old 05-10-2010
Problem using cut command in shell script

I'm new to shell programming, and am having a problem in a (Korn) shell program, which boils down to this:
The program reads a record from an input file and then uses a series of
"cut" commands to break the record into parts and assign the parts to
variables. There are no delimiters in the record. Here's a sequence of
three records from the input file:

S1830031HN106P
S1830032HN113XTA
S1830033MEI A

And here's the section of code that breaks the record into various parts,
with "InputLine" representing the record:

Code:
var1=`echo $InputLine | cut -c 1-5`
var2=`echo $InputLine | cut -c 1`
var3=`echo $InputLine | cut -c 2-5`
var4=`echo $InputLine | cut -c 6-8`
var5=`echo $InputLine | cut -c 9-13`

So the code should assign the characters in columns 9-13 to the var5
variable. The problem is that when the third record above ("S1830033MEI
A") is processed, the var5 should be assigned "MEI ", but ends up getting
assigned "MEI A". In the record, there are four spaces between the "I" and the "A", but the cut command seems to compress the consecutive whitespaces into one space, which then makes the "A" the fifth character in the variable. Is there a way to force the cut command to count each whitespace as a column, or some other simple way to assign exactly those characters in columns 9-13 to var5? I tried using various options with the cut command, but wasn't able to get the results I needed. I appreciate any help anyone can give.


.AOLWebSuite .AOLPicturesFullSizeLink { height: 1px; width: 1px; overflow: hidden; } .AOLWebSuite a {color:blue; text-decoration: underline; cursor: pointer} .AOLWebSuite a.hsSig {cursor: default}
# 2  
Old 05-10-2010
Hi.

cut does count each whitespace as a character (column, as you put it).

Here's what I get using only cut, based on your input:
Code:
$ cut -c9-13 file1
HN106
HN113
MEI

The problem is with your echo, i.e.
Code:
var5=`echo $InputLine | cut -c 9-13`

Should be:
Code:
var5=`echo "$InputLine" | cut -c 9-13`

to preserve the whitespace.
# 3  
Old 05-10-2010
Thanks a lot, scottn. That's exactly what I needed.
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 with pipe as a delimiter while using in a script

There is a text file in my project named as "mom.txt" in which i want to have contents like.................. LSCRM(Application Name): 1: This is my first application. 2: Today we did shell scripting automation for this app. 3: It was really a good fun in doing so. 4: Really good.| (Here i... (7 Replies)
Discussion started by: Abhijeet Anand
7 Replies

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

3. Shell Programming and Scripting

Cut command issue with shell script

I am writing a shell script to skip couple of feilds in a flat file and as a part of this I have written the below piece of code in it. cut -d '|' -f 1-17,19-31 $1 > filename To add pipe at end of the line I used below command, but it adds even to header and footer as well which i... (11 Replies)
Discussion started by: Aditya_001
11 Replies

4. Shell Programming and Scripting

Problem with expr command in shell script

Hi, I have used expr command to increment the date. for e.g., case 1 : echo $(date -d $(echo `expr 20010101 + 1`)) it returns Tue Jan 2 00:00:00 IST 2001 case 2: echo $(date -d $(echo `expr 20010101 - 1`)) it returns date: invalid date `20010100' please suggest me, how to... (3 Replies)
Discussion started by: nanthagopal
3 Replies

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

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

7. Shell Programming and Scripting

Problem with MV command in Shell Script

Hi Guru's, I 'm trying to execute the below given script in Unix. I am having an issue with the script. The output of the script is given below: #!/bin/bash File_Home="/home/essftp/Risk" cd /home/essftp/Risk rm -f FileList rm -f credit_risk* file1=`ls -lt... (4 Replies)
Discussion started by: ranjith_taurean
4 Replies

8. Shell Programming and Scripting

problem with sed command in shell script.

Guys, I've a problem in the "sed" command used in my shellscripts This is the problamatic line in my shell script: sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'. However If I replace the... (10 Replies)
Discussion started by: bhagat.singh-j
10 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