?
Solved

first word from the line

Posted on 2012-08-29
17
Medium Priority
?
734 Views
Last Modified: 2012-08-30
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
Comment
Question by:chanikya
  • 5
  • 5
  • 4
  • +2
17 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38345542
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38345553
Or, using awk as in your example:

str=$(echo '123 test xxxx yyyy' |awk '{ print $1 }')
0
 
LVL 85

Expert Comment

by:ozo
ID: 38345588
#!/bin/bash
str='123 test xxxx yyyy'
echo ${str/ *}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38345607
Alternatively:

#!/bin/ksh
str='123 test xxxx yyyy'
echo ${str%% *}
0
 
LVL 85

Expert Comment

by:ozo
ID: 38345613
A simpler sed:
echo '123 test xxxx yyyy' | sed 's/ .*//'
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38345631
How about good ol' "cut"?

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

Author Comment

by:chanikya
ID: 38345634
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38345656
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
 
LVL 85

Expert Comment

by:ozo
ID: 38345738
#!/bin/bash                                                                                                                                  
while read mStr; do
echo ${mStr%% *}
done < file1.txt
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38345942
cat file1.txt | while read mstr; do echo "${mstr}" | cut -d" " -f1; done;
0
 
LVL 85

Expert Comment

by:ozo
ID: 38346073
awk '{ print $1 }'  file1.txt
0
 

Author Comment

by:chanikya
ID: 38346163
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
 

Author Comment

by:chanikya
ID: 38349501
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
 
LVL 85

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 38349547
Since you apparently have bash,
why not

#!/bin/bash
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 400 total points
ID: 38349583
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
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 1200 total points
ID: 38349606
If you don't want to use bash for your script try backticks instead of $( ):

str=`echo "${str1}" | awk '{print $1 }'`
0
 

Author Closing Comment

by:chanikya
ID: 38349661
Hi Experts,

Thanks a lot for all your solutions and suggestions.

Thanks
Chanikya.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question