Cut Command value assign to variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut Command value assign to variable
# 1  
Old 06-18-2009
Cut Command value assign to variable

Hi,
I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful.

Sample Header: HJAN BALANCE 20090616
I need to retrieve the date here, which always ranges from the 18-25 position.

All I am trying to do is:

echo "Header Line: $headerLine"
headerDate = cut -c18-25 ${headerLine}

Here $headerLine has a valid value. But the command doesnt work.

Kindly assist.

Thanks,
Raghu
# 2  
Old 06-18-2009
Try...
Code:
headerDate=`cut -c18-25 ${headerLine}`

# 3  
Old 06-18-2009
Thank you Rakesh.
I tried this. Below is the response:

Header Line: HJAN BALANCE 20090616
cut: cannot open HJAN
cut: cannot open BALANCE
cut: cannot open 20090616
Launch_LKW-Gen.ksh[136]: headerDate: not found
Header Date:
# 4  
Old 06-18-2009
Try...
Code:
headerDate=$(expr substr "${headerLine}" 18 8)

# 5  
Old 06-18-2009
Following is the response:

Header Line: HJAN BALANCE 20090616
expr: syntax error

Does it have to do with the double quotes?

Last edited by ragz_82; 06-18-2009 at 02:39 AM.. Reason: Typo
# 6  
Old 06-18-2009
headerDate=`echo $headerLine | cut -c18-25`
# 7  
Old 06-18-2009
Amit,
This option worked. But the results are not as expected and also varies for different inputs. Kindly take a look:

Header Line: HJAN BALANCE 20090616
Header Date: 0616
Header Line: HJAN HOLDING 20090616
Header Date: 0616
Header Line: HJAN CASHLEDGER20090616
Header Date: 090616
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Assign a command to a variable - Help

Hi, I have the script below. When i assign SSH_COMMAND to "ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}" and then execute it as ${SSH_COMMAND} I get the following error: ssh: Could not resolve hostname sudo: Name or service not known ssh: Could not resolve hostname sudo: Name or... (3 Replies)
Discussion started by: mohca2020
3 Replies

2. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

3. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

4. Shell Programming and Scripting

How to assign value to variable using cut?

Hey guys. Below is the command I am using to assign "yellow" to the variable yellow. I can seem to echo it out using xargs but when I assign it to yellow and then try to echo it out it prints a blank line. Below is the command. Your help is highly appreciated! cut -c 2- color.txt |... (11 Replies)
Discussion started by: eldan88
11 Replies

5. Shell Programming and Scripting

Assign the result of a multiline command to a variable

Hi, I have the following command that lists all the .o files from all the directories except of vwin (which I don't want it) for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done The result is something like that dir1/file1.o dir1/file2.o dir2/file3.o etc. So, I want to create a... (9 Replies)
Discussion started by: scor6800
9 Replies

6. Shell Programming and Scripting

Unable to assign command output to variable

Code set -x STATUS="0" echo $STATUS for i in `ls -ltr Report*|awk '{ print $9 }'` do if then flg = "`head -1 "$i" |cut -c 31-33`" echo `head -1 "$i" |cut -c 31-33` echo $flg if then echo "having Fun" STATUS="2" else echo "no Fun" fi fi (2 Replies)
Discussion started by: Funkeydude
2 Replies

7. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

8. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

9. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies

10. Shell Programming and Scripting

assign awk command result to a variable

#!/bin/sh # ## MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist` if then echo String not found defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies
Login or Register to Ask a Question