Link to home
Start Free TrialLog in
Avatar of chanikya
chanikya

asked on

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.
Avatar of farzanj
farzanj
Flag of Canada image

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

Or, using awk as in your example:

str=$(echo '123 test xxxx yyyy' |awk '{ print $1 }')
#!/bin/bash
str='123 test xxxx yyyy'
echo ${str/ *}
Alternatively:

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

str=$(echo '123 test xxxx yyyy' | cut -f1 -d" ")
Avatar of chanikya
chanikya

ASKER

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.
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.
#!/bin/bash                                                                                                                                  
while read mStr; do
echo ${mStr%% *}
done < file1.txt
cat file1.txt | while read mstr; do echo "${mstr}" | cut -d" " -f1; done;
awk '{ print $1 }'  file1.txt
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..
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.
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Experts,

Thanks a lot for all your solutions and suggestions.

Thanks
Chanikya.