Result of 'cut' into variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Result of 'cut' into variable
# 1  
Old 09-02-2014
Question Result of 'cut' into variable

Hello,

I would like to have a result of a cut-command in a variable.

The file "t.dat" has the following content:
Code:
Company                 001.239879123.OB1X.672W12.STS                    UNOLD.001.02

My bash-script:
Code:
Header=""
Header=$(cut -c1-160 t.dat | head -1)
echo $Header

The result:
Code:
Company 001.239879123.OB1X.672W12.STS UNOLD.001.02

Where are the spaces between the values?

And more important - what do I have to do to get the whole record into the variable "Header"?

Tx in advance.

CU,
API

Last edited by vbe; 09-02-2014 at 12:41 PM.. Reason: code tags for all data and code not quotes... (Result is an output no?)
# 2  
Old 09-02-2014
Hello API,

Welcome to the forum.
I can see there are only 85 characters present in the line as follows.

Code:
echo "Company                 001.239879123.OB1X.672W12.STS                    UNOLD.001.02" | awk '{print length($0)}'

Output is as follows.

85
So kindly let us know your complete requirement so that we can help you.


EDIT: Seems you have modified the post now. If you want to take the complete line into a variable then I don't think that there is a need of using cut
command there. If you want to work on lines of a file you can use while loop it will read file line by line and you can perform your operation as per your requirement.

Posted by Makarand Dodmis:
Quote:
try
Code:
$ header=`cut -c1-160 t.dat | head -1`$ echo "$header"Company 001.239879123.OB1X.672W12.STS UNOLD.001.02

Hello Makarand:
your solution will not have space in the variable as per user query, user needs the spaces in variable.


Thanks,
R. Singh

Last edited by RavinderSingh13; 09-02-2014 at 12:35 PM.. Reason: Adding solution as user has modified actual post
# 3  
Old 09-02-2014
try
Code:
$ header=`cut -c1-160 t.dat | head -1`
$ echo "$header"
Company                 001.239879123.OB1X.672W12.STS                    UNOLD.001.02

This User Gave Thanks to Makarand Dodmis For This Post:
# 4  
Old 09-02-2014
Quote:
Originally Posted by API
Where are the spaces between the values?
They got lost because you didn't quote the variable $Header, try
Code:
echo "$Header"

Quote:
Originally Posted by API
And more important - what do I have to do to get the whole record into the variable "Header"?
Code:
Header=$(head -1 t.dat)

# 5  
Old 09-02-2014
Thanks for your quick reply.

First of all - you are right I had to quote the variable "$Header". Then I will get the whole string with all the spaces in it.

For explanation why I use the "cut". I have to read a file and know if it is a single line with 160 characters or the content of the file is one record and I will need also the first 160 characaters. Therfor I can not use "head".

But thanks for this really easy solution... Smilie

CU,
API
# 6  
Old 09-02-2014
Why not read header <t.dat?
# 7  
Old 09-03-2014
Hi RudiC,

its because I could have a file with - lets say 10.000 characters. All in one line! And I need 160 characters from the beginning.

If I will do it with read, I would get the whole file - because there is only one line in file.

CU,
API
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. Shell Programming and Scripting

Store result variable

Friends have the following problem: cat $PATH_DAT/mr.txt | nawk 'BEGIN { CantPnt=0; NumReg=0; FS="|" } { NumReg++ CantPnt=CantPnt+int($2) } END{ printf... (5 Replies)
Discussion started by: tricampeon81
5 Replies

3. UNIX for Dummies Questions & Answers

I save the result in a variable

I have friends that this command worked perfectly, but I would like to save the result in a variable, which I have not achieved var=prueba.txt echo $var | cut -d "." -f 1 prueba I need to do this but does not work me salida=echo $var | cut -d "." -f 1 echo "result:$salida" ... (8 Replies)
Discussion started by: tricampeon81
8 Replies

4. Shell Programming and Scripting

How to combine two variable result?

Hi, i have two variables i.e lck_ckm_customer=ckm_customer and(present in some script A) table=ckm_customer(present in script B) in script B i am executing this part if ; then --- ---- ---- fi Now, while comparing in my log file i am getting this result if but i... (2 Replies)
Discussion started by: gnnsprapa
2 Replies

5. Shell Programming and Scripting

Print the whole line which contains the result of the command cut

Hey everyone I have a file 'agenda' which contains: Object Day Month Year Birthday 09 02 2012 i want to extract from a script the line which contains the day the user typed. for example if he type 09 the line is showed using... (4 Replies)
Discussion started by: Goldstein
4 Replies

6. Shell Programming and Scripting

result in variable

Hi all, I'll try to get a result from a search with "awk" into a variable. It works for those examples: findfirstline=`awk 'BEGIN{ mycount = 1 } { if(mycount == 1 && /^Video/){ row=NR; print row; mycount = 0; }}' ${i}` findlastline=`awk '/^Video/ {row=NR} END{print row}' ${i}` But it... (6 Replies)
Discussion started by: tempestas
6 Replies

7. Shell Programming and Scripting

Taking sed result in a variable

Hi All, How can i take the following code having three seds into a variable : echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/+/g' | sed 's/\#/'$job_date'/g' I want to get the result stored in a script variable i tried var2=`echo "$DateFileFormat" | sed 's/\./\\\\./g' |... (4 Replies)
Discussion started by: abhinav192
4 Replies

8. Shell Programming and Scripting

Assign result to variable

Hi friends, firstly, i can run following expression and i took 100 value. sqlplus -s username/password@TTTEST @umt.sql umt.sql exists "select t.deger from parametre t where t.id=30". result of this query =100 i need to assign this value(100) to variable(for example x... (2 Replies)
Discussion started by: temhem
2 Replies

9. UNIX and Linux Applications

How to get a result of a cmd into variable?

Hi , I am trying following . I need to get a result of an autosys cmd into a unix variable. The autosys cmd is autostatus -G jpm_day_today Please help me in storing the value returned by this cmd into a unix variable. Appreciate your time to read this post. (1 Reply)
Discussion started by: manchau
1 Replies

10. Shell Programming and Scripting

populate variable from remote result

Hi there i am trying to pass the result of a remote command into a variable into my script ie #!/bin/sh var = `ssh remote_box 'uname'` echo $var but all i get back is ./script.sh: var: not found However when i add a -x to put it into diag mode i get the following + ssh... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question