Unix code to fetch the first field till space in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix code to fetch the first field till space in a variable
# 1  
Old 08-27-2008
Unix code to fetch the first field till space in a variable

Hi,

I want to get the value of the first field till space in a variable.

e.g x=323 /test/personel/logs/File1 [After 3 space is there]

I want to get the first field till space i.e 323 in another variable ,lets say y.

echo $x|cut -d' ' -f1 gives 323
but when I'm trying y=`echo $x|cut -d' ' -f1 its giving error.

Please help.
# 2  
Old 08-27-2008
Hi! to be sure, since your value has a space in between them, enclosed them with a double quotes, like this:
Code:
x="323 /test/personel/logs/File1"

In your command substitution(using backticks), I don't know if it's a typo or what? But it should be written as this,
Code:
y=`echo $x|cut -d' ' -f1`

# 3  
Old 08-27-2008
y=`echo $x|awk '{print $1}'`
# 4  
Old 08-27-2008
It's both safer and more efficient to use the shell's built-in variable substitution syntax.

Code:
y=${x% *}

To be sure, echo $x without double-quoting the variable name $x will wreck the whitespace in the variable's value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch field to variable

Input.txt NOTE1|VALUE1|HMM|UPSTR| NOTE2||HMP|UPSTR| NOTE3|VALUE3|HML|UPSTR| NOTE4||HMD|UPSTR| NOTE5|VALUE5|HMS|UPSTR| NOTE6|VALUE6|HSD|UPSTR| NOTE7||HSS|UPSTR| NOTE8|VALUE8|HEE|UPSTR| NOTE9|VALUE9|HER|DOWNSTR| NOTE50|VALUE1|HEE|DOWNSTR| I am trying to read field 1, 2 and 3 from the... (4 Replies)
Discussion started by: Joselouis
4 Replies

2. UNIX for Advanced & Expert Users

[Solved] Unable to fetch the UNIX variable information

I am having a file called variable_info.ksh in that file I am having following global variable information like… EMAIL_PATH=/var/mail TMP_PATH=/home/tmp And we are having another temporary parameter file abcd.txt, in that file we are having the following information like… EMAIL|EMAI_PATH I... (4 Replies)
Discussion started by: mesahammad
4 Replies

3. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: "Liberation Sans"; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

4. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

5. Shell Programming and Scripting

Print characters till the next space when the pattern is found

i have a file which contains alphanumeric data in every line. what i need is the data after certain pattern. the data after the pattern is not of fixed length so i need the data till the space after the pattern. Input file: bfdkasfbdfg khffkf lkdhfhdf pattern (datarequired data not required)... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

6. Shell Programming and Scripting

Bash take word after specific point and till next space?

Hello, I have an output like Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - and I want to take only the 'wlan0' string. This can be done by a="Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - " b=${a:25:6} echo $bThe thing is that wlan0 can be something else, like eth0 or... (2 Replies)
Discussion started by: hakermania
2 Replies

7. Shell Programming and Scripting

Remove line starting from space till end.

Hi, I have a code tag, from which i have the below snippet: intelrpt.GetCMB_FB type=ODBC> intelrpt.GetCMB_FB type=SYBASE> I want the output like: intelrpt.GetCMB_FB intelrpt.GetCMB_FB That is remove the lines starting from WHITESPACE till end. Please help. I am new to... (7 Replies)
Discussion started by: anupdas
7 Replies

8. Shell Programming and Scripting

way to print all the string till we get a space and a number

Is there any way to print all the string till we get a space and a number and store it a variable for eg we have string java.io.IOException: An existing connection was forcibly closed by the remote host 12 All I want is to store "java.io.IOException: An existing connection was forcibly closed... (13 Replies)
Discussion started by: villain41
13 Replies

9. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

10. UNIX for Dummies Questions & Answers

fetch Variable value

Hi Guys, I have written a script that declares all the variables and its values in a conf file. Now i use a variable whose value i need to change it in one of the sub-file that is used in the script. In the startup file i want to print or check its value. The value get changed and printed... (5 Replies)
Discussion started by: Swapna173
5 Replies
Login or Register to Ask a Question