• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 761
  • Last Modified:

first word from the line

Hi Experts,

I have requirement as below.

say i have string like this '123 test xxxx yyyy' . I want to fetch the first string. i.e. 123

some thing like below. But it does n't work
str=echo '123 test xxxx yyyy' |awk { print $1 }

Note: Not from the file .. But from the line

Can you please provide your ideas.

Thanks,
Chanikya.
0
chanikya
Asked:
chanikya
  • 5
  • 5
  • 4
  • +2
3 Solutions
 
farzanjCommented:
Like
 echo '123 test xxxx yyyy' | sed 's/\([^ ]*\).*/\1/'

Open in new window


If you want to store it,

str=$(echo '123 test xxxx yyyy' | sed 's/\([^ ]*\).*/\1/')

Open in new window

0
 
woolmilkporcCommented:
Or, using awk as in your example:

str=$(echo '123 test xxxx yyyy' |awk '{ print $1 }')
0
 
ozoCommented:
#!/bin/bash
str='123 test xxxx yyyy'
echo ${str/ *}
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
woolmilkporcCommented:
Alternatively:

#!/bin/ksh
str='123 test xxxx yyyy'
echo ${str%% *}
0
 
ozoCommented:
A simpler sed:
echo '123 test xxxx yyyy' | sed 's/ .*//'
0
 
woolmilkporcCommented:
How about good ol' "cut"?

str=$(echo '123 test xxxx yyyy' | cut -f1 -d" ")
0
 
chanikyaAuthor Commented:
Hi Experts,

Both are working fine.. seperately.. But when i am trying use variable in the place hard coded string. it is not working..  Please let me know what i am missing here.

#!/bin/sh
while read mStr; do
str=$(echo "${mStr}" | sed 's/\([^ ]*\).*/\1/')
#str=$(echo  "${mStr}" |awk '{ print $1 }')
echo $str

done < file1.txt

Thanks
Chanikya.
0
 
woolmilkporcCommented:
What are the results of your version?

What's in file1.txt?

If file1.txt contains just strings like the one in your example it should work fine.
0
 
ozoCommented:
#!/bin/bash                                                                                                                                  
while read mStr; do
echo ${mStr%% *}
done < file1.txt
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
cat file1.txt | while read mstr; do echo "${mstr}" | cut -d" " -f1; done;
0
 
ozoCommented:
awk '{ print $1 }'  file1.txt
0
 
chanikyaAuthor Commented:
Hi woolmilkporc,

Yes the file contains the lines similar to as i specified in the string.

file1.txt

1 ttt
2 xxx
4 ggg

Note:I would like to check with line not with entire file.

Thanks
Chanikya..
0
 
chanikyaAuthor Commented:
Hi Experts,

Pleas let me know where i am doing wrong.
-bash-3.00$ str1="123 567 897"
-bash-3.00$ str=$(echo ${str1} | awk '{print $1 }')
-bash-3.00$ echo $str
123       

Open in new window

 --- It is working as expected. But the below code is not working
-bash-3.00$ more filecompare.sh
#!/bin/sh
while read str1; do
echo "$str1"
str=$(echo "${str1}" | awk '{print $1 }')
echo $str
#  mCnt=$`egrep -c ${mStr} file2.txt`
#echo ${mCnt}
#  if  test ${mCnt} -eq  "0" ; then
#    echo "ABSENT"
#  else
#    echo ${mStr}
#  fi

done < file1.txt

Open in new window

Here you see the error
-bash-3.00$ sh filecompare.sh
filecompare.sh: syntax error at line 4: `str=$' unexpected
-bash-3.00$

Open in new window


-bash-3.00$ more file1.txt
1 test
2 xxx
3 zzz
-bash-3.00$

Open in new window


Thanks,
Chanikya.
0
 
ozoCommented:
Since you apparently have bash,
why not

#!/bin/bash
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
For readability, I'd change the layout a bit and use variables the same way:

while read str1
do
	echo "${str1}"
	str=$(echo "${str1}" | awk '{print $1 }')
	echo "${str}"
done < file1.txt

Open in new window

saveas filecompare.sh then:
chmod +x filecompare.sh to make the script executable.

Run with ./filecompare.sh with file1.txt in the same folder, output:

1 test
1
2 xxx
2
3 zzz
3
0
 
woolmilkporcCommented:
If you don't want to use bash for your script try backticks instead of $( ):

str=`echo "${str1}" | awk '{print $1 }'`
0
 
chanikyaAuthor Commented:
Hi Experts,

Thanks a lot for all your solutions and suggestions.

Thanks
Chanikya.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 5
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now