Cut in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut in shell script
# 1  
Old 05-11-2011
Cut in shell script

Hi All,

Am a Beginner to shell scripting, so please pardon my question.

Below is the file format of file a.txt

a.txt
Code:
F=3
a|b|c|d
1|2|3|4
as|as|asd|asd
Aas|as|asd|ada

In the above file format, i have record count of total no of records excluding the header file here 3 , this varies depending upon the no of records in the file.

I am using the below in command mode .
Code:
head -1 a.txt | cut -c 3-

and am getting the output correctly which is 3.

How ever when i put the same above command in shell script, am not getting the correct answer .

Below is the script
Code:
#!/bin/ksh
count=`head -1 a.txt | cut -c 3-`
echo $count

Am getting the variable count displayed as $'3\r' , Can anyone please help me if am missing something here obvious.

I cant process the downstream program, because of this issue.

Thanks for looking into the issue.

Last edited by Franklin52; 05-11-2011 at 03:43 AM.. Reason: Please use code tags
# 2  
Old 05-11-2011
Your source file is in DOS format (CR-LF) instead of Unix format (LF ending).
One workaround would be:
Code:
 count=$(head -1 a.txt | dos2unix | cut -c 3-)

# 3  
Old 05-11-2011
Very Thanks jlliagre , it works like Magic...it is indeed copied over from windows.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut string from shell

How can I cut the date and leave it in 3 variable? date="20181219" year=substr(date,1,4) monthsubstr(date,4,2) daysubstr(date,6,2) result year=2018 month=12 day=19 this does not work for me (3 Replies)
Discussion started by: tricampeon81
3 Replies

2. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 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

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

5. Shell Programming and Scripting

Shell scripting- cut the location of the file

Hi, I need to just cut the location of file and give it as input to the other command. For example: ct lsview | grep kavya- * kavya-telecom-view /first/nso01/disk2/views/kavya-telecom-view.vws I need only the location like /first/nso01/disk2/views/kavya-telecom-view.vws.How to cut that... (4 Replies)
Discussion started by: kkalyan
4 Replies

6. OS X (Apple)

Shell Script to change desktop short cut Icon

I have installed my flash application using shell script. I have created short cut to desktop. Now i want to change the default short cut Icon. Please tell me script to change the short cut icon. ---------- Post updated at 12:54 AM ---------- Previous update was at 12:33 AM ---------- Working... (0 Replies)
Discussion started by: rohaneee
0 Replies

7. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: joroca
2 Replies

8. Shell Programming and Scripting

How to cut, concatenate data in Shell Script

Hello All,, I'm very new to Unix and I'm more in SAP ABAP. I have a requirement for a shell script which will accept one parameter as Input file name(with directory) e.g : /sapdata/deubank/dbdirect/out/deu01deupe/in/deu01deupe20051207111320.pmt In the shell script I have to make two more... (2 Replies)
Discussion started by: vasan_srini
2 Replies

9. UNIX for Dummies Questions & Answers

Shell script: Cut / (slash) character in string

I have a string "\/scratch\/databases\". I want to have a new string "\/scratch\/databases" by cutting last '\' character using shell script. I can't do this Please help me. Thanks in advance ThuongTranVN (4 Replies)
Discussion started by: ThuongTranVN
4 Replies
Login or Register to Ask a Question