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.
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.
Or, using awk as in your example:
str=$(echo '123 test xxxx yyyy' |awk '{ print $1 }')
str=$(echo '123 test xxxx yyyy' |awk '{ print $1 }')
#!/bin/bash
str='123 test xxxx yyyy'
echo ${str/ *}
str='123 test xxxx yyyy'
echo ${str/ *}
Alternatively:
#!/bin/ksh
str='123 test xxxx yyyy'
echo ${str%% *}
#!/bin/ksh
str='123 test xxxx yyyy'
echo ${str%% *}
A simpler sed:
echo '123 test xxxx yyyy' | sed 's/ .*//'
echo '123 test xxxx yyyy' | sed 's/ .*//'
How about good ol' "cut"?
str=$(echo '123 test xxxx yyyy' | cut -f1 -d" ")
str=$(echo '123 test xxxx yyyy' | cut -f1 -d" ")
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.
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.
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
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
ASKER
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..
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..
ASKER
Hi Experts,
Pleas let me know where i am doing wrong.
Thanks,
Chanikya.
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
--- 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
Here you see the error-bash-3.00$ sh filecompare.sh
filecompare.sh: syntax error at line 4: `str=$' unexpected
-bash-3.00$
-bash-3.00$ more file1.txt
1 test
2 xxx
3 zzz
-bash-3.00$
Thanks,
Chanikya.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Experts,
Thanks a lot for all your solutions and suggestions.
Thanks
Chanikya.
Thanks a lot for all your solutions and suggestions.
Thanks
Chanikya.
Open in new window
If you want to store it,
Open in new window